Ole
01/23/2025, 10:55 AMMarvin
01/23/2025, 10:57 AM@sync_compatible
decorator and mypy's inference in Prefect 3.1.13 may stem from how type hints are currently interpreted by mypy in conjunction with Prefect's decorators. The documentation might suggest that such functions should be inferred as synchronous, but mypy's behavior could differ due to the way the decorator is implemented or how type hints are propagated.
Here are a few steps you can take to address this:
1. Check for Updates: Ensure that you're using the latest version of both Prefect and mypy. There have been updates related to mypy requirements in Prefect, as seen in this pull request.
2. Explicit Type Hints: You might need to explicitly annotate your function with a synchronous return type to guide mypy's inference.
3. Custom mypy Plugins: Consider writing a custom mypy plugin if this behavior is critical and cannot be resolved through configuration or updates.
4. Report or Follow Issues: If this is a consistent issue, it might be worth reporting it on the Prefect GitHub repository if it hasn't been addressed. You can also follow existing issues or discussions there for any updates.
For further details, you might want to check the Prefect GitHub issues for related discussions or updates on this topic.Ole
01/23/2025, 12:52 PMMarvin
01/23/2025, 12:53 PM@sync_compatible
decorator. However, the transition to more comprehensive type annotations in Prefect 3.x might have inadvertently affected how these decorators interact with type checkers like mypy.
If you observed the change in behavior starting from version 3.1.6, it suggests that some internal modifications related to typing or the decorator's implementation might have occurred. Unfortunately, without explicit documentation or a changelog entry, pinpointing the exact change is challenging.
For now, consider the following steps:
- Review Prefect's GitHub Repository: Check the Prefect GitHub repository for any merged pull requests or issues around the time of the 3.1.6 release that might relate to typing or decorators.
- Explicit Type Annotations: As a workaround, explicitly annotate your functions with the desired synchronous types to guide mypy's inference.
If the problem persists or is critical, you might consider raising an issue on the Prefect GitHub repository to get more targeted insights from the maintainers.Ole
01/23/2025, 1:07 PMVariable.get
as an example for a method annotated with async_compatible (also affected) and CustomWebhookNotificationBlock.load
as an example for @sync_compatible.Marvin
01/23/2025, 1:07 PM@sync_compatible
and @async_compatible
are inferred by mypy as returning Coroutine[Any, Any, Any]
, despite the expectation that they should be inferred as synchronous. This behavior was not present in version 3.1.5.
Environment:
- Prefect version: 3.1.6 and above
- Mypy version: [your mypy version]
- Python version: [your Python version]
Steps to Reproduce:
1. Create a Prefect flow with a synchronous function that uses Variable.get
and CustomWebhookNotificationBlock.load
.
2. Decorate these functions with the @sync_compatible
and @async_compatible
decorators.
3. Run mypy type checking on the code.
Expected Behavior:
Mypy should infer the return type of these functions as synchronous, in line with the Prefect documentation.
Actual Behavior:
Mypy infers the return type as Coroutine[Any, Any, Any]
.
Code Example:
python
from prefect import flow
from prefect.blocks.notifications import CustomWebhookNotificationBlock
from prefect.variables import Variable
@flow
def example_flow():
# Example of a function using @async_compatible
value = Variable.get("example_variable", default="default_value")
print(value)
# Example of a function using @sync_compatible
webhook_block = CustomWebhookNotificationBlock.load("my_webhook")
print(webhook_block)
if __name__ == "__main__":
example_flow()
Additional Context:
This issue started occurring from Prefect version 3.1.6. The behavior was as expected in version 3.1.5.
Suggested Labels:
- bug
- type-inference
- mypy
---
You can submit this report to the Prefect GitHub issues page for further assistance from the maintainers.