-
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
Improve type hinting docs for sync_compatible #15327
Improve type hinting docs for sync_compatible #15327
Conversation
CodSpeed Performance ReportMerging #15327 will not alter performanceComparing Summary
|
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.
Thanks for the PR @benjamincerigo! I've suggested an alternate wording for clarity, but this comment will be very useful for future developers!
src/prefect/utilities/asyncutils.py
Outdated
When adding type hints for functions that are decorated with `@sync_compatible` mypy will infer | ||
the return type as if the function is sync: the return type on the wrapped function rather then | ||
a coroutine. If you want to handle coroutines yourself you will need to ignore the types and | ||
"cast" the return type to a coroutine. For example: | ||
```python | ||
result: Coroutine = sync_compatible(my_async_function)(arg1, arg2) # type: ignore | ||
``` |
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.
When adding type hints for functions that are decorated with `@sync_compatible` mypy will infer | |
the return type as if the function is sync: the return type on the wrapped function rather then | |
a coroutine. If you want to handle coroutines yourself you will need to ignore the types and | |
"cast" the return type to a coroutine. For example: | |
```python | |
result: Coroutine = sync_compatible(my_async_function)(arg1, arg2) # type: ignore | |
``` | |
Note: Type checkers will infer functions decorated with `@sync_compatible` are synchronous. If you want to use the decorated function in an async context, you will need to ignore the types and | |
"cast" the return type to a coroutine. For example: | |
```python | |
result: Coroutine = sync_compatible(my_async_function)(arg1, arg2) # type: ignore | |
``` |
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.
@desertaxle thanks for the suggestions, I have fixed this up.
A quick fix for the issue: PrefectHQ#15275
491aaf2
to
5e34445
Compare
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.
Thank you!
This PR aims to improve the docs so that if a user is typing the return of a function that is decorated by
@sync_compatible
they are aware of its behaviour.It is a quick fix for the issue:
Closes: #15275