To be more specific:
To solve questions like How do I type hint a method with the type of the enclosing class?
PEP 673 introduces typing.Self
. The PEP is a Draft, but it currently an experimental type in typing_extensions 4.0.0
I tried using this in python 3.8
@dataclasses.dataclass
class MenuItem:
url: str
title: str
description: str = ""
items: typing.List[typing_extensions.Self] = dataclasses.field(default_factory=list)
But it raises
TypeError: Plain typing_extensions.Self is not valid as type argument
I could just use the literal string "MenuItem" instead. But I was wondering why this doesn't work.