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

Add support for using callable objects as tasks #7217

Merged
merged 2 commits into from
Oct 19, 2022
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
6 changes: 5 additions & 1 deletion src/prefect/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ def __init__(
raise_for_reserved_arguments(self.fn, ["return_state", "wait_for"])

self.tags = set(tags if tags else [])
self.task_key = to_qualified_name(self.fn)

if not hasattr(self.fn, "__qualname__"):
self.task_key = to_qualified_name(type(self.fn))
else:
self.task_key = to_qualified_name(self.fn)

self.cache_key_fn = cache_key_fn
self.cache_expiration = cache_expiration
Expand Down
11 changes: 10 additions & 1 deletion tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import inspect
import warnings
from typing import Dict, List
from typing import Any, Dict, List
from unittest.mock import MagicMock
from uuid import UUID

Expand Down Expand Up @@ -155,6 +155,15 @@ def bar():
with pytest.raises(ValueError, match="Test"):
state.result()

def test_task_supports_callable_objects(self):
class A:
def __call__(self, *_args: Any, **_kwargs: Any) -> Any:
return "hello"

a = A()
task = Task(fn=a, name="Task")
assert task.fn is a


class TestTaskRun:
def test_task_run_outside_flow_raises(self):
Expand Down