Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: subclass method of overloaded function #15899

Closed
SubaruArai opened this issue Aug 18, 2023 · 6 comments
Closed

False positive: subclass method of overloaded function #15899

SubaruArai opened this issue Aug 18, 2023 · 6 comments
Labels
bug mypy got something wrong

Comments

@SubaruArai
Copy link

Bug Report

Mypy reports error when a subclass overwrites a overloaded method.

To Reproduce

Gist URL
Playground URL

Expected Behavior

This should be fine

Actual Behavior

dum.py:18: error: Signature of "method" incompatible with supertype "SuperClass"  [override]
dum.py:18: note:      Superclass:
dum.py:18: note:          @overload
dum.py:18: note:          def method(self, a: int) -> int
dum.py:18: note:          @overload
dum.py:18: note:          def method(self, a: str) -> str
dum.py:18: note:      Subclass:
dum.py:18: note:          def method(self, a: int | str) -> int | str

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11

(Probably) related issues:

@SubaruArai SubaruArai added the bug mypy got something wrong label Aug 18, 2023
@JelleZijlstra
Copy link
Member

This is not a false positive. With the superclass signature, you're guaranteed to get an int back if you pass an int. With the subclass signature, you may get a str back instead. Therefore, the subclass signature is incompatible and mypy is right to show an error.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2023
@SubaruArai
Copy link
Author

@JelleZijlstra Is this what happens internally? (At least philosophically)

  1. parent method gets annotated as overloaded
  2. child method created (parent method annotations are ignored - for if you want to change the type hints)
  3. mypy detects inconsistency between parent and child

@JelleZijlstra
Copy link
Member

Correct.

@SubaruArai
Copy link
Author

SubaruArai commented Aug 18, 2023

Ok, thanks!

@JelleZijlstra
Copy link
Member

#14725 is a false positive, because there the subclass signature is compatible.

@SubaruArai
Copy link
Author

Yeah, sorry misread it. Edited the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants