You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't use Callable[..., T] | Callable[..., Awaitable[T]] as a function argument #14669
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:
Unfortunately it does not fix things if the signature with Awaitable is in another library (currently hitting this problem with trio's Nursery.start_soon() which expects something returning an Awaitable). This worked in mypy==0.991.
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 :P
cc @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.
This worked in mypy==0.991.
I can't reproduce this working in mypy 0.991. This breakage doesn't seem recent.
I can't reproduce this working in mypy 0.991. This breakage doesn't seem recent.
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 with Awaitable with mypy 1.3.0:
nucypher_async/utils/__init__.py:30: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Event], Coroutine[Any, Any, None]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/mocks/asgi.py:28: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Union[HTTPScope, WebsocketScope, LifespanScope], Callable[[], Awaitable[Union[HTTPRequestEvent, HTTPDisconnectEvent, WebsocketConnectEvent, WebsocketReceiveEvent, WebsocketDisconnectEvent, LifespanStartupEvent, LifespanShutdownEvent]]], Callable[[Union[HTTPResponseStartEvent, HTTPResponseBodyEvent, HTTPServerPushEvent, HTTPEarlyHintEvent, HTTPDisconnectEvent, WebsocketAcceptEvent, WebsocketSendEvent, WebsocketResponseStartEvent, WebsocketResponseBodyEvent, WebsocketCloseEvent, LifespanStartupCompleteEvent, LifespanStartupFailedEvent, LifespanShutdownCompleteEvent, LifespanShutdownFailedEvent]], Awaitable[None]]], Awaitable[None]]"; expected "Callable[[__T1, __T2, __T3], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/learner.py:253: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Contact], Coroutine[Any, Any, None]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/learner.py:255: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Contact], Coroutine[Any, Any, Optional[VerifiedUrsulaInfo]]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/learner.py:264: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[VerifiedUrsulaInfo], Coroutine[Any, Any, None]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/algorithms.py:132: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Contact], Coroutine[Any, Any, Optional[VerifiedUrsulaInfo]]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/algorithms.py:144: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Contact], Coroutine[Any, Any, Optional[VerifiedUrsulaInfo]]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/p2p/algorithms.py:218: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Contact], Coroutine[Any, Any, None]]"; expected "Callable[[__T1], Awaitable[Any]]" [arg-type]
nucypher_async/client/pre.py:154: error: Argument 1 to "start_soon" of "Nursery" has incompatible type "Callable[[Nursery, VerifiedUrsulaInfo], Coroutine[Any, Any, None]]"; expected "Callable[[__T1, __T2], Awaitable[Any]]" [arg-type]
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
Awaitableis 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.pytestoutput: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 withAwaitablewithmypy 1.3.0:(
Nurseryis a type from thetriopackage)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 😊