mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Allow adjacent conditionally-defined overloads

Open sterliakov opened this issue 8 months ago • 6 comments

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".

sterliakov avatar May 06 '25 13:05 sterliakov

cc @cdce8p as original author - could you have a look, please?

sterliakov avatar May 06 '25 14:05 sterliakov

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]

github-actions[bot] avatar May 06 '25 14:05 github-actions[bot]

Okay, we've even had one such false positive in primer! https://github.com/enthought/comtypes/blob/430bbdca8c0a146139acb990146ecbe06bdbf803/comtypes/_post_coinit/misc.py#L143

sterliakov avatar May 06 '25 14:05 sterliakov

This looks like it should fix #17521 as well, but I'm not able to check this right now.

bzoracler avatar Jun 06 '25 23:06 bzoracler

It does, thanks! I expected this to be too niche to run a mypy-issues scan, but that was a wrong estimate:(

sterliakov avatar Jun 07 '25 00:06 sterliakov

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]

github-actions[bot] avatar Jun 07 '25 01:06 github-actions[bot]

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]

github-actions[bot] avatar Jun 21 '25 22:06 github-actions[bot]