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

Improve type hinting docs for sync_compatible #15327

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

benjamincerigo
Copy link
Contributor

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

@github-actions github-actions bot added the bug Something isn't working label Sep 11, 2024
Copy link

codspeed-hq bot commented Sep 11, 2024

CodSpeed Performance Report

Merging #15327 will not alter performance

Comparing benjamincerigo:improve-doc-mypy-sync (5e34445) with main (6e9adb8)

Summary

✅ 3 untouched benchmarks

Copy link
Member

@desertaxle desertaxle left a 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!

Comment on lines 345 to 350
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
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
```

Copy link
Contributor Author

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.

Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@desertaxle desertaxle merged commit 1aa4a7a into PrefectHQ:main Sep 16, 2024
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migration from v2.2 to v3 breaks mypy typing of my_flow.deploy()
2 participants