Bug Report
Given a TypeVar T with multiple, concrete constraints, if you write a generic function that takes an argument of type T, another argument of type type[T], and return a value of type T, 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 an Incompatible return value type error, while attempting to cast causes a Redundant cast error.
To Reproduce
fromdecimalimportDecimalfromtypingimportTypeVar, castT=TypeVar("T", Decimal, float)
deffoo(x: T, x_type: type[T]) ->T:
ifx_type==float:
returncast("T", 1.0)
ifx_type==Decimal:
returncast("T", Decimal("2.0"))
raiseValueError("unreachable")
deffoo2(x: T, x_type: type[T]) ->T:
ifx_type==float:
return1.0ifx_type==Decimal:
returnDecimal("2.0")
raiseValueError("unreachable")https://mypy-play.net/?mypy=1.9.0&python=3.12&flags=strict&gist=4a0bc37034dcbcd5a18fabebeb5cbff5
Expected Behavior
I expected either one of foo() or foo2() to be accepted by mypy. I don't necessarily expect mypy to be smart enough to infer the the linkage between narrowing the type of x_type and relating it to the type of x, but I at least expect to be able to explicitly cast the return expression without hitting a Redundant cast error.
Actual Behavior
main.py:9: error: Redundant cast to "float" [redundant-cast]
main.py:11: error: Redundant cast to "Decimal" [redundant-cast]
main.py:17: error: Incompatible return value type (got "float", expected "Decimal") [return-value]
main.py:19: error: Incompatible return value type (got "Decimal", expected "float") [return-value]
Found 4 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.7.1 (but the mypy Playground I linked to above was 1.9.0)
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini (and other config files):
warn_unused_ignores = truewarn_redundant_casts = truewarn_unused_configs = truewarn_unreachable = truewarn_return_any = truestrict = truedisallow_untyped_decorators = truedisallow_any_generics = falseimplicit_reexport = falseshow_error_codes = true
- Python version used: 3.11.6 (mypy Playground was 3.12)
Bug Report
Given a TypeVar
Twith 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 typeerror, while attempting to cast causes aRedundant casterror.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_typeand relating it to the type ofx, but I at least expect to be able to explicitly cast the return expression without hitting aRedundant casterror.Actual Behavior
Your Environment
mypy.ini(and other config files):