Closed as not planned
Description
Bug Report
When a value whose type is a TypeVar is tested via isinstance
, mypy so aggressively narrows types that it refuses to let them be broadened back to the original TypeVar, even when the value is untouched.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.10&gist=ac798d8bf4d5a39d1446d86bd5c30cf3
from typing import TypeVar
T = TypeVar('T')
def foo(t: T) -> T:
if isinstance(t, str):
return t + "."
elif isinstance(t, dict):
return t
elif isinstance(t, list):
return t
else:
return t
Expected Behavior
no type errors
Actual Behavior
main.py:8: error: Incompatible return value type (got "str", expected "T")
main.py:10: error: Incompatible return value type (got "Dict[Any, Any]", expected "T")
main.py:12: error: Incompatible return value type (got "List[Any]", expected "T")
Found 3 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 0.971 (reproduces in all versions I've tried in the playground)
- Mypy command-line flags: N/A
- Python version used: 3.7 (reproduces in all versions I've tried in the playground)