You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)
The text was updated successfully, but these errors were encountered:
The second and third errors do look like bugs. The type of t after isinstance(t, dict) should be revealed as <subclass of "T" and "Dict[Any, Any]"> and it should be OK to return this as a value of type T.
The first one is more subtle. After isinstance(t, str) the type of t should be revealed as <subclass of "T" and "str">. But the string concatenation operator returns a str and T might actually be a subtype of str, so it's not necessarily OK to return that as a value of type T.
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
Expected Behavior
no type errors
Actual Behavior
Your Environment
The text was updated successfully, but these errors were encountered: