Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

In my opinion Pydantic is Dataclass++ and NamedTuple+. I use it everywhere performance is not critical. It does type casting and provides an option for strict types. It lets you use custom types where you can define min and max size of a list or tuple etc. Every time I had a weird validation I needed to do, the Pydantic docs already had a built-in way to do it (E.g. conlist).

It has excellent support for JSON and JSON schemas out of the box.

Error messages are excellent! Tells you exactly what went wrong (most of the time) during validation.

I do use Pydantic even when Dataclass and NamedTuples are sufficient, I know that's a bit cargo cultish but it lowers the mental overhead by just having to perfect one of way of doing things.



> it lowers the mental overhead by just having to perfect one of way of doing things.

That's exactly the point. The mental burden of remembering the quirks of (and having to choose between) namedtuples, typeddicts, dataclasses etc is very real.


I'm a fan of Pydantic and also the FastAPI project which builds heavily on it. The OpenAPI integration is just... magical.

The Pydantic page itself provides some pretty nice notes on why it's awesome: https://pydantic-docs.helpmanual.io/#rationale


I just wish I could disable the type coercion. E.g. 1 becomes "1" if the attribute is annotated str.


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?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: