Skip to content

Commit

Permalink
Fixes bug where assignments inside functions are evaluated when runni…
Browse files Browse the repository at this point in the history
…ng `prefect deploy` (#14403)
  • Loading branch information
desertaxle authored Jun 28, 2024
1 parent e368eb5 commit 33f245d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/prefect/utilities/importtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def safe_load_namespace(source_code: str):
logger.debug("Failed to import from %s: %s", node.module, e)

# Handle local definitions
for node in ast.walk(parsed_code):
for node in parsed_code.body:
if isinstance(node, (ast.ClassDef, ast.FunctionDef, ast.Assign)):
try:
# Compile and execute each class and function definition and assignment
Expand Down
23 changes: 23 additions & 0 deletions tests/utilities/test_importtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,26 @@ def test_safe_load_namespace_ignores_code_in_if_name_equals_main_block():
assert "x" in namespace
assert "y" in namespace
assert "z" not in namespace


def test_safe_load_namespace_does_not_execute_function_body():
"""
Regression test for https://github.com/PrefectHQ/prefect/issues/14402
"""
source_code = dedent(
"""
you_done_goofed = False
def my_fn():
nonlocal you_done_goofed
you_done_goofed = True
def my_other_fn():
foo = my_fn()
"""
)

# should not raise any errors
namespace = safe_load_namespace(source_code)

assert not namespace["you_done_goofed"]

0 comments on commit 33f245d

Please sign in to comment.