Skip to content

Allow Annotated to wrap Final and ClassVar #12061

Open
@GBeauregard

Description

@GBeauregard

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

  • Mypy version used: 0.931
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.9
  • Operating system and version: Gentoo Linux stable

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-finalPEP 591

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions