2018-07-28 11:05:12 +00:00
Type Annotations
================
`` attrs `` comes with first class support for type annotations for both Python 3.6 (:pep: `526` ) and legacy syntax.
2021-12-27 08:00:11 +00:00
However they will forever remain *optional* , therefore the example from the README could also be written as:
2018-07-28 11:05:12 +00:00
.. doctest ::
2021-12-27 08:00:11 +00:00
>>> from attrs import define, field
2018-07-28 11:05:12 +00:00
2021-12-27 08:00:11 +00:00
>>> @define
2018-07-28 11:05:12 +00:00
... class SomeClass:
2021-12-27 08:00:11 +00:00
... a_number = field(default=42)
... list_of_numbers = field(factory=list)
2018-07-28 11:05:12 +00:00
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
2021-12-27 08:00:11 +00:00
You can choose freely between the approaches, but please remember that if you choose to use type annotations, you **must** annotate **all** attributes!
----
2022-01-04 12:46:36 +00:00
Even when going all-in on type annotations, you will need `attr.field` for some advanced features though.
2018-07-28 11:05:12 +00:00
2019-02-09 12:55:48 +00:00
One of those features are the decorator-based features like defaults.
It's important to remember that `` attrs `` doesn't do any magic behind your back.
2021-12-27 08:00:11 +00:00
All the decorators are implemented using an object that is returned by the call to `attrs.field` .
2019-02-09 12:55:48 +00:00
Attributes that only carry a class annotation do not have that object so trying to call a method on it will inevitably fail.
*****
Please note that types -- however added -- are *only metadata* that can be queried from the class and they aren't used for anything out of the box!
2020-07-22 19:15:18 +00:00
Because Python does not allow references to a class object before the class is defined,
2021-12-27 08:00:11 +00:00
types may be defined as string literals, so-called *forward references* (:pep: `526` ).
You can enable this automatically for a whole module by using `` from __future__ import annotations `` (:pep: `563` ) as of Python 3.7.
In this case `` attrs `` simply puts these string literals into the `` type `` attributes.
If you need to resolve these to real types, you can call `attrs.resolve_types` which will update the attribute in place.
2018-07-28 11:05:12 +00:00
2021-05-05 14:06:09 +00:00
In practice though, types show their biggest usefulness in combination with tools like mypy_, pytype_, or pyright_ that have dedicated support for `` attrs `` classes.
2020-07-23 12:50:04 +00:00
2022-01-04 12:46:36 +00:00
The addition of static types is certainly one of the most exciting features in the Python ecosystem and helps you write *correct* and *verified self-documenting* code.
2021-05-05 08:24:53 +00:00
If you don't know where to start, Carl Meyer gave a great talk on `Type-checked Python in the Real World <https://www.youtube.com/watch?v=pMgmKJyWKn8> `_ at PyCon US 2018 that will help you to get started in no time.
2018-07-28 11:05:12 +00:00
2021-05-05 14:06:09 +00:00
2018-07-28 11:05:12 +00:00
mypy
----
2019-10-14 14:44:23 +00:00
While having a nice syntax for type metadata is great, it's even greater that mypy_ as of 0.570 ships with a dedicated `` attrs `` plugin which allows you to statically check your code.
2018-07-28 11:05:12 +00:00
Imagine you add another line that tries to instantiate the defined class using `` SomeClass("23") `` .
Mypy will catch that error for you:
.. code-block :: console
$ mypy t.py
t.py:12: error: Argument 1 to "SomeClass" has incompatible type "str"; expected "int"
This happens *without* running your code!
And it also works with *both* Python 2-style annotation styles.
To mypy, this code is equivalent to the one above:
.. code-block :: python
@attr.s
2022-03-21 09:35:44 +00:00
class SomeClass:
2018-07-28 11:05:12 +00:00
a_number = attr.ib(default=42) # type: int
2021-12-15 12:57:16 +00:00
list_of_numbers = attr.ib(factory=list, type=list[int])
2018-07-28 11:05:12 +00:00
2021-05-05 08:24:53 +00:00
pyright
-------
2018-07-28 11:05:12 +00:00
2021-05-05 08:24:53 +00:00
`` attrs `` provides support for pyright_ though the dataclass_transform_ specification.
This provides static type inference for a subset of `` attrs `` equivalent to standard-library `` dataclasses `` ,
2021-12-25 14:15:10 +00:00
and requires explicit type annotations using the `attrs.define` or `` @attr.s(auto_attribs=True) `` API.
2021-05-05 08:24:53 +00:00
Given the following definition, `` pyright `` will generate static type signatures for `` SomeClass `` attribute access, `` __init__ `` , `` __eq__ `` , and comparison methods::
@attr.define
2021-05-05 14:06:09 +00:00
class SomeClass:
2021-05-05 08:24:53 +00:00
a_number: int = 42
2021-12-15 12:57:16 +00:00
list_of_numbers: list[int] = attr.field(factory=list)
2021-05-05 08:24:53 +00:00
2021-05-05 14:06:09 +00:00
.. warning ::
2021-05-05 08:24:53 +00:00
The `` pyright `` inferred types are a subset of those supported by `` mypy `` , including:
2019-10-14 14:44:23 +00:00
2021-05-05 14:06:09 +00:00
- The generated `` __init__ `` signature only includes the attribute type annotations.
It currently does not include attribute `` converter `` types.
2021-05-05 08:24:53 +00:00
- The `` attr.frozen `` decorator is not typed with frozen attributes, which are properly typed via `` attr.define(frozen=True) `` .
2021-12-28 05:47:51 +00:00
A `full list <https://github.com/microsoft/pyright/blob/main/specs/dataclass_transforms.md#attrs> `_ of limitations and incompatibilities can be found in pyright's repository.
2021-12-08 06:10:11 +00:00
2021-05-05 08:24:53 +00:00
Your constructive feedback is welcome in both `attrs#795 <https://github.com/python-attrs/attrs/issues/795> `_ and `pyright#1782 <https://github.com/microsoft/pyright/discussions/1782> `_ .
2021-12-08 06:10:11 +00:00
Generally speaking, the decision on improving `` attrs `` support in pyright is entirely Microsoft's prerogative though.
2021-05-05 08:24:53 +00:00
2019-10-14 14:44:23 +00:00
.. _mypy: http://mypy-lang.org
.. _pytype: https://google.github.io/pytype/
2021-05-05 08:24:53 +00:00
.. _pyright: https://github.com/microsoft/pyright
2021-12-27 15:38:49 +00:00
.. _dataclass_transform: https://github.com/microsoft/pyright/blob/main/specs/dataclass_transforms.md