attrs2 20.1.2
Classes Without Boilerplate
License: MIT
Requires Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
Keywords: class,attribute,boilerplate

.. image:: https://www.attrs.org/en/latest/_static/attrs_logo.png
   :alt: attrs Logo

======================================
``attrs``: Classes Without Boilerplate
======================================

.. image:: https://readthedocs.org/projects/attrs/badge/?version=stable
   :target: https://www.attrs.org/en/stable/?badge=stable
   :alt: Documentation Status

.. image:: https://github.com/python-attrs/attrs/workflows/CI/badge.svg?branch=master
   :target: https://github.com/python-attrs/attrs/actions?workflow=CI
   :alt: CI Status

.. image:: https://codecov.io/github/python-attrs/attrs/branch/master/graph/badge.svg
   :target: https://codecov.io/github/python-attrs/attrs
   :alt: Test Coverage

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
   :target: https://github.com/psf/black
   :alt: Code style: black

.. teaser-begin

``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 <https://nedbatchelder.com/blog/200605/dunder.html>`_ methods).

Its main goal is to help you to write **concise** and **correct** software without slowing down your code.

.. teaser-end

For that, it gives you a class decorator and a way to declaratively define the attributes on that class:

.. -code-begin-

.. code-block:: pycon

   >>> import attr

   >>> @attr.s
   ... class SomeClass(object):
   ...     a_number = attr.ib(default=42)
   ...     list_of_numbers = attr.ib(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

   >>> attr.asdict(sc)
   {'a_number': 1, 'list_of_numbers': [1, 2, 3]}

   >>> SomeClass()
   SomeClass(a_number=42, list_of_numbers=[])

   >>> C = attr.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__``,
- a complete set of comparison methods (equality and ordering),
- an initializer,
- and much more,

*without* writing dull boilerplate code again and again and *without* runtime performance penalties.

On Python 3.6 and later, you can often even drop the calls to ``attr.ib()`` by using `type annotations <https://www.attrs.org/en/latest/types.html>`_.

This gives you the power to use actual classes with actual types in your code instead of confusing ``tuple``\ s or `confusingly behaving <https://www.attrs.org/en/stable/why.html#namedtuples>`_ ``namedtuple``\ s.
Which in turn encourages you to write *small classes* that do `one thing well <https://www.destroyallsoftware.com/talks/boundaries>`_.
Never again violate the `single responsibility principle <https://en.wikipedia.org/wiki/Single_responsibility_principle>`_ just because implementing ``__init__`` et al is a painful drag.


.. -getting-help-

Getting Help
============

Please use the ``python-attrs`` tag on `StackOverflow <https://stackoverflow.com/questions/tagged/python-attrs>`_ to get help.

Answering questions of your fellow developers is also great way to help the project!


.. -project-information-

Project Information
===================

``attrs`` is released under the `MIT <https://choosealicense.com/licenses/mit/>`_ license,
its documentation lives at `Read the Docs <https://www.attrs.org/>`_,
the code on `GitHub <https://github.com/python-attrs/attrs>`_,
and the latest release on `PyPI <https://pypi.org/project/attrs/>`_.
It’s rigorously tested on Python 2.7, 3.5+, and PyPy.

We collect information on **third-party extensions** in our `wiki <https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs>`_.
Feel free to browse and add your own!

If you'd like to contribute to ``attrs`` you're most welcome and we've written `a little guide <https://www.attrs.org/en/latest/contributing.html>`_ 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. <https://tidelift.com/subscription/pkg/pypi-attrs?utm_source=pypi-attrs&utm_medium=referral&utm_campaign=enterprise&utm_term=repo>`_


Release Information
===================

20.1.0 (2020-08-20)
-------------------

Backward-incompatible Changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Python 3.4 is not supported anymore.
  It has been unsupported by the Python core team for a while now, its PyPI downloads are negligible, and our CI provider removed it as a supported option.

  It's very unlikely that ``attrs`` will break under 3.4 anytime soon, which is why we do *not* block its installation on Python 3.4.
  But we don't test it anymore and will block it once someone reports breakage.
  `#608 <https://github.com/python-attrs/attrs/issues/608>`_


Deprecations
^^^^^^^^^^^^

- Less of a deprecation and more of a heads up: the next release of ``attrs`` will introduce an ``attrs`` namespace.
  That means that you'll finally be able to run ``import attrs`` with new functions that aren't cute abbreviations and that will carry better defaults.

  This should not break any of your code, because project-local packages have priority before installed ones.
  If this is a problem for you for some reason, please report it to our bug tracker and we'll figure something out.

  The old ``attr`` namespace isn't going anywhere and its defaults are not changing – this is a purely additive measure.
  Please check out the linked issue for more details.

  These new APIs have been added *provisionally* as part of #666 so you can try them out today and provide feedback.
  Learn more in the `API docs <https://www.attrs.org/en/stable/api.html#provisional-apis>`_.
  `#408 <https://github.com/python-attrs/attrs/issues/408>`_


Changes
^^^^^^^

- Added ``attr.resolve_types()``.
  It ensures that all forward-references and types in string form are resolved into concrete types.

  You need this only if you need concrete types at runtime.
  That means that if you only use types for static type checking, you do **not** need this function.
  `#288 <https://github.com/python-attrs/attrs/issues/288>`_,
  `#302 <https://github.com/python-attrs/attrs/issues/302>`_
- Added ``@attr.s(collect_by_mro=False)`` argument that if set to ``True`` fixes the collection of attributes from base classes.

  It's only necessary for certain cases of multiple-inheritance but is kept off for now for backward-compatibility reasons.
  It will be turned on by default in the future.

  As a side-effect, ``attr.Attribute`` now *always* has an ``inherited`` attribute indicating whether an attribute on a class was directly defined or inherited.
  `#428 <https://github.com/python-attrs/attrs/issues/428>`_,
  `#635 <https://github.com/python-attrs/attrs/issues/635>`_
- On Python 3, all generated methods now have a docstring explaining that they have been created by ``attrs``.
  `#506 <https://github.com/python-attrs/attrs/issues/506>`_
- It is now possible to prevent ``attrs`` from auto-generating the ``__setstate__`` and ``__getstate__`` methods that are required for pickling of slotted classes.

  Either pass ``@attr.s(getstate_setstate=False)`` or pass ``@attr.s(auto_detect=True)`` and implement them yourself:
  if ``attrs`` finds either of the two methods directly on the decorated class, it assumes implicitly ``getstate_setstate=False`` (and implements neither).

  This option works with dict classes but should never be necessary.
  `#512 <https://github.com/python-attrs/attrs/issues/512>`_,
  `#513 <https://github.com/python-attrs/attrs/issues/513>`_,
  `#642 <https://github.com/python-attrs/attrs/issues/642>`_
- Fixed a ``ValueError: Cell is empty`` bug that could happen in some rare edge cases.
  `#590 <https://github.com/python-attrs/attrs/issues/590>`_
- ``attrs`` can now automatically detect your own implementations and infer ``init=False``, ``repr=False``, ``eq=False``, ``order=False``, and ``hash=False`` if you set ``@attr.s(auto_detect=True)``.
  ``attrs`` will ignore inherited methods.
  If the argument implies more than one method (e.g. ``eq=True`` creates both ``__eq__`` and ``__ne__``), it's enough for *one* of them to exist and ``attrs`` will create *neither*.

  This feature requires Python 3.
  `#607 <https://github.com/python-attrs/attrs/issues/607>`_
- Added ``attr.converters.pipe()``.
  The feature allows combining multiple conversion callbacks into one by piping the value through all of them, and retuning the last result.

  As part of this feature, we had to relax the type information for converter callables.
  `#618 <https://github.com/python-attrs/attrs/issues/618>`_
- Fixed serialization behavior of non-slots classes with ``cache_hash=True``.
  The hash cache will be cleared on operations which make "deep copies" of instances of classes with hash caching,
  though the cache will not be cleared with shallow copies like those made by ``copy.copy()``.

  Previously, ``copy.deepcopy()`` or serialization and deserialization with ``pickle`` would result in an un-initialized object.

  This change also allows the creation of ``cache_hash=True`` classes with a custom ``__setstate__``,
  which was previously forbidden (`#494 <https://github.com/python-attrs/attrs/issues/494>`_).
  `#620 <https://github.com/python-attrs/attrs/issues/620>`_
- It is now possible to specify hooks that are called whenever an attribute is set **after** a class has been instantiated.

  You can pass ``on_setattr`` both to ``@attr.s()`` to set the default for all attributes on a class, and to ``@attr.ib()`` to overwrite it for individual attributes.

  ``attrs`` also comes with a new module ``attr.setters`` that brings helpers that run validators, converters, or allow to freeze a subset of attributes.
  `#645 <https://github.com/python-attrs/attrs/issues/645>`_,
  `#660 <https://github.com/python-attrs/attrs/issues/660>`_
- **Provisional** APIs called ``attr.define()``, ``attr.mutable()``, and ``attr.frozen()`` have been added.

  They are only available on Python 3.6 and later, and call ``attr.s()`` with different default values.

  If nothing comes up, they will become the official way for creating classes in 20.2.0 (see above).

  **Please note** that it may take some time until mypy – and other tools that have dedicated support for ``attrs`` – recognize these new APIs.
  Please **do not** open issues on our bug tracker, there is nothing we can do about it.
  `#666 <https://github.com/python-attrs/attrs/issues/666>`_
- We have also provisionally added ``attr.field()`` that supplants ``attr.ib()``.
  It also requires at least Python 3.6 and is keyword-only.
  Other than that, it only dropped a few arguments, but changed no defaults.

  As with ``attr.s()``: ``attr.ib()`` is not going anywhere.
  `#669 <https://github.com/python-attrs/attrs/issues/669>`_

`Full changelog <https://github.com/nexB/attrsen/stable/changelog.html>`_.

Credits
=======

``attrs`` is written and maintained by `Hynek Schlawack <https://hynek.me/>`_.

The development is kindly supported by `Variomedia AG <https://www.variomedia.de/>`_.

A full list of contributors can be found in `GitHub's overview <https://github.com/python-attrs/attrs/graphs/contributors>`_.

It’s the spiritual successor of `characteristic <https://characteristic.readthedocs.io/>`_ and aspires to fix some of it clunkiness and unfortunate decisions.
Both were inspired by Twisted’s `FancyEqMixin <https://twistedmatrix.com/documents/current/api/twisted.python.util.FancyEqMixin.html>`_ but both are implemented using class decorators because `subclassing is bad for you <https://www.youtube.com/watch?v=3MNVP9-hglc>`_, m’kay?