Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Closed
Description
Trying to get typing work in an asynchronous code and I don't understand mypy's behavior.
Let's say we have two coroutines typed in the following way:
fromtypingimportAsyncGeneratorasyncdefgen1()->AsyncGenerator[str,None]:passreveal_type(gen1)asyncdefgen2()->AsyncGenerator[str,None]:yield'tick'reveal_type(gen2)
mypy:
$ mypy file.pyfile.py:5: error: Revealedtype is'def () -> typing.Awaitable[typing.AsyncGenerator[builtins.str, builtins.None]]'file.py:9: error: Revealedtype is'def () -> typing.AsyncGenerator[builtins.str, builtins.None]'
So the type of the first one gets wrapped inAwaitable. I don't fully understand why. I thought allAsync... are Awaitables by definition. I did not find anything about it but here#3576, which is not enough for me to get it.
The same thing in case of methods:
classBaseClass:asyncdefgen1(self)->AsyncGenerator[str,None]:passreveal_type(BaseClass.gen1)classMyClass(BaseClass):asyncdefgen1(self)->AsyncGenerator[str,None]:yield'tick'reveal_type(MyClass.gen1)
mypy:
file.py:4: error: Revealedtype is'def (self: acm.workers.BaseClass) -> typing.Awaitable[typing.AsyncGenerator[builtins.str, builtins.None]]'file.py:7: error: Returntype of"gen1" incompatible with supertype"BaseClass"file.py:9: error: Revealedtype is'def (self: acm.workers.MyClass) -> typing.AsyncGenerator[builtins.str, builtins.None]'
So now I cannot define abstract methods that are async generators, in a straightforward way.
Versions
Python 3.6.5
mypy==0.600
EDIT: I fixed a random typo@JelleZijlstra found and edited parts of the text based on the typo. Now the whole problem is clearer.