Just ran into this problem with Awaitable (which suffers from the same problem as Coroutine ). It is missing from stdlib, and there seems to be no easy way to just pull it out of pypi. If you're associated with Python 3.5, this workaround might work for you:
We rely on the fact that while Python 3.5 stdlib typing does not contain Coroutine (or Awaitable ), mypy is not actually used to use stdlib typing . Instead, when invoked, it uses its own version of the typing module. So, while your mypy updated, it will know about typing.Coroutine (and typing.Awaitable ). Therefore, the only thing you need to do is fake the existence of these types for the runtime (where you cannot import them). This can be done like this:
from typing import Any, TYPE_CHECKING try: from typing import Coroutine except ImportError: class _Coroutine:
After that, use Coroutine[A, B, C] as usual. Your code will look right, and at runtime you will not have problems due to the lack of 3.5 stdlib.
This does not allow you to do any RTTI, but AFAIK, that part of the PEP that landed experimentally at 3.6 (or possibly 3.7) anyway.
For Awaitable this is the same workaround except for s/Coroutine/Awaitable .
source share