Classes Without Boilerplate
Project description
attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka dunder methods). Trusted by NASA for Mars missions since 2020!
Its main goal is to help you to write concise and correct software without slowing down your code.
For that, it gives you a class decorator and a way to declaratively define the attributes on that class:
>>> from attrs import asdict, define, make_class, Factory
>>> @define
... class SomeClass:
... a_number: int = 42
... list_of_numbers: list[int] = Factory(list)
...
... def hard_math(self, another_number):
... return self.a_number + sum(self.list_of_numbers) * another_number
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> sc.hard_math(3)
19
>>> sc == SomeClass(1, [1, 2, 3])
True
>>> sc != SomeClass(2, [3, 2, 1])
True
>>> asdict(sc)
{'a_number': 1, 'list_of_numbers': [1, 2, 3]}
>>> SomeClass()
SomeClass(a_number=42, list_of_numbers=[])
>>> C = make_class("C", ["a", "b"])
>>> C("foo", "bar")
C(a='foo', b='bar')
After declaring your attributes, attrs gives you:
a concise and explicit overview of the class’s attributes,
a nice human-readable __repr__,
equality-checking methods,
an initializer,
and much more,
without writing dull boilerplate code again and again and without runtime performance penalties.
Hate type annotations!? No problem! Types are entirely optional with attrs. Simply assign attrs.field() to the attributes instead of annotating them with types.
This example uses attrs’s modern APIs that have been introduced in version 20.1.0, and the attrs package import name that has been added in version 21.3.0. The classic APIs (@attr.s, attr.ib, plus their serious-business aliases) and the attr package import name will remain indefinitely.
Please check out On The Core API Names for a more in-depth explanation.
Data Classes
On the tin, attrs might remind you of dataclasses (and indeed, dataclasses are a descendant of attrs). In practice it does a lot more and is more flexible. For instance it allows you to define special handling of NumPy arrays for equality checks, or allows more ways to plug into the initialization process.
For more details, please refer to our comparison page.
Project Information
License: MIT
Source Code: https://github.com/python-attrs/attrs
Documentation: https://www.attrs.org/
Get Help: please use the python-attrs tag on StackOverflow
Third-party Extensions: https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs
Supported Python Versions: 3.5 and later (last 2.7-compatible release is 21.4.0)
If you’d like to contribute to attrs you’re most welcome and we’ve written a little guide to get you started!
attrs for Enterprise
Available as part of the Tidelift Subscription.
The maintainers of attrs and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.
Release Information
22.1.0 (2022-07-28)
Backwards-incompatible Changes
Python 2.7 is not supported anymore.
Dealing with Python 2.7 tooling has become too difficult for a volunteer-run project.
We have supported Python 2 more than 2 years after it was officially discontinued and feel that we have paid our dues. All version up to 21.4.0 from December 2021 remain fully functional, of course. #936
The deprecated cmp attribute of attrs.Attribute has been removed. This does not affect the cmp argument to attr.s that can be used as a shortcut to set eq and order at the same time. #939
Changes
Instantiation of frozen slotted classes is now faster. #898
If an eq key is defined, it is also used before hashing the attribute. #909
Added attrs.validators.min_len(). #916
attrs.validators.deep_iterable()’s member_validator argument now also accepts a list of validators and wraps them in an attrs.validators.and_(). #925
Added missing type stub re-imports for attrs.converters and attrs.filters. #931
Added missing stub for attr(s).cmp_using(). #949
attrs.validators._in()’s ValueError is not missing the attribute, expected options, and the value it got anymore. #951
Python 3.11 is now officially supported. #969
Credits
attrs is written and maintained by Hynek Schlawack.
The development is kindly supported by Variomedia AG.
A full list of contributors can be found in GitHub’s overview.
It’s the spiritual successor of characteristic and aspires to fix some of it clunkiness and unfortunate decisions. Both were inspired by Twisted’s FancyEqMixin but both are implemented using class decorators because subclassing is bad for you, m’kay?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file attrs-22.1.0.tar.gz
.
File metadata
- Download URL: attrs-22.1.0.tar.gz
- Upload date:
- Size: 201.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6 |
|
MD5 | 0487081b7ead8753fc46cf7c6d1e28e3 |
|
BLAKE2b-256 | 1acbc4ffeb41e7137b23755a45e1bfec9cbb76ecf51874c6f1d113984ecaa32c |
File details
Details for the file attrs-22.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: attrs-22.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 58.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c |
|
MD5 | f440620ea6b2c6555964dda7d9efc5d6 |
|
BLAKE2b-256 | f2bcd817287d1aa01878af07c19505fafd1165cd6a119e9d0821ca1d1c20312d |