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.
Dataclasses can optionally be «frozen» / immutable IIRC.
Namedtuple is intended for when you need a tuple. I’ve used it for e.g numpy array shapes, so image data has names (imgshape.width instead of imgshape[1]). There, you need an actual tuple (or subclass of tuple if you want).
NamedTuple works great for a lot of cases, but not always. For example, when dealing with attribute defaults for mutable collections.
Dataclasses have a `default_factory` that is used in these occasions.
I recently started using [Pyserde](https://pypi.org/project/pyserde/) for easily serializing dataclasses to MsgPack format. You can even serialize dataclasses that contain numpy arrays using [msgpack-numpy](https://pypi.org/project/msgpack-numpy/). This is a good option for mixed/nested data where you can’t easily just dump your data as a Parquet file.
I use these for almost all my Python Classes. In my imaginary Python 4, these are the default. This, along with python's Enum, make up the core of my projects.
[+] [-] nitred|4 years ago|reply
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.
[+] [-] 331c8c71|4 years ago|reply
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.
[+] [-] jayc7|4 years ago|reply
The Pydantic page itself provides some pretty nice notes on why it's awesome: https://pydantic-docs.helpmanual.io/#rationale
[+] [-] eirki|4 years ago|reply
[+] [-] Ciantic|4 years ago|reply
[+] [-] 331c8c71|4 years ago|reply
[+] [-] woadwarrior01|4 years ago|reply
[1]: https://github.com/python/cpython/blob/3.10/Lib/collections/...
[+] [-] BurningFrog|4 years ago|reply
[+] [-] pletnes|4 years ago|reply
Namedtuple is intended for when you need a tuple. I’ve used it for e.g numpy array shapes, so image data has names (imgshape.width instead of imgshape[1]). There, you need an actual tuple (or subclass of tuple if you want).
[+] [-] lcnmrn|4 years ago|reply
[+] [-] vsmhn|4 years ago|reply
[+] [-] JoBrad|4 years ago|reply
[+] [-] greymalik|4 years ago|reply
[+] [-] gjvc|4 years ago|reply
[+] [-] avidphantasm|4 years ago|reply
[+] [-] the__alchemist|4 years ago|reply
[+] [-] xnaht|4 years ago|reply
[deleted]
[+] [-] kzrdude|4 years ago|reply