Skip to content

generic is incorrectly inferred as invariant if class uses Self type on an attribute #18334

@DetachHead

Description

@DetachHead
from typing import Self


class Foo[T]:
    instance: Self # commenting out this line makes the error go away

    def foo(self) -> T: ... # type:ignore[empty-body]


class Bar(Foo[int]): ...

# Incompatible types in assignment (expression has type "Bar", variable has type "Foo[object]")  [assignment]
foo: Foo[object] = Bar()

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions