The documentation (typing, PEP 526, PEP 591, PEP 593) is silent on how Annotated and ClassVar/Final should be nested.
Intuitively, Annotated should appear on the outside, as the annotation applies to the whole ClassVar/Final variable, and as it's easiest to strip from that point.
In fact, this is not accepted by the Python runtime:
fromtypingimport (
Annotated,
ClassVar,
)
classFoo:
a: Annotated[ClassVar[int], 'hello'] =42# raises: TypeError: typing.ClassVar[int] is not valid as# type argumentb: ClassVar[Annotated[int, 'hello']] =42# accepted by the Python runtime
My intuition says that this behaviour is undesirable.
Even if this behaviour is correct, in my humble opinion, the documentation should be amended to speak to how Annotated and ClassVar/Final interact.
The text was updated successfully, but these errors were encountered:
As @Gobot1234 said the runtime restriction will be relaxed in 3.11. My general goal is to keep the runtime permissive, so it's easy for type checkers and users to experiment with new forms of type annotations.
That said, it's not unreasonable for type checkers to reject this form. Annotated is meant to be used on types, and ClassVar/Final are type modifiers, not types themselves. So you could make the argument that Annotated should not be used outside ClassVar/Final. I'm fine with leaving that up to individual type checker authors for now.
finite-state-machine commentedJun 18, 2022
The documentation (
typing, PEP 526, PEP 591, PEP 593) is silent on howAnnotatedandClassVar/Finalshould be nested.Intuitively,
Annotatedshould appear on the outside, as the annotation applies to the wholeClassVar/Finalvariable, and as it's easiest to strip from that point.In fact, this is not accepted by the Python runtime:
My intuition says that this behaviour is undesirable.
Even if this behaviour is correct, in my humble opinion, the documentation should be amended to speak to how
AnnotatedandClassVar/Finalinteract.The text was updated successfully, but these errors were encountered: