TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

I use Attrs instead of Pydantic

132 点作者 YohAsakura超过 3 年前

13 条评论

andrewingram超过 3 年前
I&#x27;m a bit confused, because I ultimately understood Attrs and Pydantic to be aimed at solving different problems. I may&#x27;ve been wrong, but my understanding was that:<p>* Attrs - To reduce the boilerplate of defining classes, pre-dates dataclasses, but still has a bunch of capabilities that dataclasses don&#x27;t.<p>* Pydantic - A declarative data validation + [de]serialization tool, mostly to be used at the boundaries between systems.<p>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&#x27;t be crazy to use both in your code.<p>Personally what I&#x27;m missing in my suite of libs, is something that <i>only</i> does data validation. The nicest (IMO) API I&#x27;ve seen for it is Django&#x27;s forms, but I don&#x27;t like that they&#x27;re inherently coupled with the UI aspect of said forms and Django itself.
评论 #28312393 未加载
评论 #28312468 未加载
评论 #28314752 未加载
kortex超过 3 年前
<i>That&#x27;s just like, your opinion, man!</i><p>I&#x27;m a die-hard pydantic fan, but it&#x27;s refreshing to see other perspectives, while the author is acknowledging it is their opinion, without getting all holy-war. Also, they aren&#x27;t orthogonal. Pydantic is definitely heavier than attrs in terms of processing. This is what makes pydantic a great bastion at shearing layers. &quot;Validate all 10000 ints&quot; is <i>exactly</i> what you want when parsing a request, CLI input, or some configuration.<p>Also, pydantic makes it almost trivial to write top-level app config logic that is populated from configs, env variables, secrets, etc.<p>Also, I can generate pydantic structures from openApi, jsonschema, etc, and conversely generate schema&#x2F;swagger from pydantic. This is game-breaking amounts of awesomeness.<p>On the other hand, inside business logic, pydantic has the downsides TFA mentions. I would however still contend some validation sprinkled in with business is helpful to reign in some of that zany python dynamicism in huge codebases.<p>I like the simplicity and composability of the c&#x2F;attrs approach. Along with the &quot;pydantic does not like positional args,&quot; (`__root__=(a,b,c,...)`, ugh) I&#x27;ll be considering this for retrofitting some code paths that are currently dicts with actual structured types.<p>I didn&#x27;t know about the performance differences. Sam Colvin strikes me as very thorough and the community is very involved, so I don&#x27;t think the benchmarks claiming pydantic faster than attrs is wrong. I know pydantic uses cython under the hood (no idea what) - is it possible that environmental differences are causing the discrepancy?
评论 #28314190 未加载
评论 #28317267 未加载
quietbritishjim超过 3 年前
A little off topic, but the article mentions:<p>&gt; There are three reasons to use dataclasses over attrs:<p>Another, not in this list, is that since it is in the standard library, co-workers&#x2F;contributors are significantly more likely to have heard of dataclasses and already know its interface. Another reason, sort of along similar lines, is the fact that that dataclasses has fewer features than attrs - the author mentions this as a disadvantage, but actually it&#x27;s a benefit if it has the features you need, because a simpler library is less cognative overhead to write and maintain code against.
评论 #28319282 未加载
评论 #28315412 未加载
ThePhysicist超过 3 年前
Recently there&#x27;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&#x27;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 realizing such functionality, so maybe I&#x27;m just being grumpy here.
评论 #28312927 未加载
评论 #28312189 未加载
评论 #28312806 未加载
评论 #28312793 未加载
评论 #28313063 未加载
xarope超过 3 年前
Specifically I use pydantic for validation, so yes, I expect pydantic to iterate over a list of 10000 elements and verify these are all ints.
评论 #28313068 未加载
评论 #28313807 未加载
qubidt超过 3 年前
Having run into these issues with Pydantic, we&#x27;ve been using Mashumaro[1], which, while not having all the bells and whistles of Pydantic, has served us pretty well.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;Fatal1ty&#x2F;mashumaro" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Fatal1ty&#x2F;mashumaro</a>
评论 #28313778 未加载
评论 #28336004 未加载
评论 #28315896 未加载
teajunky超过 3 年前
The author of the article is very biased because he is also the (co-)author of these libraries.<p>Personally I think that is not the fine way to bash other projects in the open source community. Clearly, attrs+cattrs and pydantic focus on different things. Let us all live together peacefully :)
评论 #28315241 未加载
pcwelder超过 3 年前
Pyantic and Cattrs both suffer with unacceptable issues and footguns. I have used Pydantic in production a lot and have played around with Cattrs. I&#x27;ve also looked at similar libraries like Dacite and marshmallow-dataclasses but none seem to be well thought out and mature.<p>* Neither Pydantic nor Cattrs handle unions like how I&#x27;d expect (although Cattrs has stronger guarantees in converting Unions)<p><pre><code> &gt;&gt;&gt; class Y(BaseModel): pass &gt;&gt;&gt; class X(BaseModel): pass &gt;&gt;&gt; class Z(BaseModel): a: Union[X, Y] &gt;&gt;&gt; Z(a=Y()) Z(a=X()) # Converts Y to X implicitly </code></pre> Cattrs has some problems with generics [1] [2]. Dacite and marshmallow-dataclasses don&#x27;t support generics well either, with some issues around Union types.<p>They do work well for simple python types but what I&#x27;d like to see is guarantee that the serialisation operation is completely reversible and if not raise warning&#x2F;exception.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;Tinche&#x2F;cattrs&#x2F;issues&#x2F;149" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Tinche&#x2F;cattrs&#x2F;issues&#x2F;149</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;Tinche&#x2F;cattrs&#x2F;issues&#x2F;44" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Tinche&#x2F;cattrs&#x2F;issues&#x2F;44</a>
评论 #28320917 未加载
da39a3ee超过 3 年前
I absolutely hate the attrs “joke” names attr.ib and attr.s.<p>That’s it. I simply can’t stand them, they drive me crazy. When I first saw them I wondered what the fuck “ib” meant. And the thing is, I’d expect every decent python programmer to have the same reaction: . is syntax; it simply can’t be part of a name. The library is called “attrs”, so why is it imported as “attr”? I simply don’t know what the author thought he was doing.<p>I just don’t see how it is appropriate for a serious library to contain a joke that’s going to trip up literally every programmer with any taste. Maybe this is my problem, maybe one day I’ll relax and wonder why I was making such a big deal about it. But for the last several years it has made me think that the author completely lacks the judgement required to be a library author.<p>To add to my criticisms, imagine what the python ecosystem would look like if everyone thought they could have names that spanned attribute lookup syntax!
评论 #28313025 未加载
评论 #28318185 未加载
mrfusion超过 3 年前
Why should I use either? I hope that’s not an obtuse question.
评论 #28317517 未加载
zmmmmm超过 3 年前
Quick look and from what I can see attrs&#x2F;cattrs can&#x27;t generate jsonschema =&gt; game over.
评论 #28313344 未加载
ledauphin超过 3 年前
I&#x27;m just here to say I largely agree with the author.<p>I think a lot of this boils down to the functional approach that (c)attrs takes, vs Pydantic&#x27;s OO. Because it&#x27;s more functional, there&#x27;s higher composability and more power given to the user.<p>On my team, we use cattrs and add our own customizations to it to great effect, and these simply would not be possible with Pydantic.<p>That said, there are reasons Pydantic seems to be ascendant despite attrs being the library that inspired dataclasses. It&#x27;s very much a batteries-included library, which fits the ethos of Python.
delaaxe超过 3 年前
From my understanding,<p><pre><code> size: float = None </code></pre> is syntactic sugar for<p><pre><code> size: Optional[float]</code></pre>
评论 #28314449 未加载
评论 #28313685 未加载