Skip to content

Self type doesn't work on attributes #16547

Closed
@DetachHead

Description

@DetachHead
from typing import Self, ClassVar

class Foo:
    instance: ClassVar[Self]
    
    @classmethod
    def get_instance(cls) -> Self:
        return cls.instance # error: Incompatible return value type (got "Foo", expected "Self")  [return-value]
    
    @classmethod
    def get_instance2(cls) -> Self:
        return cls.get_instance() # no error

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-self-typesTypes for self

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions