8000 Overriding an overloaded method that returns a value of a Union type · Issue #10799 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Overriding an overloaded method that returns a value of a Union type #10799

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

Closed
yoshum opened this issue Jul 10, 2021 · 4 comments
Closed

Overriding an overloaded method that returns a value of a Union type #10799

yoshum opened this issue Jul 10, 2021 · 4 comments
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-overloads

Comments

@yoshum
Copy link
yoshum commented Jul 10, 2021

Bug Report

Mypy raises an error when overriding an overloaded method that returns a value of a Union type, even though the original and overridden methods have the same signature.

To Reproduce

class Base:
    @overload
    def A(self, x: str) -> str:
        ...

    @overload
    def A(self, x: int) -> int:
        ...

    def A(self, x: Any) -> Union[str, int]:
        pass


class Derived(Base):
    def A(self, x: Any) -> Union[str, int]:  # Error here!
        pass

Expected Behavior

Because the implementations of Base.A and Derived.A have exactly the same signature indeed, I want this to pass the type checking.

Actual Behavior

An error Signature of "A" incompatible with supertype "Base" is raised at the definition of Derived.A.

Some additional observations are:

  • If I remove the decorated methods in the superclass, the error disappears
  • If I add to the subclass @overload-decorated methods as in the superclass, the error also disappears
  • If Base.A and Derived.A are implemented as static methods, adding @overload-decorated methods to the subclass does not resolve the error

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.8.6
  • Operating system and version: macOS
@yoshum yoshum added the bug mypy got something wrong label Jul 10, 2021
@erictraut
Copy link

I think this is expected behavior. Overloads are not inherited and must be replicated in the subclass. See #10699.

@yoshum
Copy link
Author
yoshum commented Jul 10, 2021

Thank you for giving the relevant information. I'll follow the issue and the discussion cited in it.

In the meantime, there is still an issue as I wrote in the third item of the "additional observations" above. To be specific, here is an example:

class Base:
    @staticmethod
    @overload
    def A(x: str) -> str:
        ...

    @staticmethod
    @overload
    def A(x: int) -> int:
        ...

    @staticmethod
    def A(x: Any) -> Union[str, int]:
        return x


class Derived(Base):
    @staticmethod
    @overload
    def A(x: str) -> str:
        ...

    @staticmethod
    @overload
    def A(x: int) -> int:
        ...

    @staticmethod
    def A(x: Any) -> Union[str, int]:  # Error here!
        return x

In fact, this is the original case I faced with. Is this expected as well?

@erictraut
Copy link

No, that's not expected. It looks like a bug. Appears to be a bad interaction between @overload and @staticmethod. This works fine in pyright, but mypy reports a spurious error in this case.

@AlexWaygood AlexWaygood added topic-overloads topic-inheritance Inheritance and incompatible overrides labels Mar 31, 2022
@erictraut
Copy link

The above bug appears to have been fixed. It no longer occurs with the latest version of mypy (1.5).

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-inheritance Inheritance and incompatible overrides topic-overloads
Projects
None yet
Development

No branches or pull requests

4 participants
0