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

Weird error message for incompatible override when using a TypeVar for self #12786

Open
AlexWaygood opened this issue May 14, 2022 · 1 comment
Labels
bug mypy got something wrong topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self topic-usability

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented May 14, 2022

Bug Report

Minimal repro:

from typing import TypeVar

Self = TypeVar("Self")

class Flag:
    def __or__(self: Self, other: Self) -> Self: ...
    __ror__ = __or__

class IntFlag(int, Flag):
    def __or__(self: Self, other: int) -> Self: ...
    def __ror__(self: Self, other: int) -> Self: ...

mypy output:

main.py:11: error: Signature of "__ror__" incompatible with supertype "Flag"
main.py:11: note:      Superclass:
main.py:11: note:          def [Self] __or__(self, Self, Self) -> Self
main.py:11: note:      Subclass:
main.py:11: note:          def __ror__(self, int) -> IntFlag

Mypy playground link here: https://mypy-play.net/?mypy=latest&python=3.10&gist=26b60881cd02e608bb9e555db4c3f627

Expected Behavior

I'm not sure an error is even appropriate here: no incompatible override is reported for __or__.

Even if an error is appropriate here, however, the error message is weird and garbled. According to the error message, mypy seems to think that __ror__ in the superclass expects three arguments (it only expects two). It also seems to think the method in the subclass will return IntFlag (not necessarily true; the return type will be whatever self is, and self could be any subtype of IntFlag).

The function that creates this error message is here:

def signature_incompatible_with_supertype(

Mypy version used

  • 0.950.
  • This doesn't seem to be a recent regression: I can reproduce as far back as 0.920, which is the earliest mypy version that has the verbose error messages.
  • I've also reproduced the bug on mypy master.
@AlexWaygood AlexWaygood added bug mypy got something wrong topic-usability topic-inheritance Inheritance and incompatible overrides topic-error-reporting How we report errors topic-self-types Types for self labels May 14, 2022
@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jun 8, 2022

I think the root cause of the bug here is probably the fact that mypy erroneously believes __ror__ in the base class to be a callable instance variable, rather than a method:

from typing import TypeVar

Self = TypeVar("Self")

class Flag:
    def __or__(self: Self, other: Self) -> Self: ...
    __ror__ = __or__

reveal_type(Flag.__or__)  # Revealed type is "def [Self] (Self`-1, Self`-1) -> Self`-1"
reveal_type(Flag.__ror__)  # error: Access to generic instance variables via class is ambiguous  # Revealed type is "def [Self] (Any, Any) -> Any"

https://mypy-play.net/?mypy=latest&python=3.10&gist=daa1934028156c5298f443fc65c1afd4

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

No branches or pull requests

1 participant