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
-[_attrs_ aliases](https://www.attrs.org/en/stable/init.html#private-attributes-and-aliases) are now supported, although aliased fields still map to their attribute name instead of their alias by default when un/structuring.
Copy file name to clipboardExpand all lines: docs/customizing.md
+39-24Lines changed: 39 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,27 @@
1
-
# Customizing class un/structuring
1
+
# Customizing Class Un/structuring
2
2
3
-
This section deals with customizing the unstructuring and structuring processes in `cattrs`.
3
+
This section deals with customizing the unstructuring and structuring processes in _cattrs_.
4
4
5
-
## Using `cattr.Converter`
5
+
## Using `cattrs.Converter`
6
6
7
-
The default `Converter`, upon first encountering an `attrs` class, will use
8
-
the generation functions mentioned here to generate the specialized hooks for it,
9
-
register the hooks and use them.
7
+
The default {class}`Converter <cattrs.Converter>`, upon first encountering an _attrs_ class, will use the generation functions mentioned here to generate the specialized hooks for it, register the hooks and use them.
10
8
11
9
## Manual un/structuring hooks
12
10
13
11
You can write your own structuring and unstructuring functions and register
14
-
them for types using {meth}`Converter.register_structure_hook <cattrs.BaseConverter.register_structure_hook>` and
15
-
{meth}`Converter.register_unstructure_hook <cattrs.BaseConverter.register_unstructure_hook>`. This approach is the most
12
+
them for types using {meth}`Converter.register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` and
13
+
{meth}`Converter.register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>`. This approach is the most
16
14
flexible but also requires the most amount of boilerplate.
17
15
18
16
## Using `cattrs.gen` generators
19
17
20
-
`cattrs` includes a module, {mod}`cattrs.gen`, which allows for generating and
21
-
compiling specialized functions for unstructuring `attrs` classes.
18
+
_cattrs_ includes a module, {mod}`cattrs.gen`, which allows for generating and compiling specialized functions for unstructuring _attrs_ classes.
22
19
23
-
One reason for generating these functions in advance is that they can bypass
24
-
a lot of `cattrs` machinery and be significantly faster than normal `cattrs`.
20
+
One reason for generating these functions in advance is that they can bypass a lot of _cattrs_ machinery and be significantly faster than normal _cattrs_.
25
21
26
22
Another reason is that it's possible to override behavior on a per-attribute basis.
27
23
28
-
Currently, the overrides only support generating dictionary un/structuring functions
29
-
(as opposed to tuples), and support `omit_if_default`, `forbid_extra_keys`,
30
-
`rename` and `omit`.
24
+
Currently, the overrides only support generating dictionary un/structuring functions (as opposed to tuples), and support `omit_if_default`, `forbid_extra_keys`, `rename` and `omit`.
31
25
32
26
### `omit_if_default`
33
27
@@ -82,13 +76,10 @@ This override has no effect when generating structuring functions.
82
76
83
77
### `forbid_extra_keys`
84
78
85
-
By default `cattrs` is lenient in accepting unstructured input. If extra
86
-
keys are present in a dictionary, they will be ignored when generating a
87
-
structured object. Sometimes it may be desirable to enforce a stricter
88
-
contract, and to raise an error when unknown keys are present - in particular
89
-
when fields have default values this may help with catching typos.
90
-
`forbid_extra_keys` can also be enabled (or disabled) on a per-class basis when
91
-
creating structure hooks with `make_dict_structure_fn`.
79
+
By default _cattrs_ is lenient in accepting unstructured input.
80
+
If extra keys are present in a dictionary, they will be ignored when generating a structured object.
81
+
Sometimes it may be desirable to enforce a stricter contract, and to raise an error when unknown keys are present - in particular when fields have default values this may help with catching typos.
82
+
`forbid_extra_keys` can also be enabled (or disabled) on a per-class basis when creating structure hooks with {py:func}`make_dict_structure_fn() <cattrs.gen.make_dict_structure_fn>`.
92
83
93
84
```{doctest}
94
85
:options: +SKIP
@@ -110,8 +101,7 @@ ForbiddenExtraKeyError: Extra fields in constructor for TestClass: nummber
110
101
TestClass(number=1)
111
102
```
112
103
113
-
This behavior can only be applied to classes or to the default for the
114
-
`Converter`, and has no effect when generating unstructuring functions.
104
+
This behavior can only be applied to classes or to the default for the {class}`Converter <cattrs.Converter>`, and has no effect when generating unstructuring functions.
115
105
116
106
### `rename`
117
107
@@ -183,3 +173,28 @@ This process can be overriden by passing in the desired un/structure manually.
183
173
>>> c.structure({"an_int": 1}, ExampleClass)
184
174
ExampleClass(an_int=2)
185
175
```
176
+
177
+
### `use_alias`
178
+
179
+
By default, fields are un/structured to and from dictionary keys exactly matching the field names.
180
+
_attrs_ classes support field aliases, which override the `__init__` parameter name for a given field.
181
+
By generating your un/structure function with `_cattrs_use_alias=True`, _cattrs_ will use the field alias instead of the field name as the un/structured dictionary key.
0 commit comments