News for hackers. Stuff that matters.
Re:I use Attrs instead of Pydantic by andrewingram on Thursday August 26, 2021 @05:02AM (#28312363)
I'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 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 by zo1 on Thursday August 26, 2021 @05:06AM (#28312393)
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/

Re:I use Attrs instead of Pydantic by nerdponx on Thursday August 26, 2021 @07:57AM (#28313766)
Worth pointing out Desert, which generates a Marshmallow schema from an Attrs class. You could then integrate this with Apispec or Marshmallow-Jsonschema.

https://pypi.org/project/desert/

https://pypi.org/project/marshmallow-jsonschema/

https://pypi.org/project/apispec/

Re:I use Attrs instead of Pydantic by andrewingram on Thursday August 26, 2021 @05:23AM (#28312498)
Thanks, I've had a look at Marshmallow in the past, but it's still doing the extra bits of dealing with the extra bits of serialization, it never really felt all that dissimilar to Pydantic.

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 by radus on Thursday August 26, 2021 @09:25AM (#28314586)
Pedantic has a validate_arguments decorator that while in beta, I think does what you want: https://pydantic-docs.helpmanual.io/usage/validation_decorat...
Re:I use Attrs instead of Pydantic by nkconway on Thursday August 26, 2021 @04:18PM (#28320103)
I have used a combo of attrs and fastjsonschema to do dataclass validation on instance construction.

It works great for a python stack and you can generate some clean looking code if you have a reusable data class package

Re:I use Attrs instead of Pydantic by nerdponx on Thursday August 26, 2021 @07:54AM (#28313725)
Design-by-contract, perhaps? There are a couple libraries like this currently, and you could pretty easily write your own framework for it.

Also, Attrs and Pydantic both support arbitrary additional validation on fields.

Re:I use Attrs instead of Pydantic by bilboa on Thursday August 26, 2021 @09:40AM (#28314793)
Sounds like you want jsonschema.
Re:I use Attrs instead of Pydantic by JPKab on Thursday August 26, 2021 @12:31PM (#28317109)
I use marshmallow-dataclass in an API client lib I've been continuously working on for a while for work.

It's great stuff, but I'm always looking for alternatives.

Re:I use Attrs instead of Pydantic by yeray on Thursday August 26, 2021 @05:18AM (#28312468)
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.

Re:I use Attrs instead of Pydantic by andrewingram on Thursday August 26, 2021 @05:25AM (#28312513)
Thanks for the clarification, cattrs actually looks pretty cool!
Re:I use Attrs instead of Pydantic by prionassembly on Thursday August 26, 2021 @09:37AM (#28314752)
Pydantic's "BaseSettings" has pretty much cured the evergreen itch of writing my own configuration languages. I love that env vars automatically override default values.
Re:I use Attrs instead of Pydantic by RSHEPP on Thursday August 26, 2021 @12:28PM (#28317061)
Agreed. All my Go apps were written this way, it was nice to use Pydantic in a similar way. Especially in K8s, where most of my config is env vars.