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
4: error: Argument "key" to "max" has incompatible type "Callable[[Sized], int]"; expected "Callable[[Optional[str]], Any]"
While this looks similar to #6460, the root cause is not the same: mypy 0.701 contains the updated typeshed with a signature that uses a Union for the return type of max. Also the test case from that issue passes now. The difference with my test case seems to be in the expected return type.
To make absolutely certain that the built-in signature is not the problem, I created this test case that uses a custom mox function with the same signature as max:
12: error: Argument "key" to "mox" has incompatible type "Callable[[Sized], int]"; expected "Callable[[Optional[str]], Any]"
15: error: Argument "key" to "mox" has incompatible type "Callable[[Sized], int]"; expected "Callable[[object], Any]"
So the expected value type seems to be influenced by the expected return type of the expression, instead of being determined solely by the argument types.
I guess there could be situations in which picking a weaker value type is useful, but in this case the weaker type causes a false positive.
The text was updated successfully, but these errors were encountered:
So the expected value type seems to be influenced by the expected return type of the expression, instead of being determined solely by the argument types.
Yes, this is how mypy is designed to work. Empirically, this approach leads to fewer false positives and better error messages.
I guess there could be situations in which picking a weaker value type is useful, but in this case the weaker type causes a false positive.
Yes, contravariant generics (like callables) is a known exception. Essentially, this is a duplicate of #6482 which is an (important) special case of #5874.
I'm using mypy 0.701 on Python 3.7.2, no flags.
When run on the following code:
mypy reports:
While this looks similar to #6460, the root cause is not the same: mypy 0.701 contains the updated typeshed with a signature that uses a
Union
for the return type ofmax
. Also the test case from that issue passes now. The difference with my test case seems to be in the expected return type.To make absolutely certain that the built-in signature is not the problem, I created this test case that uses a custom
mox
function with the same signature asmax
:mypy reports:
So the expected value type seems to be influenced by the expected return type of the expression, instead of being determined solely by the argument types.
I guess there could be situations in which picking a weaker value type is useful, but in this case the weaker type causes a false positive.
The text was updated successfully, but these errors were encountered: