top | item 29495916

The Data Classes Decorator

59 points| nothrowaways | 4 years ago |docs.python.org | reply

32 comments

order
[+] nitred|4 years ago|reply
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.

[+] 331c8c71|4 years ago|reply
> 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.

[+] jayc7|4 years ago|reply
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

[+] eirki|4 years ago|reply
I just wish I could disable the type coercion. E.g. 1 becomes "1" if the attribute is annotated str.
[+] Ciantic|4 years ago|reply
Interesting to find this here, if someone else haven't followed Python3, there is also NamedTuple which for all purposes is like immutable record:

    from typing import NamedTuple
    class Coord(NamedTuple):
        x: int
        y: int
I would prefer immutable records over dataclasses on many occasions.
[+] 331c8c71|4 years ago|reply
Long gone are the days of "There should be one-- and preferably only one --obvious way to do it" from PEP 20 ("Python Zen") and it's a pity IMO.
[+] BurningFrog|4 years ago|reply
You can pass frozen=True to the @dataclass decoratot.
[+] pletnes|4 years ago|reply
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).

[+] vsmhn|4 years ago|reply
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.
[+] JoBrad|4 years ago|reply
If I recall correctly, NamedTuple doesn’t support calculated properties, but a Data class does (as the first example shows).
[+] greymalik|4 years ago|reply
Data classes were added as a core part of Python three and a half years ago and were available as an option before that. Why is this of interest now?
[+] gjvc|4 years ago|reply
Not everyone is up to date.
[+] the__alchemist|4 years ago|reply
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.
[+] xnaht|4 years ago|reply

[deleted]

[+] kzrdude|4 years ago|reply
It's not useless. Hack maybe (uses type annotations but they are not checked unless you add something besides vanilla python. I.e they can lie.)