Allow adjacent conditionally-defined overloads
Fixes #19015. Fixes #17521. Side question: why do we do that extra work to show "is already defined" error instead of "implementation must come last" in the following setting?
@overload
def f2(x: A) -> A: ...
if True:
@overload
def f2(x: B) -> B: ...
def f2(x): ...
if True:
#@overload # both with and without this decorator
def f2(x): ... # E: Name "f2" already defined on line 17
We have to track it specially and it doesn't work when f2 is decorated, and I struggle to understand how "already defined" is better than an overload-specific message "implementation must come last".
cc @cdce8p as original author - could you have a look, please?
Diff from mypy_primer, showing the effect of this PR on open source code:
comtypes (https://github.com/enthought/comtypes)
- comtypes/_post_coinit/misc.py:145: error: An overloaded function outside a stub file must have an implementation [no-overload-impl]
- comtypes/_post_coinit/misc.py:161: error: Name "CoGetClassObject" already defined on line 145 [no-redef]
Okay, we've even had one such false positive in primer! https://github.com/enthought/comtypes/blob/430bbdca8c0a146139acb990146ecbe06bdbf803/comtypes/_post_coinit/misc.py#L143
This looks like it should fix #17521 as well, but I'm not able to check this right now.
It does, thanks! I expected this to be too niche to run a mypy-issues scan, but that was a wrong estimate:(
Diff from mypy_primer, showing the effect of this PR on open source code:
comtypes (https://github.com/enthought/comtypes)
- comtypes/_post_coinit/misc.py:145: error: An overloaded function outside a stub file must have an implementation [no-overload-impl]
- comtypes/_post_coinit/misc.py:161: error: Name "CoGetClassObject" already defined on line 145 [no-redef]
Diff from mypy_primer, showing the effect of this PR on open source code:
comtypes (https://github.com/enthought/comtypes)
- comtypes/_post_coinit/misc.py:146: error: An overloaded function outside a stub file must have an implementation [no-overload-impl]
- comtypes/_post_coinit/misc.py:162: error: Name "CoGetClassObject" already defined on line 146 [no-redef]