| 1 |
# On The Core API Names |
| 2 |
|
| 3 |
You may be surprised seeing *attrs* classes being created using {func}`attrs.define` and with type annotated fields, instead of {func}`attr.s` and {func}`attr.ib()`. |
| 4 |
|
| 5 |
Or, you wonder why the web and talks are full of this weird `attr.s` and `attr.ib` -- including people having strong opinions about it and using `attr.attrs` and `attr.attrib` instead. |
| 6 |
|
| 7 |
And what even is `attr.dataclass` that's not documented but commonly used!? |
| 8 |
|
| 9 |
|
| 10 |
## TL;DR |
| 11 |
|
| 12 |
We recommend our modern APIs for new code: |
| 13 |
|
| 14 |
% {func} syntax does currently not work for `.. function` definitions. |
| 15 |
|
| 16 |
- {func}`attrs.define` to define a new class, |
| 17 |
- [`attrs.mutable()`](attrs.mutable) is an alias for {func}`attrs.define`, |
| 18 |
- [`attrs.frozen()`](attrs.frozen) is an alias for `define(frozen=True)` |
| 19 |
- and {func}`attrs.field()` to define an attribute. |
| 20 |
|
| 21 |
They have been added in *attrs* 20.1.0, they are expressive, and they have modern defaults like slots and type annotation awareness switched on by default. |
| 22 |
Sometimes they're referred to as *next-generation* or *NG* APIs. |
| 23 |
As of *attrs* 21.3.0 you can also import them from the `attrs` package namespace. |
| 24 |
|
| 25 |
The traditional APIs {func}`attr.s` / {func}`attr.ib`, their serious-business aliases `attr.attrs` / `attr.attrib`, and the never-documented, but popular `attr.dataclass` easter egg will stay **forever**. |
| 26 |
|
| 27 |
*attrs* will **never** force you to use type annotations. |
| 28 |
|
| 29 |
|
| 30 |
## A Short History Lesson |
| 31 |
|
| 32 |
At this point, *attrs* is an old project. |
| 33 |
It had its first release in April 2015 -- back when most Python code was on Python 2.7 and Python 3.4 was the first Python 3 release that showed promise. |
| 34 |
*attrs* was always Python 3-first, but [type annotations](https://peps.python.org/pep-0484/) came only into Python 3.5 that was released in September 2015 and were largely ignored until years later. |
| 35 |
|
| 36 |
At this time, if you didn't want to implement all the {term}`dunder methods`, the most common way to create a class with some attributes on it was to subclass a {obj}`collections.namedtuple`, or one of the many hacks that allowed you to access dictionary keys using attribute lookup. |
| 37 |
|
| 38 |
But *attrs* history goes even a bit further back, to the now-forgotten [*characteristic*](https://github.com/hynek/characteristic) that came out in May 2014 and already used a class decorator, but was overall too unergonomic. |
| 39 |
|
| 40 |
In the wake of all of that, [Glyph](https://github.com/glyph) and [Hynek](https://github.com/hynek) came together on IRC and brainstormed how to take the good ideas of *characteristic*, but make them easier to use and read. |
| 41 |
At this point the plan was not to make *attrs* what it is now -- a flexible class-building kit. |
| 42 |
All we wanted was an ergonomic little library to succinctly define classes with attributes. |
| 43 |
|
| 44 |
Under the impression of the unwieldy `characteristic` name, we went to the other side and decided to make the package name part of the API, and keep the API functions very short. |
| 45 |
This led to the infamous {func}`attr.s` and {func}`attr.ib` which some found confusing and pronounced it as "attr dot s" or used a singular `@s` as the decorator. |
| 46 |
But it was really just a way to say `attrs` and `attrib`[^attr]. |
| 47 |
|
| 48 |
[^attr]: We considered calling the PyPI package just `attr` too, but the name was already taken by an *ostensibly* inactive [package on PyPI](https://pypi.org/project/attr/#history). |
| 49 |
|
| 50 |
Some people hated this cutey API from day one, which is why we added aliases for them that we called *serious business*: `@attr.attrs` and `attr.attrib()`. |
| 51 |
Fans of them usually imported the names and didn't use the package name in the first place. |
| 52 |
Unfortunately, the `attr` package name started creaking the moment we added `attr.Factory`, since it couldn’t be morphed into something meaningful in any way. |
| 53 |
A problem that grew worse over time, as more APIs and even modules were added. |
| 54 |
|
| 55 |
But overall, *attrs* in this shape was a **huge** success -- especially after Glyph's blog post [*The One Python Library Everyone Needs*](https://glyph.twistedmatrix.com/2016/08/attrs.html) in August 2016 and [*pytest*](https://docs.pytest.org/) adopting it. |
| 56 |
|
| 57 |
Being able to just write: |
| 58 |
|
| 59 |
``` |
| 60 |
@attr.s |
| 61 |
class Point: |
| 62 |
x = attr.ib() |
| 63 |
y = attr.ib() |
| 64 |
``` |
| 65 |
|
| 66 |
was a big step for those who wanted to write small, focused classes. |
| 67 |
|
| 68 |
### Dataclasses Enter The Arena |
| 69 |
|
| 70 |
A big change happened in May 2017 when Hynek sat down with [Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum) and [Eric V. Smith](https://github.com/ericvsmith) at PyCon US 2017. |
| 71 |
|
| 72 |
Type annotations for class attributes have [just landed](https://peps.python.org/pep-0526/) in Python 3.6 and Guido felt like it would be a good mechanic to introduce something similar to *attrs* to the Python standard library. |
| 73 |
The result, of course, was {pep}`557`[^stdlib] which eventually became the `dataclasses` module in Python 3.7. |
| 74 |
|
| 75 |
[^stdlib]: The highly readable PEP also explains why *attrs* wasn't just added to the standard library. |
| 76 |
Don't believe the myths and rumors. |
| 77 |
|
| 78 |
*attrs* at this point was lucky to have several people on board who were also very excited about type annotations and helped implement it; including a [Mypy plugin](https://medium.com/@Pilot-EPD-Blog/mypy-and-attrs-e1b0225e9ac6). |
| 79 |
And so it happened that *attrs* [shipped](https://www.attrs.org/en/17.3.0.post2/changelog.html) the new method of defining classes more than half a year before Python 3.7 -- and thus `dataclasses` -- were released. |
| 80 |
|
| 81 |
--- |
| 82 |
|
| 83 |
Due to backwards-compatibility concerns, this feature is off by default in the {func}`attr.s` decorator and has to be activated using `@attr.s(auto_attribs=True)`, though. |
| 84 |
As a little easter egg and to save ourselves some typing, we've also [added](https://github.com/python-attrs/attrs/commit/88aa1c897dfe2ee4aa987e4a56f2ba1344a17238#diff-4fc63db1f2fcb7c6e464ee9a77c3c74e90dd191d1c9ffc3bdd1234d3a6663dc0R48) an alias called `attr.dataclasses` that just set `auto_attribs=True`. |
| 85 |
It was never documented, but people found it and used it and loved it. |
| 86 |
|
| 87 |
Over the next months and years it became clear that type annotations have become the popular way to define classes and their attributes. |
| 88 |
However, it has also become clear that some people viscerally hate type annotations. |
| 89 |
We're determined to serve both. |
| 90 |
|
| 91 |
|
| 92 |
### *attrs* TNG |
| 93 |
|
| 94 |
Over its existence, *attrs* never stood still. |
| 95 |
But since we also greatly care about backwards-compatibility and not breaking our users' code, many features and niceties have to be manually activated. |
| 96 |
|
| 97 |
That is not only annoying, it also leads to the problem that many of *attrs*'s users don't even know what it can do for them. |
| 98 |
We've spent years alone explaining that defining attributes using type annotations is in no way unique to {mod}`dataclasses`. |
| 99 |
|
| 100 |
Finally we've decided to take the [Go route](https://go.dev/blog/module-compatibility): |
| 101 |
Instead of fiddling with the old APIs -- whose names felt anachronistic anyway -- we'd define new ones, with better defaults. |
| 102 |
So in July 2018, we [looked for better names](https://github.com/python-attrs/attrs/issues/408) and came up with {func}`attr.define`, {func}`attr.field`, and friends. |
| 103 |
Then in January 2019, we [started looking for inconvenient defaults](https://github.com/python-attrs/attrs/issues/487) that we now could fix without any repercussions. |
| 104 |
|
| 105 |
These APIs proved to be very popular, so we've finally changed the documentation to them in November of 2021. |
| 106 |
|
| 107 |
All of this took way too long, of course. |
| 108 |
One reason is the COVID-19 pandemic, but also our fear to fumble this historic chance to fix our APIs. |
| 109 |
|
| 110 |
Finally, in December 2021, we've added the *attrs* package namespace. |
| 111 |
|
| 112 |
We hope you like the result: |
| 113 |
|
| 114 |
``` |
| 115 |
from attrs import define |
| 116 |
|
| 117 |
@define |
| 118 |
class Point: |
| 119 |
x: int |
| 120 |
y: int |
| 121 |
``` |