Skip to content

False negative for restricted self in subclass #7861

Open
@ilevkivskyi

Description

@ilevkivskyi

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).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions