-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
Return type of contextlib.asynccontextmanager does not substitute typing.TypeVar determined from parameters.
To Reproduce
Pass the following script to mypy, using mypy --config-file= main.py or otherwise.
#!/usr/bin/env python3
import contextlib, typing
_T = typing.TypeVar('_T')
@contextlib.asynccontextmanager
async def identity(element: _T) -> typing.AsyncIterator[_T]:
"""
A context manager that does nothing
and returns the given element.
Type checking should assume the yielded value
has the same type as the parameter.
"""
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)
number = 2Expected Behavior
The type of number should be int. In particular, assignment of the integer 2 to it should be compatible, and so mypy should return without errors.
Actual Behavior
The variable number is assigned, what I assume, is the TypeVar. This leads to an incompatible assignment of 2.
main.py:26: note: Revealed type is '_T`-1'
main.py:27: error: Incompatible types in assignment (expression has type "int", variable has type "_T")
Found 1 error in 1 file (checked 1 source file)
As a side note, a non-async counterpart of this behaves as expected, and does not produce any errors.
Your Environment
- Mypy version used: 0.790
- Mypy command-line flags:
--config-file= - Mypy configuration options from
mypy.ini(and other config files): Not applicable. - Python version used: Python 3.9.1
- Operating system and version:
Debian bullseye/sidandWindows 10 version 1909 build 18363.1256
li-wjohnson and xelhark
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong