serhii.net

In the middle of the desert you can say anything you want

11 Sep 2025

pydantic validation and fields and assignments

Lost cumulatively hours on these things this months.

MODEL_CONFIG = ConfigDict(
    serialize_by_alias=True,  # why doesn't this, alone, work?
)

Guess why? Because I have pydantic 2.10, the config above was introduced in 2.11, and it just quietly allows me to set this config value. (Configuration - Pydantic) (Ty ty for picking up on this)

Next. Configuration - Pydantic

ConfigDict(
    arbitrary_types_allowed=False,  # disallow obj.invalid_field = "whatever"
)

For my own models as well. Setting obj.name='test' when you want obj.step_name is almost never a good idea.

And again about serialize_by_alias: will be default in pydantic v3, which I welcome, because if you forget to model_dump(by_alias=True) then the model will be dumped with unexpected names, which will then be quietly deleted when you try to initialize a new model from that dict through e.g. NewModel(**old_model.model_dump()). (Should’ve validated anyway, but…)

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus