Saaket Prakash <saak...@gmail.com> added the comment:

Treating Final as ClassVar by default may be fine,
but it should not throw when using default_factory like ClassVar does.

There are valid uses of Final with instance variable when one would want the 
value to be unchanged after the `__init__` runs
but different instances can be initialized with different values that are 
generated by a default_factory.

A quick search on github for this pattern gives this
https://github.com/166MMX/hiro-python-library/blob/fb29e3247a8fe1b0f7dc4e68141cf7340a8dd0a5/src/arago/hiro/model/ws.py#L120
which will break if Final throws when using default_factory.

PEP 591 says:
Type checkers should infer a final attribute _that is initialized in a class 
body_ as being a class variable.
When using default_factory the attribute is not initialized inside the class 
body but when the instance is initialized.
So allowing instance level Final with default_factory will not be going against 
the PEP.

----------
nosy: +saaketp

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45384>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to