Skip to main content

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.


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!


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)
>>> sc == SomeClass(1, [1, 2, 3])
>>> sc != SomeClass(2, [3, 2, 1])

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

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

  • Python 3.6 has been dropped and packaging switched to static package data using Hatch. #993


  • The support for zope-interface via the attrs.validators.provides validator 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


  • attrs.filters.exclude() and attrs.filters.include() now support the passing of attribute names as strings. #1068

  • attrs.has() and attrs.fields() now handle generic classes correctly. #1079

  • Fix frozen exception classes when raised within e.g. contextlib.contextmanager, which mutates their __traceback__ attributes. #1081

  • @frozen now 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 and attrs.astuple()'s type stubs now accept the attrs.AttrsInstance protocol. #1090

  • Fix slots class cellvar updating closure in CPython 3.8+ even when __code__ introspection is unavailable. #1092

  • attrs.resolve_types() can now pass include_extras to typing.get_type_hints() on Python 3.9+, and does so by default. #1099

  • Added instructions for pull request workflow to #1105

  • Added type parameter to attrs.field() function for use with attrs.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() (and attr.evolve()) to change fields named inst if the instance is passed as a positional argument.

    Passing the instance using the inst keyword 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

Full changelog

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

attrs-23.1.0.tar.gz (212.9 kB view hashes)

Uploaded source

Built Distribution

attrs-23.1.0-py3-none-any.whl (61.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page