You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Spike `alias` implementation.
* Move default alias init to after field_transformer.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fixup docs.
* Update docs/extending.rst
* Pre-commit fixes
* Partially fix doctest
* Add test docstrings.
* Add typing_example tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Tidy typing_example
* Add note in init.rst on private aliases
* Add alias example to examples.rst
* Assert to comment
* Add changelog entry
* Fixup doc error
* Tidy dataclass_transform docs
* Lil' spice for the changelog.
* Fix doctest
* Update extending.rst
* Make alias introspection more explicit
* Update src/attr/_make.py
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
``attrs.field`` now supports an ``alias`` option for explicit ``__init__`` argument names.
2
+
3
+
Get ``__init__`` signatures matching any taste, peculiar or plain!
4
+
The `PEP 681 compatible <https://peps.python.org/pep-0681/#field-specifier-parameters>`_ ``alias`` option can be use to override private attribute name mangling,
5
+
or add other arbitrary field argument name overrides.
Or, perhaps you would prefer to generate dataclass-compatible ``__init__`` signatures via a default field ``alias``.
264
+
Note, ``field_transformer`` operates on `attrs.Attribute` instances before the default private-attribute handling is applied so explicit user-provided aliases can be detected.
Copy file name to clipboardExpand all lines: docs/init.rst
+17-2Lines changed: 17 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,9 +47,10 @@ Embrace functions and classmethods as a filter between reality and what's best f
47
47
48
48
If you look for powerful-yet-unintrusive serialization and validation for your ``attrs`` classes, have a look at our sibling project `cattrs <https://cattrs.readthedocs.io/>`_ or our `third-party extensions <https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs>`_.
49
49
50
+
.. _private_attributes:
50
51
51
-
Private Attributes
52
-
------------------
52
+
Private Attributes and Aliases
53
+
------------------------------
53
54
54
55
One thing people tend to find confusing is the treatment of private attributes that start with an underscore.
55
56
``attrs`` follows the doctrine that `there is no such thing as a private argument`_ and strips the underscores from the name when writing the ``__init__`` method signature:
@@ -78,6 +79,20 @@ But it's important to be aware of it because it can lead to surprising syntax er
78
79
79
80
In this case a valid attribute name ``_1`` got transformed into an invalid argument name ``1``.
80
81
82
+
If your taste differs, you can use the ``alias`` argument to `attrs.field` to explicitly set the argument name.
83
+
This can be used to override private attribute handling, or make other arbitrary changes to ``__init__`` argument names.
0 commit comments