Skip to content

Self being a typing._SpecialForm prevents a case from PEP 673 #26

Closed
@CharString

Description

@CharString

Among the valid locations for Self in PEP 673 we find:

TupleSelf = Tuple[Self, Self]
class Alias:
    def return_tuple(self) -> TupleSelf:
        return (self, self)

But typing._type_check regards typing._SpecialForm instances invalid type arguments
https://github.com/python/cpython/blob/e0f8a3e9b96bbc2597f61be96993ef0c768a19fe/Lib/typing.py#L179

So this use will result in a

TypeError: Plain typing_extensions.Self is not valid as type argument

This also affects uses in linked lists and recursive dataclasses lke

@dataclasses.dataclass
class MenuItem:
    url: str
    title: str
    description: str = ""
    items: typing.List[typing_extensions.Self] = dataclasses.field(default_factory=list)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions