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

False negative for restricted self in subclass #7861

Open
ilevkivskyi opened this issue Nov 3, 2019 · 1 comment
Open

False negative for restricted self in subclass #7861

ilevkivskyi opened this issue Nov 3, 2019 · 1 comment
Labels
bug mypy got something wrong priority-1-normal topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self

Comments

@ilevkivskyi
Copy link
Member

This currently passes, but the code is unsafe:

from typing import TypeVar, Generic

T = TypeVar('T')

class Base(Generic[T]):
    def __init__(self, item: T) -> None:
        self.item = item
    def foo(self) -> None:
        pass

class Sub(Base[T]):
    def foo(self: Sub[str]) -> None:
        self.item + 'no'

# This is why the above is unsafe.
a: Base[int] = Sub(0)
a.foo()

On one hand it is a very rare situation, on the other hand in view of making explicit self-types a more "official" feature (see #7860) we might want to fix this.

Also it would be great to fix this in a consistent way, so that the fix also works for same issue with multiple inheritance without code duplication (see #7724).

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Nov 3, 2019
@ilevkivskyi
Copy link
Member Author

Btw I just discovered a TODO-like comment in bind_self():

        # Type check method override

Probably it is about this issue.

@AlexWaygood AlexWaygood added topic-self-types Types for self topic-inheritance Inheritance and incompatible overrides labels Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-inheritance Inheritance and incompatible overrides topic-self-types Types for self
Projects
None yet
Development

No branches or pull requests

2 participants