Incorrect "redundant cast" for linked TypeVar T
and type[T]
variables
#17045
Labels
bug
mypy got something wrong
T
and type[T]
variables
#17045
Bug Report
Given a TypeVar
T
with multiple, concrete constraints, if you write a generic function that takes an argument of typeT
, another argument of typetype[T]
, and return a value of typeT
, mypy does not seem to have consistent behavior when it comes to handling the return type of individual expressions. Attempting to return a value without casting hits anIncompatible return value type
error, while attempting to cast causes aRedundant cast
error.To Reproduce
https://mypy-play.net/?mypy=1.9.0&python=3.12&flags=strict&gist=4a0bc37034dcbcd5a18fabebeb5cbff5
Expected Behavior
I expected either one of
foo()
orfoo2()
to be accepted by mypy. I don't necessarily expect mypy to be smart enough to infer the the linkage between narrowing the type ofx_type
and relating it to the type ofx
, but I at least expect to be able to explicitly cast the return expression without hitting aRedundant cast
error.Actual Behavior
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: