Skip to content

Commit

Permalink
Changed TaskSource key computation to handle `OSError("source not a…
Browse files Browse the repository at this point in the history
…vailable")` (#15583)

Co-authored-by: Chris White <white.cdw@gmail.com>
  • Loading branch information
kzvezdarov and cicdw authored Oct 4, 2024
1 parent 50cedb2 commit a12aeaf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/prefect/cache_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def compute_key(
except TypeError:
lines = inspect.getsource(task_ctx.task.fn.__class__)
except OSError as exc:
if "could not get source code" in str(exc):
if "source code" in str(exc):
lines = task_ctx.task.fn.__code__.co_code
else:
raise
Expand Down
23 changes: 11 additions & 12 deletions tests/test_cache_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,17 @@ def task_b_fn():
task_ctx_a = TaskRunContext.model_construct(task=mock_task_a)
task_ctx_b = TaskRunContext.model_construct(task=mock_task_b)

with patch(
"inspect.getsource", side_effect=OSError("could not get source code")
):
fallback_key_a = policy.compute_key(
task_ctx=task_ctx_a, inputs=None, flow_parameters=None
)
fallback_key_b = policy.compute_key(
task_ctx=task_ctx_b, inputs=None, flow_parameters=None
)

assert fallback_key_a and fallback_key_b
assert fallback_key_a != fallback_key_b
for os_error_msg in {"could not get source code", "source code not available"}:
with patch("inspect.getsource", side_effect=OSError(os_error_msg)):
fallback_key_a = policy.compute_key(
task_ctx=task_ctx_a, inputs=None, flow_parameters=None
)
fallback_key_b = policy.compute_key(
task_ctx=task_ctx_b, inputs=None, flow_parameters=None
)

assert fallback_key_a and fallback_key_b
assert fallback_key_a != fallback_key_b


class TestDefaultPolicy:
Expand Down

0 comments on commit a12aeaf

Please sign in to comment.