Skip to content

Commit 7305bb7

Browse files
committed
Doc improvements
1 parent 959d2d3 commit 7305bb7

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ strategies
1414
validation
1515
preconf
1616
unions
17+
history
1718
benchmarking
1819
contributing
19-
history
20+
API <modules>
2021
```
2122

2223
```{include} ../README.md

docs/structuring.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ to deques are:
154154
- `Deque[T]`
155155
- `deque[T]`
156156

157-
In all cases, a new **unbounded** deque (`maxlen=None`) will be returned,
157+
In all cases, a new **unbounded** deque (`maxlen=None`) will be returned,
158158
so this operation can be used to copy an iterable into a deque.
159159
If you want to convert into bounded `deque`, registering a custom structuring hook is a good approach.
160160

@@ -173,6 +173,7 @@ deque(['1', None, '3'])
173173
```
174174

175175
```{versionadded} 23.1.0
176+
176177
```
177178

178179
### Sets and Frozensets
@@ -384,10 +385,10 @@ Another option is to use a custom tagged union strategy (see [Strategies - Tagge
384385
[PEP 593](https://www.python.org/dev/peps/pep-0593/) annotations (`typing.Annotated[type, ...]`) are supported and are
385386
matched using the first type present in the annotated type.
386387
387-
### `typing.NewType`
388+
## `typing.NewType`
388389
389390
[NewTypes](https://docs.python.org/3/library/typing.html#newtype) are supported and are structured according to the rules for their underlying type.
390-
Their hooks can also be overriden using :py:attr:`cattrs.Converter.register_structure_hook`.
391+
Their hooks can also be overriden using {meth}`Converter.register_structure_hook() <cattrs.BaseConverter.register_structure_hook>`.
391392
392393
```{doctest}
393394

docs/unions.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
# Tips for handling unions
1+
# Tips for Handling Unions
22

33
This sections contains information for advanced union handling.
44

5-
As mentioned in the structuring section, _cattrs_ is able to handle simple
6-
unions of _attrs_ classes automatically. More complex cases require
7-
converter customization (since there are many ways of handling unions).
5+
As mentioned in the structuring section, _cattrs_ is able to handle simple unions of _attrs_ classes automatically.
6+
More complex cases require converter customization (since there are many ways of handling unions).
7+
8+
_cattrs_ also comes with a number of strategies to help handle unions:
9+
10+
- [tagged unions strategy](strategies.md#tagged-unions-strategy) mentioned below
11+
- [union passthrough strategy](strategies.md#union-passthrough), which is preapplied to all the [preconfigured](preconf.md) converters
812

913
## Unstructuring unions with extra metadata
1014

src/cattrs/converters.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,10 @@ def register_unstructure_hook_factory(
279279
"""
280280
Register a hook factory for a given predicate.
281281
282-
A predicate is a function that, given a type, returns whether the factory
283-
can produce a hook for that type.
284-
285-
A factory is a callable that, given a type, produces an unstructuring
286-
hook for that type. This unstructuring hook will be cached.
282+
:param predicate: A function that, given a type, returns whether the factory
283+
can produce a hook for that type.
284+
:param factory: A callable that, given a type, produces an unstructuring
285+
hook for that type. This unstructuring hook will be cached.
287286
"""
288287
self._unstructure_func.register_func_list([(predicate, factory, True)])
289288

@@ -326,11 +325,10 @@ def register_structure_hook_factory(
326325
"""
327326
Register a hook factory for a given predicate.
328327
329-
A predicate is a function that, given a type, returns whether the factory
330-
can produce a hook for that type.
331-
332-
A factory is a callable that, given a type, produces a structuring
333-
hook for that type. This structuring hook will be cached.
328+
:param predicate: A function that, given a type, returns whether the factory
329+
can produce a hook for that type.
330+
:param factory: A callable that, given a type, produces a structuring
331+
hook for that type. This structuring hook will be cached.
334332
"""
335333
self._structure_func.register_func_list([(predicate, factory, True)])
336334

0 commit comments

Comments
 (0)