[New-bugs-announce] [issue45384] Accept Final as indicating ClassVar for dataclass

Gregory Beauregard report at bugs.python.org
Tue Oct 5 19:00:02 EDT 2021


New submission from Gregory Beauregard <greg at greg.red>:

PEP 591 for the Final Attribute states "Type checkers should infer a final attribute that is initialized in a class body as being a class variable. Variables should not be annotated with both ClassVar and Final."

This is a bit of a typing conflict for dataclasses, where ClassVar is used to indicate a desired library behavior, but one may want to indicate Final.

I propose accepting the Final attribute as an indicator of a ClassVar in dataclasses class bodies in order to be better compatible with the Final PEP.

There is at least one edge case that would need to be handled where someone might want to explicitly mark a dataclass field Final, which could be allowed as a field:
a: Final[int] = dataclasses.field(init=False, default=10)

----------
components: Library (Lib)
messages: 403277
nosy: GBeauregard
priority: normal
severity: normal
status: open
title: Accept Final as indicating ClassVar for dataclass
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45384>
_______________________________________


More information about the New-bugs-announce mailing list