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.
Sponsors
attrs would not be possible without our amazing sponsors. Especially those generously supporting us at the The Organization tier and higher:
Please consider joining them to help make attrs’s maintenance more sustainable!
Example
attrs 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
- Changelog
- Documentation
- PyPI
- Source Code
- Contributing
- Third-party Extensions
- License: MIT
- Get Help: please use the
python-attrstag on StackOverflow - Supported Python Versions: 3.7 and later
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
Backwards-incompatible Changes
Deprecations
-
The support for zope-interface via the
attrs.validators.providesvalidator is now deprecated and will be removed in, or after, April 2024.The presence of a C-based package in our developement dependencies has caused headaches and we're not under the impression it's used a lot.
Let us know if you're using it and we might publish it as a separate package. #1120
Changes
-
attrs.filters.exclude()andattrs.filters.include()now support the passing of attribute names as strings. #1068 -
attrs.has()andattrs.fields()now handle generic classes correctly. #1079 -
Fix frozen exception classes when raised within e.g.
contextlib.contextmanager, which mutates their__traceback__attributes. #1081 -
@frozennow works with type checkers that implement PEP-681 (ex. pyright). #1084 -
Restored ability to unpickle instances pickled before 22.2.0. #1085
-
attrs.asdict()'s andattrs.astuple()'s type stubs now accept theattrs.AttrsInstanceprotocol. #1090 -
Fix slots class cellvar updating closure in CPython 3.8+ even when
__code__introspection is unavailable. #1092 -
attrs.resolve_types()can now passinclude_extrastotyping.get_type_hints()on Python 3.9+, and does so by default. #1099 -
Added instructions for pull request workflow to
CONTRIBUTING.md. #1105 -
Added type parameter to
attrs.field()function for use withattrs.make_class().Please note that type checkers ignore type metadata passed into
make_class(), but it can be useful if you're wrapping attrs. #1107 -
It is now possible for
attrs.evolve()(andattr.evolve()) to change fields namedinstif the instance is passed as a positional argument.Passing the instance using the
instkeyword argument is now deprecated and will be removed in, or after, April 2024. #1117 -
attrs.validators.optional()now also accepts a tuple of validators (in addition to lists of validators). #1122
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file attrs-23.1.0.tar.gz.
File metadata
- Download URL: attrs-23.1.0.tar.gz
- Upload date:
- Size: 212.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6279836d581513a26f1bf235f9acd333bc9115683f14f7e8fae46c98fc50e015
|
|
| MD5 |
6623fed7ffa22261ba25fccaf4d99539
|
|
| BLAKE2b-256 |
979081f95d5f705be17872843536b1868f351805acf6971251ff07c1b8334dbb
|
File details
Details for the file attrs-23.1.0-py3-none-any.whl.
File metadata
- Download URL: attrs-23.1.0-py3-none-any.whl
- Upload date:
- Size: 61.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f28b4522cdc2fb4256ac1a020c78acf9cba2c6b461ccd2c126f3aa8e8335d04
|
|
| MD5 |
5aecf1f8857233a04c1926ea68e8f860
|
|
| BLAKE2b-256 |
f0ebfcb708c7bf5056045e9e98f62b93bd7467eb718b0202e7698eb11d66416c
|