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
- Python 3.8 is no longer supported, as it is end-of-life. Use previous versions on this Python version.
25
-
- Change type of Converter.__init__.unstruct_collection_overrides from Callable to Mapping[type, UnstructureHook] ([#594](https://github.com/python-attrs/cattrs/pull/594).
Named tuples can be un/structured using dictionaries using the {meth}`namedtuple_dict_structure_factory <cattrs.cols.namedtuple_dict_structure_factory>`
236
-
and {meth}`namedtuple_dict_unstructure_factory <cattrs.cols.namedtuple_dict_unstructure_factory>`
237
-
hook factories.
238
-
239
-
To unstructure _all_ named tuples into dictionaries:
<function namedtuple_dict_unstructure_factory at ...>
249
-
250
-
>>>class MyNamedTuple(NamedTuple):
251
-
... a: int
252
-
253
-
>>> c.unstructure(MyNamedTuple(1))
254
-
{'a': 1}
255
-
```
256
-
257
-
To only un/structure _some_ named tuples into dictionaries,
258
-
change the predicate function when registering the hook factory:
259
-
260
-
```{doctest} namedtuples
261
-
:options: +ELLIPSIS
262
-
263
-
>>> c.register_unstructure_hook_factory(
264
-
...lambdat: t is MyNamedTuple,
265
-
... namedtuple_dict_unstructure_factory,
266
-
... )
267
-
<function namedtuple_dict_unstructure_factory at ...>
268
-
```
269
-
270
-
## Using `cattrs.gen` Generators
271
-
272
-
The {mod}`cattrs.gen` module allows for generating and compiling specialized hooks for unstructuring _attrs_ classes, dataclasses and typed dicts.
160
+
The {mod}`cattrs.gen` module contains [hook factories](#hook-factories) for un/structuring _attrs_ classes, dataclasses and typed dicts.
273
161
The default {class}`Converter <cattrs.Converter>`, upon first encountering one of these types,
274
-
will use the generation functions mentioned here to generate specialized hooks for it,
162
+
will use the hook factories mentioned here to generate specialized hooks for it,
275
163
register the hooks and use them.
276
164
277
165
One reason for generating these hooks in advance is that they can bypass a lot of _cattrs_ machinery and be significantly faster than normal _cattrs_.
278
-
The hooks are also good building blocks for more complex customizations.
166
+
The hook factories are also good building blocks for more complex customizations.
279
167
280
168
Another reason is overriding behavior on a per-attribute basis.
281
169
282
-
Currently, the overrides only support generating dictionary un/structuring hooks (as opposed to tuples), and support `omit_if_default`, `forbid_extra_keys`, `rename`and`omit`.
170
+
Currently, the overrides only support generating dictionary un/structuring hooks (as opposed to tuples),
171
+
and support `omit_if_default`, `forbid_extra_keys`, `rename`and`omit`.
Named tuples can be un/structured using dictionaries using the {meth}`namedtuple_dict_structure_factory <cattrs.cols.namedtuple_dict_structure_factory>`
464
+
and {meth}`namedtuple_dict_unstructure_factory <cattrs.cols.namedtuple_dict_unstructure_factory>`
465
+
hook factories.
466
+
467
+
To unstructure _all_ named tuples into dictionaries:
can be structured by default if they can be initialized using their value type hint.
190
+
Supported types are:
191
+
192
+
-`collections.defaultdict[K, V]`
193
+
-`typing.DefaultDict[K, V]`
194
+
195
+
For example, `defaultdict[str, int]` works since _cattrs_ will initialize it with`defaultdict(int)`.
196
+
197
+
This also means `defaultdicts` without key and value annotations (bare `defaultdicts`) cannot be structured by default.
198
+
199
+
`defaultdicts`with arbitrary default factories can be structured by using {meth}`defaultdict_structure_factory <cattrs.cols.defaultdict_structure_factory>`:
defaultdict(<function <lambda> at ...>, {'key': 1})
214
+
```
215
+
216
+
`defaultdicts` are unstructured into plain dictionaries.
217
+
218
+
```{note}
219
+
`defaultdicts` are not supported by the BaseConverter.
220
+
```
221
+
222
+
```{versionadded} 24.2.0
223
+
224
+
```
225
+
186
226
### Virtual Subclasses of [`abc.Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping) and [`abc.MutableMapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableMapping)
187
227
188
228
If a class declares itself a virtual subclass of `collections.abc.Mapping`or`collections.abc.MutableMapping`and its initializer accepts a dictionary,
0 commit comments