-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
✅ Deploy Preview for prefect-orion ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
src/prefect/utilities/importtools.py
Outdated
@@ -26,6 +26,8 @@ def to_qualified_name(obj: Any) -> str: | |||
Returns: | |||
str: the qualified name | |||
""" | |||
if not hasattr(obj, "__qualname__"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update this in tasks/flows.py
to call to_qualified_name(type(obj))
instead? to_qualified_name
should throw an error for instances because we will not be able to recreate it in from_qualified_name
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I don't think that is a good idea because it will break existing behavior (?)
In [1]: def f():
...: pass
...:
In [2]: type(f).__qualname__
Out[2]: 'function'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh we'd still want to include some if
logic, e.g. if not inpect.isfunction(obj): ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, moving the logic to Task.__init__
, something like:
if not hasattr(obj, "__qualname__"):
self.task_key = to_qualified_name(type(obj))
else:
self.task_key = to_qualified_name(obj)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep!
@jmg-duarte The following test fails def test_task_run_from_callable_object(self):
class Foo:
message = "hello"
def __call__(self, prefix: str, suffix: str) -> Any:
return prefix + self.message + suffix
obj = Foo()
foo = Task(fn=obj)
@flow
def bar():
return foo("a", suffix="b")
assert bar() == "ahellob"
I may have to revert this change. Do you have time to look at a fix? |
@madkinsz as a quick fix: adding |
Ah I see I didn't even notice that it was in the |
Sorry, I don't understand what you mean by this and by extension, which path forward to take: revert or apply the suggestion I gave |
Sorry! Can you:
|
Fixes #7196
Example
The following example should now work:
Checklist
<link to issue>
"fix
,feature
,enhancement