News for hackers. Stuff that matters.
Re:The Data Classes Decorator by eirki on Sunday December 12, 2021 @12:29PM (#29531529)
I just wish I could disable the type coercion. E.g. 1 becomes "1" if the attribute is annotated str.
Re:The Data Classes Decorator by nitred on Sunday December 12, 2021 @01:04PM (#29531894)
My recommendation is to get into the habit using StrictStr, StrictInt and StrictFloat as your choice of types to start off with and use str, int, float etc only when coercion is acceptable.

Coercion is quite powerful and saves me quite a bit of work. I write validation for config files that non programmers create. I would happily avoid explaining the difference between a 1 and a "1" to them whenever I can and let coercion do the work. It reduces the mental load on them too.

Re:The Data Classes Decorator by kkirsche on Sunday December 12, 2021 @12:54PM (#29531794)
Isn’t that the allow_mutation parameter of the field function?

https://pydantic-docs.helpmanual.io/usage/schema/#field-cust...

Re:The Data Classes Decorator by eirki on Sunday December 12, 2021 @03:22PM (#29533233)
Disabling mutation prevents you from changing attributes after instantiation, no?