Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix task key comp #15190

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading