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
If you have a function that uses a TypeVar to fix the type of multiple parameters, and one of those has a default value that restricts the possible types, MyPy doesn't raise an error if a caller uses the default parameter but has an invalid type in the other.
MyPy should report an error for the second call to sum_values as str is not a valid type for T when the default map restricts it to int.
Actual Behavior
No error reported by MyPy, but a runtime error in Python.
Traceback (most recent call last):
File "/Users/a.wilkinson1/Downloads/mypy_map_func_error.py", line 13, in <module>
sum_values(["abc", "defg"])
File "/Users/a.wilkinson1/Downloads/mypy_map_func_error.py", line 9, in sum_values
return sum([map_func(x) for x in values])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Your Environment
Mypy version used: 1.6.1
Python version used: 3.11 and 3.12
The text was updated successfully, but these errors were encountered:
I should add, if you include the default parameter when calling the function you get the correct error from MyPy.
sum_values(["abc", "defg"], default_map)
mypy_map_func_error.py:14: error: Argument 2 to "sum_values" has incompatible type "Callable[[int], int]"; expected "Callable[[str], int]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
You should remove that bound=Any on the type variable (Any is not the same as object). Having such type variable is almost the same as having plain Any. Mypy will then give an error, likely not in a place where you want, but then it is #3737
Fair point about bound, that's definitely my mistake.
Without that you get the error error: Incompatible default for argument "map_func" (default has type "Callable[[int], int]", argument has type "Callable[[T], int]") [assignment]. I'd argue that this is incorrect, as the default value is compatible. It's more specific than the type of the argument, but it's definitely compatible. I'm not sure how I'd create a default value that was compatible, given the typevar in the type.
Bug Report
If you have a function that uses a TypeVar to fix the type of multiple parameters, and one of those has a default value that restricts the possible types, MyPy doesn't raise an error if a caller uses the default parameter but has an invalid type in the other.
To Reproduce
Playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=6733a6d4078776cb4c54bfca985c3dc5
Expected Behavior
MyPy should report an error for the second call to
sum_values
asstr
is not a valid type forT
when the default map restricts it toint
.Actual Behavior
No error reported by MyPy, but a runtime error in Python.
Your Environment
The text was updated successfully, but these errors were encountered: