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

UnionType of Callable[P, Awaitable[T]] | Callable[P, T] not being matched by async function. #16646

Open
cvm-a opened this issue Dec 10, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@cvm-a
Copy link

cvm-a commented Dec 10, 2023

Bug Report
I'm trying to annotate a decorator that can take both async and normal functions using

def decorator(func: Callable[P, Awaitable[T]] | Callable[P, T])
...

The Callable[P, Awaitable[T]] variant does not match when using the decorator with an async function and instead raises an error
"Argument 1 to "decorator" has incompatible type "Callable[[int], Coroutine[Any, Any, str]]"; expected "Callable[[VarArg(Never), KwArg(Never)], Awaitable[Never]] | Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type]"

This may be related to #16451 which was fixed by 1.7.1

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&flags=new-type-inference&gist=c5255c4185cf954aa32bc8f8b2c04c10
Expected Behavior
Both sync and async should be type matched correctly

Actual Behavior

I get a strange error with the async variant (main.py:61: error: Argument 1 to "decorator" has incompatible type "Callable[[int], Coroutine[Any, Any, str]]"; expected "Callable[[VarArg(Never), KwArg(Never)], Awaitable[Never]] | Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type])

Your Environment

  • Mypy version used: 1.7.1 compiled
  • Mypy command-line flags: new-type-inference (by default)
  • Python version used: 3.11.6

Also replicated in the playground with 1.7.1 compiled and Python 3.11 and 3.12.

@cvm-a cvm-a added the bug mypy got something wrong label Dec 10, 2023
@practual
Copy link

practual commented Dec 15, 2023

I believe the same issue can be seen when handling coroutine objects directly: https://mypy-play.net/?mypy=latest&python=3.12&gist=41efe2212da7ae2596cf34b0577aead7

main.py:27: error: Argument 1 to "wrap_as_coroutine" has incompatible type "Coroutine[Any, Any, str]"; expected "Coroutine[Any, Any, Never]"  [arg-type]

As a workaround, I can add @overload rather than defining a Union of types, but it shouldn't be necessary: https://mypy-play.net/?mypy=latest&python=3.12&gist=6fb7b237cd11b5bfe192108543e1ac36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants