Description
Bug description
The behavior of Annotated
has been changed so that it is allowed at runtime to wrap ClassVar
and Final
. This change helps avoid friction with other users of annotations; see https://bugs.python.org/issue46491 for context. This change has landed in 3.11 and has been backported to 3.9, 3.10, and typing_extensions
. pytype should follow suit in allowing the wrapping in order to allow the friction lifting to happen in practice. The current implementation of Final
in pytype errors for this case.
Reproduction steps
from typing import Annotated, ClassVar, Final
class C:
classvar: Annotated[ClassVar[int], (2, 5)] = 4
const: Annotated[Final[int], "metadata"] = 4
Expected behavior
The above code should type check. Currently ClassVar
works, but Final
throws an error in the current main
code.
Logs
[1/1] check antest
FAILED: /home/gregory/Downloads/testcase/pytest/antest/.pytype/pyi/antest.pyi
/home/gregory/.venvs/pytype/bin/python -m pytype.single --imports_info /home/gregory/Downloads/testcase/pytest/antest/.pytype/imports/antest.imports --module-name antest -V 3.9 -o /home/gregory/Downloads/testcase/pytest/antest/.pytype/pyi/antest.pyi --analyze-annotated --nofail --quick /home/gregory/Downloads/testcase/pytest/antest/antest.py
File "/home/gregory/Downloads/testcase/pytest/antest/antest.py", line 21, in C: Invalid type annotation 'Annotated[Final[int], "metadata"]' [invalid-annotation]
Invalid use of typing.Final
Final may only be used as the outermost type in assignments or variable annotations.
File "/home/gregory/Downloads/testcase/pytest/antest/antest.py", line 21, in C: Invalid use of typing.Final [final-error]
Final may only be used as the outermost type in assignments or variable annotations.
Additional context
Corresponding issue for dataclasses (incl InitVar
) is at https://bugs.python.org/issue46511
pyright change: microsoft/pyright@8ce7fa6
mypy bug: python/mypy#12061
pyre bug: facebook/pyre-check#577