Skip to content

Instantly share code, notes, and snippets.

@BoniLindsley

BoniLindsley/forward.py Secret

Created Jan 17, 2021
Embed
What would you like to do?
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