-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
A few mypy fixups #3346
A few mypy fixups #3346
Conversation
- Fix `__signature__` of `Task` types to work on both the class and on instances. - Fix `task` decorator to support type checking. mypy doesn't like decorators that accept optional arguments, to handle this we have to add additional annotations. - *Almost* fix `resource_manager` decorator to support type checking. This is annoying, mypy assumes (incorrectly) that the output of a class decorator is always an instance of the class, even if the decorator type signature says otherwise.
The type checking this supports only checks that the output of a Demo: from prefect import task, Flow
@task
def hello(name: str) -> str:
print("Hello %s" % name)
return name
@task(name="hello-2")
def hello2(name: str) -> None:
print("Hello %s" % name)
reveal_type(hello)
reveal_type(hello2)
with Flow("greeter") as flow:
res = hello("world")
reveal_type(res)
res2 = hello2(res)
reveal_type(res2)
flow.run() $ mypy test.py
test2.py:15: note: Revealed type is 'prefect.tasks.core.function.FunctionTask'
test2.py:16: note: Revealed type is 'prefect.tasks.core.function.FunctionTask'
test2.py:21: note: Revealed type is 'prefect.core.task.Task'
test2.py:23: note: Revealed type is 'prefect.core.task.Task' |
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.
I'll admit I'm not 100% in the loop on overload
here, but the changes seem safe and valuable, so LGTM!
I am also not a mypy user, but per googling on how to do this, this appears to be the recommended approach. |
__signature__
ofTask
types to work on both the class and oninstances.
task
decorator to support type checking. mypy doesn't likedecorators that accept optional arguments, to handle this we have to
add additional annotations.
resource_manager
decorator to support type checking.This is annoying, mypy assumes (incorrectly) that the output of a
class decorator is always an instance of the class, even if the
decorator type signature says otherwise.
Fixes #3056.
Related to, but doesn't fix #3191.
changes/
directory (if appropriate)docs/outline.toml
for API reference docs (if appropriate)