Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Accept nesting Final in ClassVar for dataclasses #18235

Closed
@ncanceill

Description

@ncanceill

Bug Report

[Python 3.13]mypy does not accept nestingClassVar andFinal in class body

Theofficial docs of thetyping module say:

Changed in version 3.13:ClassVar can now be nested inFinal and vice versa.

Thetyping specs on qualifiers are more specific:

Final may be wrapped only by other type qualifiers (e.g.ClassVar or
Annotation).

In particular, nestingClassVar andFinal is a special case for dataclasses:

Type checkers should infer a final attribute that is initialized in a class
body as being a class variable, except in the case ofdataclasses, where
x: Final[int] = 3 creates a dataclass field and instance-level final
attributex with default value3;x: ClassVar[Final[int]] = 3 is
necessary to create a final class variable with value3. In
non-dataclasses, combiningClassVar andFinal is redundant, and type
checkers may choose to warn or error on the redundancy.

To Reproduce

In both classes below thefoo statement is not accepted bymypy:

fromdataclassesimportdataclassfromtypingimportClassVar,Final@dataclassclassBar:foo:ClassVar[Final[int]]=0@dataclassclassBaz:foo:Final[ClassVar[int]]=0

Note that the docs only mentionClassVar[Final[int]] and notFinal[ClassVar[int]]. In fact, the latter is not considered a class variable by the currentdataclasses implementation:

>>>Bar(foo=1)# foo is a class variableTraceback (mostrecentcalllast):File"<python-input-2>",line1,in<module>Bar(foo=1)~~~^^^^^^^TypeError:Bar.__init__()gotanunexpectedkeywordargument'foo'>>>Baz(foo=1)# foo is an instance variableBaz(foo=1)

Expected Behavior

[Python 3.13]mypy should acceptClassVar[Final[<type>]] in the body of a dataclass

The formClassVar[Final[int]] conforms with the official typing specs and behaves as expected. I think it should be accepted as part of Python 3.13 support.

The typing specs allow for a warning or error whenClassVar andFinal are nested outside of a dataclass body. I think this should result in an error since it is redundant, perhaps only in strict mode.

For the formFinal[ClassVar[int]] I am not sure. It could either result in an error or simply be parsed as a final instance variable.

Actual Behavior

The formClassVar[Final[int]] results in:
error: Final can be only used as an outermost qualifier in a variable annotation [valid-type]

The formFinal[ClassVar[int]] results in:
error: Variable should not be annotated with both ClassVar and Final [misc]

The behavior is the same with or without the@dataclass decorator.

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags:
  • Mypy configuration options frommypy.ini (and other config files):strict = true
  • Python version used: 3.13.0

Notes

The typing specs were changed inpython/typing#1669

Related discussion is found inpython/cpython#89547

The difference betweenClassVar[Final[int]] andFinal[ClassVar[int]] was noticed inmicrosoft/pyright#8676 (comment)

This issue is about Python 3.13 so could be added to#17264

This issue is related to#12061

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp