-
-
Save BoniLindsley/39254532172b74277697f9b73a53eace to your computer and use it in GitHub Desktop.
A potential `mypy` bug in `asynccontextmanager`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import contextlib, typing | |
_T = typing.TypeVar('_T') | |
@contextlib.asynccontextmanager | |
async def identity(element: _T) -> typing.AsyncIterator[_T]: | |
""" | |
An context manager that does nothing | |
and returns the given element. | |
Type checking should assume the yielded value | |
has the same type as the argument. | |
""" | |
yield element | |
async def main() -> None: | |
""" | |
Here we give an ``int`` to ``identity``. | |
So the yielded ``number`` should be ``int`` as well. | |
""" | |
async with identity(1) as number: | |
"""Problem: We got ``_T`-1`` as its type instead of ``int``.""" | |
reveal_type(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment