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

Incorrect incompatible override report with self type + overload + TypeVar with constraints #14866

Closed
JelleZijlstra opened this issue Mar 10, 2023 · 0 comments · Fixed by #14882
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self

Comments

@JelleZijlstra
Copy link
Member

Similar to #14002; came up in python/typeshed#9084.

from typing import TypeVar, Generic, overload

T = TypeVar("T", str, int)

class Base(Generic[T]):
    @overload
    def method(self, s: T) -> T: ...
    @overload
    def method(self: Base[str], s: bytes) -> str: ...
    def method(self, s: object): ...

class Child(Base[int]):
    def method(self, s: int) -> int:
        raise NotImplementedError

(https://mypy-play.net/?mypy=latest&python=3.10&gist=da356a97851f7b7649d208e1359a6de4)

with mypy 1.1.1 produces:

/Users/jelle/bin/inerhit.py:13: error: Signature of "method" incompatible with supertype "Base"  [override]
/Users/jelle/bin/inerhit.py:13: note:      Superclass:
/Users/jelle/bin/inerhit.py:13: note:          @overload
/Users/jelle/bin/inerhit.py:13: note:          def method(self, s: int) -> int
/Users/jelle/bin/inerhit.py:13: note:          @overload
/Users/jelle/bin/inerhit.py:13: note:          def method(self, s: bytes) -> str
/Users/jelle/bin/inerhit.py:13: note:      Subclass:
/Users/jelle/bin/inerhit.py:13: note:          def method(self, s: int) -> int
Found 1 error in 1 file (checked 1 source file)

There should be no errors. The second overload is not applicable to a Base[int], so it should be fine that the subclass lacks it.

@JelleZijlstra JelleZijlstra added bug mypy got something wrong topic-type-variables topic-overloads topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self and removed topic-type-variables topic-overloads labels Mar 10, 2023
@AlexWaygood AlexWaygood added the affects-typeshed Anything that blocks a typeshed change label Mar 10, 2023
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Mar 11, 2023
Fixes python#14866

Basically does the potential todo I'd mentioned in python#14017
JelleZijlstra pushed a commit that referenced this issue Mar 11, 2023
…4882)

Fixes #14866

Basically does the potential todo I'd mentioned in #14017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants