You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TristanSpeakEasy
changed the title
Order a Final annotation changes behaviour of field
Order of Final annotation changes behaviour of field
Jul 16, 2024
So actually I went too fast when reading this. Currently, Pydantic considers final fields with a default value to be class variables. This is why in TestModel2, the field value is actually not present in the model dump (because class variables aren't serialized). This decision predates the typing specification update that specified the meaning of Final in dataclasses and dataclass-like models (see #11119).
The logic to determine if a field is final was incomplete, and that's why TestModel1 did not suffer from this issue. In 2.11, both cases will be considered as class variables, but a deprecation warning will be emitted, stating that starting in V3, they will be considered as normal fields.
In the meanwhile, you can omit the Final qualifier, and specify frozen on the Field() function instead.
Uh oh!
There was an error while loading. Please reload this page.
Initial Checks
Description
For a class defined like so:
when dumping the model the
someConst
field will be present in the dumped dict.But Mypy complains that the
Final
annotation should be the last in the chain.error: Final can be only used as an outermost qualifier in a variable annotation [valid-type]
So when modifying it to this:
and dumping the model the
someConst
value is not longer present in the dumped dict.I would expect that change of order here to have no material impact on behaviour and would expect it to behave as the first example.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: