Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Annotated to wrap Final #1110

Closed
GBeauregard opened this issue Jan 25, 2022 · 1 comment
Closed

Allow Annotated to wrap Final #1110

GBeauregard opened this issue Jan 25, 2022 · 1 comment

Comments

@GBeauregard
Copy link

GBeauregard commented Jan 25, 2022

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

rchen152 pushed a commit that referenced this issue Jan 26, 2022
Also tweaks a couple of initializers to let __repr__ work during construction.

Fixes #1110

PiperOrigin-RevId: 424194421
@martindemello
Copy link
Contributor

done, thanks for notifying us!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants