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
{{ message }}
Repository navigation
Regression: overload signatures with default parameters no longer reported as overlapping #18581
Since 1.11.0 and up to current master mypy no longer reports overloads in the snippet below as overlapping. 1.10.1 detects the overlap correctly.
According to the recent typing spec update for overloads, this is a bug:
If two overloads can accept the same set of arguments, they are said to "partially overlap". If two overloads partially overlap, the return type of the former overload should be assignable to the return type of the latter overload. If this condition doesn't hold, it is indicative of a programming error and should be reported by type checkers.
This bisects to #17392 - this false negative was missed there. cc @ilevkivskyi
This is not a regression but a very conscious decision. For three reasons:
First, an "unsafe overlap" is not simply when two overload variants with distinct return types can be matched with the same actual call (otherwise we would need to prohibit a lot of overloads that are currently allowed and are actually useful). We trust that the implementation reflects the order of overloads, but we flag when it is easy to "fool" mypy into inferring a type that doesn't match the runtime one, the prime (although oversimplified) example is
classB: ...
classC(B): ...
@overloaddeffoo(x: C) ->int: ...
@overloaddeffoo(x: B) ->str: ...
x: B=C()
foo(x) # static `str`, runtime `int`
The example you show doesn't fit here, as it is very hard to fool mypy into falsely matching the second overload.
Should we flag it as error as well? Unlike your example (where there is an easy fix), this example is very annoying to type "properly", because arguments without a default can't follow an argument with default. And people were actually complaining about this.
And last but not least, when typical users see the "unsafe overlap" error, they think "WTH do you want from me?", as it is often not obvious how to fix the error. So we try to limit this error only to cases that are known to actually cause bugs in real code.
Activity
ilevkivskyi commentedon Feb 1, 2025
This is not a regression but a very conscious decision. For three reasons:
First, an "unsafe overlap" is not simply when two overload variants with distinct return types can be matched with the same actual call (otherwise we would need to prohibit a lot of overloads that are currently allowed and are actually useful). We trust that the implementation reflects the order of overloads, but we flag when it is easy to "fool" mypy into inferring a type that doesn't match the runtime one, the prime (although oversimplified) example is
The example you show doesn't fit here, as it is very hard to fool mypy into falsely matching the second overload.
Second, what if I consider a similar example:
Should we flag it as error as well? Unlike your example (where there is an easy fix), this example is very annoying to type "properly", because arguments without a default can't follow an argument with default. And people were actually complaining about this.
And last but not least, when typical users see the "unsafe overlap" error, they think "WTH do you want from me?", as it is often not obvious how to fix the error. So we try to limit this error only to cases that are known to actually cause bugs in real code.
sterliakov commentedon Feb 2, 2025
This is a duplicate of #6580 (well, same scenario, but reporting the opposite behavior) - do you feel like closing that one as well?
ilevkivskyi commentedon Feb 2, 2025
@sterliakov Yes, this one of those complains I mentioned above. I will close it as it is now fixed.