Skip to content

Self type doesn't work on attributes #16547

Closed

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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