Skip to content

Instantly share code, notes, and snippets.

@BoniLindsley
Created January 17, 2021 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoniLindsley/39254532172b74277697f9b73a53eace to your computer and use it in GitHub Desktop.
Save BoniLindsley/39254532172b74277697f9b73a53eace to your computer and use it in GitHub Desktop.
A potential `mypy` bug in `asynccontextmanager`
#!/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