python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.13k stars 2.76k forks source link

Allow Annotated to wrap Final and ClassVar #12061

Open GBeauregard opened 2 years ago

GBeauregard commented 2 years ago

Bug Report

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. mypy should follow suit in allowing the wrapping in order to allow the friction lifting to happen in practice.

To Reproduce

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.

Actual Behavior

antest/antest.py:4: error: Invalid type: ClassVar nested inside other type
antest/antest.py:5: error: Final can be only used as an outermost qualifier in a variable annotation
Found 2 errors in 1 file (checked 1 source file)

Your Environment

Additional Context Corresponding issue for dataclasses (incl InitVar) is at https://bugs.python.org/issue46511 pyright changed at https://github.com/microsoft/pyright/commit/8ce7fa6e0ad883fb17407add955273bbfbe506c5 pytype issue at https://github.com/google/pytype/issues/1110 pyre issue at https://github.com/facebook/pyre-check/issues/577

hmc-cs-mdrissi commented 2 years ago

I'd like to take this task. I'm pretty happy my comment led to this change so I'm happy to cover the pr. I'll try to do it this weekend.

JWCS commented 5 months ago

@hmc-cs-mdrissi Is this resolved now with #116069?

hmc-cs-mdrissi commented 5 months ago

No. That is related (but little different) python typing runtime change, while this issue is for type checker change. I'd forgotten about this mypy issue.