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 using an isinstance check in a generic function/class, mypy infers the type as being the checked type, overriding the knowledge that it's also the generic type, leading to type errors even if the check is only relevant when T could include that type.
Here, returning the value in an isinstance(x, str) check (or outside the reverse check) will cause error: error: Incompatible return value type (got "str", expected "T")
Interestingly, replacing return in the not isinstance(x, str) check with pass causes it to typecheck with no issues, even though the exact same code path will be taken for values not of that type.
Expected Behavior
I wouldn't have expected any of the functions in the gist to raise type errors - it doesn't seem like an isinstance check should override the known type being T.
Actual Behavior
Mypy generates errors for two of the functions:
main.py:7: error: Incompatible return value type (got "str", expected "T") [return-value]
main.py:13: error: Incompatible return value type (got "str", expected "T") [return-value]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
Mypy version used: 1.3.0
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): empty
Python version used: 3.11
The text was updated successfully, but these errors were encountered:
Bug Report
When using an isinstance check in a generic function/class, mypy infers the type as being the checked type, overriding the knowledge that it's also the generic type, leading to type errors even if the check is only relevant when T could include that type.
To Reproduce
See https://mypy-play.net/?mypy=latest&python=3.11&gist=4eb527cbef6cebc403b3d6bb3fae3873
Here, returning the value in an
isinstance(x, str)
check (or outside the reverse check) will causeerror: error: Incompatible return value type (got "str", expected "T")
Interestingly, replacing return in the
not isinstance(x, str)
check withpass
causes it to typecheck with no issues, even though the exact same code path will be taken for values not of that type.Expected Behavior
I wouldn't have expected any of the functions in the gist to raise type errors - it doesn't seem like an isinstance check should override the known type being T.
Actual Behavior
Mypy generates errors for two of the functions:
main.py:7: error: Incompatible return value type (got "str", expected "T") [return-value]
main.py:13: error: Incompatible return value type (got "str", expected "T") [return-value]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
mypy.ini
(and other config files): emptyThe text was updated successfully, but these errors were encountered: