-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
"override" check causes false positive error #12390
Comments
Here is your issue minified: Which looks like correct analysis to me. Why not annotate |
Thanks @KotlinIsland, I missed that tiny " If so, then the only thing I'd propose mypy do differently here would be to improve the error message to point out the actually-problematic part(s) of the difference, e.g. main.py:23: error: Signature of "pop" incompatible with supertype "MutableMapping" in second overload
main.py:23: note: Superclass:
main.py:23: note: @overload
main.py:23: note: def pop(self, KT) -> VT
main.py:23: note: @overload
main.py:23: note: def [_T] pop(self, KT, Union[VT, _T] = ...) -> Union[VT, _T]
^^^^^
main.py:23: note: Subclass:
main.py:23: note: @overload
main.py:23: note: def pop(self, KT) -> VT
main.py:23: note: @overload
main.py:23: note: def [DT] pop(self, KT, Union[VT, DT]) -> Union[VT, DT]
Found 1 error in 1 file (checked 1 source file) Note the addition of "in second overload" to the error message, as well as the all-important |
Oh, perhaps the most problematic thing here is that the mypy error message only mentions incompatibility between the subclass and superclass. But there is no incompatibility in the actual implementation: The version of And given that, I'm not sure I'd call this a false negative bug in Pyright, as much as neglecting to enforce pedantic enough overloads. Thoughts? ❯ cat foo.py
import typing as t
T = t.TypeVar("T")
class A(t.Generic[T]):
@t.overload
def pop(self) -> T: ...
@t.overload
def pop(self, d: T=...) -> T: ...
def pop(self, d: T=...) -> T: ...
class B(A[T]):
@t.overload
def pop(self) -> T: ...
@t.overload
def pop(self, d: T) -> T: ... # This...
def pop(self, d: T=...) -> T: ... # ...doesn't match this. This line makes the subclass compatible with the superclass, but mypy ignores this line?
b: A[int] = B()
b.pop() # NOT a runtime error
❯ mypy foo.py
fn.py:13: error: Signature of "pop" incompatible with supertype "A"
fn.py:13: note: Superclass:
fn.py:13: note: @overload
fn.py:13: note: def pop(self) -> T
fn.py:13: note: @overload
fn.py:13: note: def pop(self, d: T = ...) -> T
fn.py:13: note: Subclass:
fn.py:13: note: @overload
fn.py:13: note: def pop(self) -> T
fn.py:13: note: @overload
fn.py:13: note: def pop(self, d: T) -> T
Found 1 error in 1 file (checked 1 source file)
❯ pyright foo.py
Loading configuration file at /private/tmp/pyrightconfig.json
Assuming Python version 3.10
Assuming Python platform Darwin
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
stubPath /private/tmp/typings is not a valid directory.
Searching for source files
Found 1 source file
0 errors, 0 warnings, 0 informations
Completed in 0.589sec |
Annoyingly mypy isn't very strict regarding overload implementations #12401 |
Yeah, this was a missing check in Pyright. This will be addressed in the next release. |
Thanks @erictraut! (Anyone ever tell you your follow-up is amazing?:) On the mypy side, should this issue be repurposed for improving the error message? |
Probably better to raise a new one |
Since there's now been a new release of Pyright, I retried the reproducer in #12390 (comment) with latest Pyright, and it still emitted no errors. I didn't see a corresponding fix in https://github.com/microsoft/pyright/commits or issue in the tracker. Happy to create one if that'd be helpful, and hopefully I haven't missed something. (Before raising a new issue to improve mypy's output for this type of error, I wanted to see what Pyright's output would be like for this type of error first, in case Pyright comes up with any other improvements I didn't think of. In the meantime, I'll close this issue since this isn't actually a false positive error in mypy.) |
And another thing before I raise a followup mypy issue for this, can anyone respond to this question? It seems this error is more a case of "overloads don't add up to properly match function's runtime signature" (due to forgetting to give the kwarg a default value in the corresponding overload), more-so than the subclass's method matching the superclass's? |
@jab Mypy doesn't consider the implementation when comparing the types, the overloads are the only source of information: Which isn't the best in my opinion and leads to situation like this. |
Bug Report
mypy's "override" check is erroneously flagging the type hints for the
pop()
method in the following code:See https://mypy-play.net/?mypy=latest&python=3.10&gist=eb527d76a58888350b4038002816923f for a live demo.
Other type checkers, such as Pyright, flag no error here. But mypy fails with the following:
Comparing the superclass to the subclass, mypy apparently thinks that
is incompatible with
even though these should be compatible.
Your Environment
The text was updated successfully, but these errors were encountered: