[Python-Dev] We should accept Final as indicating ClassVar for dataclasses

177 views
Skip to first unread message

Gregory Beauregard via Python-Dev

unread,
Oct 8, 2021, 11:11:51 PM10/8/21
to pytho...@python.org
(copied from typing on advice since this affects runtime behavior)
PEP 591 for Final 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."

ClassVar is used to indicate library behavior in dataclasses. Therefore, I propose accepting the Final attribute as an indicator of a ClassVar in dataclass class bodies in order to be better compatible with the Final PEP.

One edge case that would need to be handled is when someone might explicitly mark a dataclass field Final:
a: Final[int] = dataclasses.field(init=False, default=10)

I've opened an issue as well and would greatly appreciate any feedback: https://bugs.python.org/issue45384

I appreciate your time,
Gregory Beauregard
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/HS2UEMTQJYPHXOI6FPU523JMCCHHRNGH/
Code of Conduct: http://python.org/psf/codeofconduct/
Reply all
Reply to author
Forward
0 new messages