Skip to content

Commit

Permalink
Fix task key comp (#15190)
Browse files Browse the repository at this point in the history
  • Loading branch information
cicdw authored Sep 3, 2024
1 parent 67518bb commit 8bc4939
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/prefect/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,17 @@ def _generate_task_key(fn: Callable[..., Any]) -> str:

qualname = fn.__qualname__.split(".")[-1]

try:
code_obj = getattr(fn, "__code__", None)
if code_obj is None:
code_obj = fn.__call__.__code__
except AttributeError:
raise AttributeError(
f"{fn} is not a standard Python function object and could not be converted to a task."
) from None

code_hash = (
h[:NUM_CHARS_DYNAMIC_KEY] if (h := hash_objects(fn.__code__)) else "unknown"
h[:NUM_CHARS_DYNAMIC_KEY] if (h := hash_objects(code_obj)) else "unknown"
)

return f"{qualname}-{code_hash}"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ def test_task_key_after_import(self):

assert noop.task_key.startswith("noop-")

def test_task_key_with_funky_class(self):
class Funky:
def __call__(self, x):
return x

# set up class to trigger certain code path
# see https://github.com/PrefectHQ/prefect/issues/15058
funky = Funky()
funky.__qualname__ = "__main__.Funky"
if hasattr(funky, "__code__"):
del funky.__code__

tt = task(funky)
assert tt.task_key.startswith("Funky-")


class TestTaskRunName:
def test_run_name_default(self):
Expand Down

0 comments on commit 8bc4939

Please sign in to comment.