Hacker News new | ask | show | jobs
by eirki 1651 days ago
I just wish I could disable the type coercion. E.g. 1 becomes "1" if the attribute is annotated str.
2 comments

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.

Isn’t that the allow_mutation parameter of the field function?

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

Disabling mutation prevents you from changing attributes after instantiation, no?