Open
Description
Code:
I was trying to define a callable that can be async or not and use a TypeVar as the return type, but it seem to be broken, here's the full example:
from typing import Union, Awaitable, Callable, TypeVar, Any
T = TypeVar("T")
_RESOLVER_TYPE = Union[
Callable[..., T],
Callable[..., Awaitable[T]],
]
def x() -> int:
return 1
async def a_x() -> int:
return 1
def field_ok(
resolver: _RESOLVER_TYPE,
) -> Any:
...
def field_broken(
resolver: _RESOLVER_TYPE[T],
) -> Any:
...
field_ok(x)
field_ok(a_x)
field_broken(x)
field_broken(a_x)
if I don't pass T it seem to work, no sure what is wrong, the error is:
main.py:31: error: Argument 1 to "field_broken" has
incompatible type "Callable[[], Coroutine[Any, Any, int]]";
expected "Union[Callable[..., <nothing>], Callable[..., Awaitable[<nothing>]]]" [arg-type]
Here's the playground url: https://mypy-play.net/?mypy=latest&python=3.11&gist=6d8ed4a4e55e0d6b2712eff23cd3e3b0
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
patrick91 commentedon Feb 12, 2023
I don't know if this helps, but while trying to make a test for this I noticed that adding
[builtins fixtures/tuple.pyi]
makes the problem disappear. here's the test for reference:A5rocks commentedon Feb 13, 2023
For test cases, put the fixtures at the end (that may be why, though it may be a difference in typeshed vs fixtures in which case we should fix that)
patrick91 commentedon Feb 13, 2023
@A5rocks moving it at the end doesn't seem to change anything :)
I can send a PR with the test, if that helps 😊
Also for now I changed my code to look like this:
which "fixed" my original problem :)
fjarri commentedon Feb 19, 2023
Unfortunately it does not fix things if the signature with
Awaitable
is in another library (currently hitting this problem withtrio
'sNursery.start_soon()
which expects something returning anAwaitable
). This worked inmypy==0.991
.A5rocks commentedon Feb 25, 2023
@patrick91 Sorry for the delay -- I've finally tried out the test... and it seemed to work?
I stuck this at the bottom of my
test-data/unit/check-type-aliases.test
:And then I ran
pytest -k testTestTestTest
.pytest
output:Do you have specifics on what you're doing? (A link to a branch would work, or a PR as you suggested)
A5rocks commentedon Feb 25, 2023
My best guess is this is another impact of the type alias typevar change (the one which allowed paramspecs as type aliases, #14159). In which case, this is probably just a small missed detail. This hunch is based on the fact mypy seems to be inferring the typevar out and that it worked in 0.991 -- IDK any other disruptive change that impacted type aliases during that time but I may be completely misremembering :Pcc @ilevkivskyi since you made that change (in case you want to correct me or take a closer look at this -- I haven't done so yet! All the above is simply conjecture)I should have verified before sending this -- this is present in the commit before that!
I'll bisect this.I can't reproduce this working in mypy 0.991. This breakage doesn't seem recent.
fjarri commentedon May 11, 2023
Sorry, just noticed your edit.
This commit: fjarri/nucypher-async@25718f2 passes with
mypy 0.991
(ok, there is one fail, but it is unrelated to this issue), but reports a number of problems withAwaitable
withmypy 1.3.0
:(
Nursery
is a type from thetrio
package)patrick91 commentedon Jun 15, 2023
@A5rocks sorry for the radio silence, I just tested this again and it seems to work with the latest version of mypy 😊
my final type looks like this:
A5rocks commentedon Jun 15, 2023
Hmm, wacky. I meant to check the repository but completely forgot to. It's good that it's fixed now, nonetheless!
EDIT: I forgot that your original example still fails and has done so since... a while. That should still be fixed, I suppose.
patrick91 commentedon Jun 15, 2023
Yes, I needed to keep the
Callable[..., Coroutine[T, Any, Any]],
but also add the awaitable one after removing some parts of our plugin here: https://github.com/strawberry-graphql/strawberry/pull/2852/files#diff-00af9d43c9b59404ba170e72e470939f2a6dd50e2eae24d0e24ef105431e5414L46-L48I'll leave this issue open then 😊