Live data from Hacker News

I use Attrs instead of Pydantic

threeofwands.com

21–30 of 76 posts

Re: I use Attrs instead of Pydantic

#22
post #17

Quick look and from what I can see attrs/cattrs can't generate jsonschema => game over.

It would be better in the other way. Generate sane python models with it using existing jsonschema - similar to stuff like grpc, but without extra bits.

The existing generators are generally terrible.

Re: I use Attrs instead of Pydantic

#23
post #16

Earlier quoted context omitted.

The `attrs` authors address it in the documentation: https://www.attrs.org/en/stable/overview.html#on-the-attr-s-... At first, some people have a negative gut reaction to that; resembling the reactions to Python’s significant whitespace. And as with that, once one gets used to it, the readability and explicitness of that API prevails and delights. For those who can’t swallow that API at all, attrs comes with serious…

Yes, I know. That doesn’t redeem it. I want never to have to read those names in python code I'm working with; the documentation entry doesn't achieve that. And anyway, the library is called “attrs”, so why is it imported as “attr”?

>And anyway, the library is called “attrs”, so why is it imported as “attr”?

Python packaging is terrible, and giving package authors footguns like this. It's possible that it would be imported as "da39a3ee".

Re: I use Attrs instead of Pydantic

#25
post #17

Quick look and from what I can see attrs/cattrs can't generate jsonschema => game over.

It would be better in the other way. Generate sane python models with it using existing jsonschema - similar to stuff like grpc, but without extra bits. The existing generators are generally terrible.

had generally good experience creating typed wrappers for api's with json-schema-to-pydantic[0] converter

[0] https://github.com/koxudaxi/datamodel-code-generator

Re: I use Attrs instead of Pydantic

#27

Earlier quoted context omitted.

Yes, I know. That doesn’t redeem it. I want never to have to read those names in python code I'm working with; the documentation entry doesn't achieve that. And anyway, the library is called “attrs”, so why is it imported as “attr”?

>And anyway, the library is called “attrs”, so why is it imported as “attr”? Python packaging is terrible, and giving package authors footguns like this. It's possible that it would be imported as "da39a3ee".

"attr" is the choice of the library designers. The question is why did they choose that name when users will expect it to be imported as "attrs"?

Clearly if they had chosen "da39a3ee" we wouldn't need to discuss whether they had good judgement.

Re: I use Attrs instead of Pydantic

#28
post #16

Earlier quoted context omitted.

The `attrs` authors address it in the documentation: https://www.attrs.org/en/stable/overview.html#on-the-attr-s-... At first, some people have a negative gut reaction to that; resembling the reactions to Python’s significant whitespace. And as with that, once one gets used to it, the readability and explicitness of that API prevails and delights. For those who can’t swallow that API at all, attrs comes with serious…

Yes, I know. That doesn’t redeem it. I want never to have to read those names in python code I'm working with; the documentation entry doesn't achieve that. And anyway, the library is called “attrs”, so why is it imported as “attr”?

The package can contain entirely arbitrary module names. Any similarity in naming is by convention only. Don't quote me on this, but I don't see why two different packages can't contain the same module name and overwrite each other.

Re: I use Attrs instead of Pydantic

#29
post #6

Earlier quoted context omitted.

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/

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 transitio…

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

#30
post #6

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…

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/

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/

Post reply on HN