Live data from Hacker News

Attrs – the Python library everyone needs (2016)

glyph.twistedmatrix.com

81–90 of 115 posts

Re: Attrs – the Python library everyone needs (2016)

#81
post #72

Earlier quoted context omitted.

As soon as I opened the article I pressed Cmd+F and searched for "dataclasses". This article was correct and addressed a very real need in Python programming—for year 2016. By now it is obsolete and today's standard library module `dataclasses` does all of that and more.

attrs classes still have several features that dataclasses don't, and likely never will, [like validators and converters]( https://www.attrs.org/en/stable/why.html#data-classes ). So it's not obsolete, particularly for anyone already relying on those features.

dataclasses have methods for iterating fields and inspecting types, so any feature can be added. There is also the benefit of type checking. My grief with dataclasses is how I can't inherit from dataclasses with default fields without making all child class fields also have defaults.

Re: Attrs – the Python library everyone needs (2016)

#82
post #9

Earlier quoted context omitted.

Pydantic types will be checked by mypy or any other static type analysis tool as well. I don’t expect any type-related thing to be remotely safe in Python without applying at least mypy and pylint, potentially pyright as well, plus, as always with an interpreted language, unit tests for typing issues that would be caught by a compiler in another language

Welp not sure why I caught downvotes for using the two most common static analysis tools for Python, but here we are on Hacker News

Likely the reason is that you really only need just mypy. It handles the type issues. Pylint is useful, but doesn't really overlap with mypy in terms of what it catches, and there's no need to use pyright or to write type-verifying unit tests, or to do runtime type validation if you have mypy.

Using mypy gives you the type safety equivalent of a compiled language. If you're using mypy, you don't need any additional validation that you wouldn't use in java or c++. I didn't downvote you, but the needless defense in depth is weird.

Re: Attrs – the Python library everyone needs (2016)

#83
post #22

Costs: Depending on a relatively unknown library. Using arcane class decorators and unusual syntactic constructs: @attr.s and x = attr.ib() (a pun?). Benefits: Saving at best 10-15 lines of boilerplate per data class. Much less if namedtuple works for you. If you want to save lines in __init__ you can write "for k, v in locals().items(): setattr(self, k, v)". But you shouldn't. Edit: Forgot to add to the most importa…

> However, you do need to understand a lot of Python internals to grok x = attr.ib().

No more than with namedtuples (in fact, both use essentially the same magic: code generation and `eval`).

Re: Attrs – the Python library everyone needs (2016)

#85

The @dataclass decorator [1] as proposed in PEP 557 [2] has also been available since Python 3.7 was released in 2018. Using @dataclass the example from OP would look like: from dataclasses import dataclass @dataclass class Point3D: x: float y: float z: float [1]: https://docs.python.org/3/library/dataclasses.html [2]: https://www.python.org/dev/peps/pep-0557/

As soon as I opened the article I pressed Cmd+F and searched for "dataclasses". This article was correct and addressed a very real need in Python programming—for year 2016. By now it is obsolete and today's standard library module `dataclasses` does all of that and more.

attrs was first, dataclasses design is actually based on attrs! dataclasses is inferior in features and actually a shame because I see this ignorant attitude everywhere and I have to defend and explain attrs every time we need it.

Re: Attrs – the Python library everyone needs (2016)

#86
post #4

IMO Pydantic is way more ergonomic, has great defaults, and easier to bend to your will when you want to use it a little differently. Lots of love to Attrs, which is a great library and is a component of a lot of great software. It was my go-to library for years before Pydantic matured, but I think a lot of people have rightly started to move on to Pydantic, particularly with the popularity of FastAPI

Apples and oranges. pydantic is a serialization and input validation library, attrs is a class-generator library. Completely different features, completely different use cases.

Yes, you can do validation in attrs, but it's not meant to be used the same way as pydantic. For serialization, you need cattrs, which is a completely different package.

Re: Attrs – the Python library everyone needs (2016)

#87

Earlier quoted context omitted.

well one appeal is that you dont have to write constructors, that‘s already enough of a win for me. then you get sane eq, and sane str, and already you remove 90% boilerplate

I really, genuinely don't get the appeal. I don't follow the "less code = better" ideology so maybe that's a contributor but I really don't see how this: class Person: def __init__(self, name, age): self.name = name self.age = age is any worse than this: @dataclass class Person: name: str age: int I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost.

> I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost.

Until you need them for debugging.

And dataclasses make them free, at lesst syntactically.

Re: Attrs – the Python library everyone needs (2016)

#88
post #60

This is only remotely relevant but I recently learned that the related `dataclasses` is implemented by constructing _string representations_ of functions and calling `exec` on them. https://github.com/python/cpython/blob/3.10/Lib/dataclasses.... Kind of blew my mind

Ew. How can there not be some better way to do it?

New to python? It might be big, but it sure is slow!

Re: Attrs – the Python library everyone needs (2016)

#89
post #72

Earlier quoted context omitted.

attrs classes still have several features that dataclasses don't, and likely never will, [like validators and converters]( https://www.attrs.org/en/stable/why.html#data-classes ). So it's not obsolete, particularly for anyone already relying on those features.

dataclasses have methods for iterating fields and inspecting types, so any feature can be added. There is also the benefit of type checking. My grief with dataclasses is how I can't inherit from dataclasses with default fields without making all child class fields also have defaults.

dataclasses author here.

Does the keyword-only feature in 3.10 help you at all?

Re: Attrs – the Python library everyone needs (2016)

#90

Earlier quoted context omitted.

dataclasses have methods for iterating fields and inspecting types, so any feature can be added. There is also the benefit of type checking. My grief with dataclasses is how I can't inherit from dataclasses with default fields without making all child class fields also have defaults.

dataclasses author here. Does the keyword-only feature in 3.10 help you at all?

Wow, I was unaware of this feature. It appears there are 3 ways to declare fields as keyword-only.

    @dataclass(kw_only=True)
    class Birthday:
        name: str
        birthday: datetime.date

    # ---

    @dataclass
    class Birthday:
        name: str
        birthday: datetime.date = field(kw_only=True)

    # ---

    from dataclasses import KW_ONLY

    @dataclass
    class Point:
        x: float
        y: float
        _: KW_ONLY
        z: float = 0.0
        t: float = 0.0

https://docs.python.org/3/whatsnew/3.10.html#keyword-only-fi...
Post reply on HN