I use Attrs instead of Pydantic
threeofwands.com
I use Attrs instead of Pydantic
1–10 of 76 posts
Re: I use Attrs instead of Pydantic
#2Re: I use Attrs instead of Pydantic
#3Recently there's a strong tendency in the Python ecosystem to wrap perfectly fine idioms with even higher abstractions. Not sure if I like this, in the end it leads to a lot of cargo culting and people writing code they don't fully understand because it contains a lot of metaclass and runtime magic. But I understand that a lot of developers find this attractive, and type annotations really provide a good channel for…
Re: I use Attrs instead of Pydantic
#4Recently there's a strong tendency in the Python ecosystem to wrap perfectly fine idioms with even higher abstractions. Not sure if I like this, in the end it leads to a lot of cargo culting and people writing code they don't fully understand because it contains a lot of metaclass and runtime magic. But I understand that a lot of developers find this attractive, and type annotations really provide a good channel for…
For myself, better code completion is enough to lure me into the world of fully type-annotated python. I don't understand what you mean by saying "cargo cult", please enlighten me.
Re: I use Attrs instead of Pydantic
#5* Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don't.
* Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between systems.
Assuming my understanding is correct, it feels a bit odd to compare them as if they were like-for-like alternatives to each other. It wouldn't be crazy to use both in your code.
Personally what I'm missing in my suite of libs, is something that only does data validation. The nicest (IMO) API I've seen for it is Django's forms, but I don't like that they're inherently coupled with the UI aspect of said forms and Django itself.
Re: I use Attrs instead of Pydantic
#6I'm a bit confused, because I ultimately understood Attrs and Pydantic to be aimed at solving different problems. I may've been wrong, but my understanding was that: * Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don't. * Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between…
Links:
Re: I use Attrs instead of Pydantic
#7I'm a bit confused, because I ultimately understood Attrs and Pydantic to be aimed at solving different problems. I may've been wrong, but my understanding was that: * Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don't. * Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between…
You're right, `attrs` solves a different problem, but `cattrs` builds on top of it to tackle the same ones as Pydantic.
Re: I use Attrs instead of Pydantic
#8I'm a bit confused, because I ultimately understood Attrs and Pydantic to be aimed at solving different problems. I may've been wrong, but my understanding was that: * Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don't. * Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between…
You could have a look at Marshmallow perhaps? I use it to great effect with marshmallow-dataclass so I don't have to define the boiler-plate data classes separately to the validation/schema. Essentially the dataclass (with type-hints) becomes the data-validation specification. Links: https://pypi.org/project/marshmallow-dataclass/ https://pypi.org/project/marshmallow/
What I've really been looking for something that I can just use to check a data structure i'm passing in to a function call is valid. One use case is related to state machines, where I'm providing some data along with a transition name, and want to make sure the combination of transition + data is valid.
Re: I use Attrs instead of Pydantic
#9I'm a bit confused, because I ultimately understood Attrs and Pydantic to be aimed at solving different problems. I may've been wrong, but my understanding was that: * Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don't. * Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between…
Note the author is comparing `cattrs` with Pydantic, not `attrs`. You're right, `attrs` solves a different problem, but `cattrs` builds on top of it to tackle the same ones as Pydantic.