Skip to content

3.1.6+: Mypy 1.14.1 incorrectly infers Coroutine return type for @sync_compatible and @async_dispatch decorators #16825

@neurolabs

Description

@neurolabs

Bug summary

Starting from Prefect version 3.1.6, functions decorated with @sync_compatible and @async_dispatch are inferred by mypy as returning Coroutine[Any, Any, R?], despite the expectation that they should be inferred as synchronous as described in the docstring. This behavior was not present in version 3.1.5.

MyPy version: 1.14.1

Reproduction (for prefect 3.1.13):

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_dispatch
    value: StrictVariableValue = Variable.get("example_variable")
    
    # Example of a function using @sync_compatible
    webhook_block: CustomWebhookNotificationBlock = CustomWebhookNotificationBlock.load("my_webhook")

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 "R | Coroutine[Any, Any, R]".

$ mypy test.py 
test.py:10: error: Incompatible types in assignment (expression has type "str | int | float | dict[str, Any] | list[Any] | Coroutine[Any, Any, str | int | float | dict[str, Any] | list[Any] | None] | None", variable has type "str | int | bool | float | dict[str, Any] | list[Any] | None")  [assignment]
test.py:13: error: Incompatible types in assignment (expression has type "Any | Coroutine[Any, Any, Any]", variable has type "CustomWebhookNotificationBlock")  [assignment]
Found 2 errors in 1 file (checked 1 source file)

Version info

Version:             3.1.13
API version:         0.8.4
Python version:      3.12.8
Git commit:          16e85ce3
Built:               Fri, Jan 17, 2025 8:46 AM
OS/Arch:             linux/x86_64
Profile:             ephemeral
Server type:         server
Pydantic version:    2.10.3
Integrations:
  prefect-gcp:       0.6.2

and also with

Version:             3.1.6
API version:         0.8.4
Python version:      3.12.8
Git commit:          6bfce9e8
Built:               Wed, Dec 11, 2024 10:46 AM
OS/Arch:             linux/x86_64
Profile:             ephemeral
Server type:         server
Pydantic version:    2.10.3
Integrations:
  prefect-gcp:       0.6.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions