Skip to content

Generic Self classmethod fails for generic types #3631

Closed
@rowillia

Description

@rowillia

Repro:

from typing import Generic, TypeVar, Type

_T = TypeVar('_T')
SelfType = TypeVar('SelfType', bound='Friend')

class Friend(Generic[_T]):
    @classmethod
    def make(cls: Type[SelfType]) -> SelfType:  # E: Unsupported type Type["SelfType"]
        return cls()

I suspect the issue is that the type should be:

def make(cls: Type[SelfType[T]]) -> SelfType[T]:
        return cls()

But there is no way to express that that I can tell.

Using an instance method behaves as expected:

from typing import Generic, TypeVar, Type

_T = TypeVar('_T')
SelfType = TypeVar('SelfType', bound='Friend')

class Friend(Generic[_T]):
    other = None  # type: Friend

    def make(self: SelfType) -> SelfType:
        return self

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions