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

Improve "override" error output #12466

Open
jab opened this issue Mar 26, 2022 · 1 comment
Open

Improve "override" error output #12466

jab opened this issue Mar 26, 2022 · 1 comment
Labels
diagnostics topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides topic-usability

Comments

@jab
Copy link
Contributor

jab commented Mar 26, 2022

(Opening a new issue for this as discussed in #12390.)

Live demo: https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=d3e1a06857e4c6f1c698b7fa89e22f40

Example code:

import typing as t


class A:
    @t.overload
    def pop(self) -> int: ...
    @t.overload
    def pop(self, d: int = ...) -> int: ...
    
    def pop(self, d: int = ...) -> int: ...


class B(A):
    @t.overload
    def pop(self) -> int: ...
    @t.overload
    def pop(self, d: int) -> int: ...
    
    def pop(self, d: int = ...) -> int: ...

Current mypy output:

main.py:14: error: Signature of "pop" incompatible with supertype "A"
main.py:14: note:      Superclass:
main.py:14: note:          @overload
main.py:14: note:          def pop(self) -> int
main.py:14: note:          @overload
main.py:14: note:          def pop(self, d: int = ...) -> int
main.py:14: note:      Subclass:
main.py:14: note:          @overload
main.py:14: note:          def pop(self) -> int
main.py:14: note:          @overload
main.py:14: note:          def pop(self, d: int) -> int
Found 1 error in 1 file (checked 1 source file)

It's somewhat helpful that mypy quotes the code in this error output, but it would be really helpful if mypy's output helped us zero in on exactly where the problem is, e.g.:

main.py:14: error: Signature of "pop" incompatible with supertype "A" in second overload
main.py:14: note:      Superclass:
main.py:14: note:          @overload
main.py:14: note:          def pop(self) -> int
main.py:14: note:          @overload
main.py:14: note:          def pop(self, d: int = ...) -> int
                                                ^^^^^
main.py:14: note:      Subclass:
main.py:14: note:          @overload
main.py:14: note:          def pop(self) -> int
main.py:14: note:          @overload
main.py:14: note:          def pop(self, d: int) -> int
Found 1 error in 1 file (checked 1 source file)

Note the addition of "in second overload" in the error message, as well as the all-important ^ arrows pointing to exactly where the supertype differs (namely, d should be a keyword argument). Without the arrows, it's harder to see in the output that the only difference is the = ... in the code for the superclass.

This is exacerbated when method signatures are more complex than this, e.g. MutableMapping.pop(), where the current output makes it even harder to spot where the problem is compared to this minimized example (see #12390 (comment)). It is also exacerbated by mypy ignoring the signature in the actual implementation, where d correctly appears as a keyword argument in the subclass. Finally, it can be exacerbated when upgrading to a mypy version that includes typeshed changes like this one: python/typeshed@92438dc#diff-b12c4be0f8e39f89957bbe7be6b6327185e18b69ea80a0f62c0746bb26f4a58eL478

@JelleZijlstra JelleZijlstra added diagnostics topic-inheritance Inheritance and incompatible overrides labels Mar 26, 2022
@AlexWaygood AlexWaygood added the topic-error-reporting How we report errors label Mar 27, 2022
@hackaugusto
Copy link
Contributor

This would also be useful for statis overrides, something like:

class A:
    def f(self, a: int, b: float, c: str, d: dict, f: float = None) -> None:
        pass


class B(A):
    def f(self, a: int, b: float, c: str, d: dict, f: float) -> None:
        pass

Will report:

t.py:8: error: Signature of "f" incompatible with supertype "A"
t.py:8: note:      Superclass:
t.py:8: note:          def f(self, a: int, b: float, c: str, d: Dict[Any, Any], f: Optional[float] = ...) -> None
t.py:8: note:      Subclass:
t.py:8: note:          def f(self, a: int, b: float, c: str, d: Dict[Any, Any], f: float) -> None
Found 1 error in 1 file (checked 1 source file)

Pointing out where is the issue makes it easier to consume the error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostics topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides topic-usability
Projects
None yet
Development

No branches or pull requests

4 participants