1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
API Reference for the ``attr`` Namespace
========================================
.. note::
These are the traditional APIs whose creation predates type annotations.
They are **not** deprecated, but we suggest using the :mod:`attrs` namespace for new code, because they look nicer and have better defaults.
See also :doc:`names`.
.. module:: attr
Core
----
.. autofunction:: attr.s(these=None, repr_ns=None, repr=None, cmp=None, hash=None, init=None, slots=False, frozen=False, weakref_slot=True, str=False, auto_attribs=False, kw_only=False, cache_hash=False, auto_exc=False, eq=None, order=None, auto_detect=False, collect_by_mro=False, getstate_setstate=None, on_setattr=None, field_transformer=None, match_args=True, unsafe_hash=None)
For example:
.. doctest::
>>> import attr
>>> @attr.s
... class C:
... _private = attr.ib()
>>> C(private=42)
C(_private=42)
>>> class D:
... def __init__(self, x):
... self.x = x
>>> D(1)
<D object at ...>
>>> D = attr.s(these={"x": attr.ib()}, init=False)(D)
>>> D(1)
D(x=1)
>>> @attr.s(auto_exc=True)
... class Error(Exception):
... x = attr.ib()
... y = attr.ib(default=42, init=False)
>>> Error("foo")
Error(x='foo', y=42)
>>> raise Error("foo")
Traceback (most recent call last):
...
Error: ('foo', 42)
>>> raise ValueError("foo", 42) # for comparison
Traceback (most recent call last):
...
ValueError: ('foo', 42)
.. autofunction:: attr.ib
.. note::
*attrs* also comes with a serious-business alias ``attr.attrib``.
The object returned by `attr.ib` also allows for setting the default and the validator using decorators:
.. doctest::
>>> @attr.s
... class C:
... x = attr.ib()
... y = attr.ib()
... @x.validator
... def _any_name_except_a_name_of_an_attribute(self, attribute, value):
... if value < 0:
... raise ValueError("x must be positive")
... @y.default
... def _any_name_except_a_name_of_an_attribute(self):
... return self.x + 1
>>> C(1)
C(x=1, y=2)
>>> C(-1)
Traceback (most recent call last):
...
ValueError: x must be positive
.. function:: attrs
Serious business alias for `attr.s`.
.. function:: define
Same as `attrs.define`.
.. function:: mutable
Same as `attrs.mutable`.
.. function:: frozen
Same as `attrs.frozen`.
.. function:: field
Same as `attrs.field`.
.. class:: Attribute
Same as `attrs.Attribute`.
.. function:: make_class
Same as `attrs.make_class`.
.. autoclass:: Factory
:noindex:
Same as `attrs.Factory`.
.. data:: NOTHING
Same as `attrs.NOTHING`.
Exceptions
----------
.. module:: attr.exceptions
All exceptions are available from both ``attr.exceptions`` and `attrs.exceptions` (it's the same module in a different namespace).
Please refer to `attrs.exceptions` for details.
Helpers
-------
.. currentmodule:: attr
.. function:: cmp_using
Same as `attrs.cmp_using`.
.. function:: fields
Same as `attrs.fields`.
.. function:: fields_dict
Same as `attrs.fields_dict`.
.. function:: has
Same as `attrs.has`.
.. function:: resolve_types
Same as `attrs.resolve_types`.
.. autofunction:: asdict
.. autofunction:: astuple
.. module:: attr.filters
.. function:: include
Same as `attrs.filters.include`.
.. function:: exclude
Same as `attrs.filters.exclude`.
See :func:`attrs.asdict` for examples.
All objects from `attrs.filters` are also available in ``attr.filters``.
----
.. currentmodule:: attr
.. function:: evolve
Same as `attrs.evolve`.
.. function:: validate
Same as `attrs.validate`.
Validators
----------
.. module:: attr.validators
All objects from `attrs.validators` are also available in ``attr.validators``.
Please refer to the former for details.
Converters
----------
.. module:: attr.converters
All objects from `attrs.converters` are also available from ``attr.converters``.
Please refer to the former for details.
Setters
-------
.. module:: attr.setters
All objects from `attrs.setters` are also available in ``attr.setters``.
Please refer to the former for details.
Deprecated APIs
---------------
.. currentmodule:: attr
To help you write backward compatible code that doesn't throw warnings on modern releases, the ``attr`` module has an ``__version_info__`` attribute as of version 19.2.0.
It behaves similarly to `sys.version_info` and is an instance of `attr.VersionInfo`:
.. autoclass:: VersionInfo
With its help you can write code like this:
>>> if getattr(attr, "__version_info__", (0,)) >= (19, 2):
... cmp_off = {"eq": False}
... else:
... cmp_off = {"cmp": False}
>>> cmp_off == {"eq": False}
True
>>> @attr.s(**cmp_off)
... class C:
... pass
----
.. autofunction:: assoc
Before *attrs* got `attrs.validators.set_disabled` and `attrs.validators.set_disabled`, it had the following APIs to globally enable and disable validators.
They won't be removed, but are discouraged to use:
.. autofunction:: set_run_validators
.. autofunction:: get_run_validators
----
The serious-business aliases used to be called ``attr.attributes`` and ``attr.attr``.
There are no plans to remove them but they shouldn't be used in new code.
|