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
* Initial work on decorators
* Docs
* More decorators
* exclude_also?
* Enable hook factories to take converters
* Fix lint
* Docs
* Tweak docs some more
Copy file name to clipboardExpand all lines: docs/customizing.md
+73-3Lines changed: 73 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,44 @@ Some examples of this are:
17
17
* protocols, unless they are `runtime_checkable`
18
18
* various modifiers, such as `Final` and `NotRequired`
19
19
* newtypes and 3.12 type aliases
20
+
*`typing.Annotated`
20
21
21
22
... and many others. In these cases, predicate functions should be used instead.
22
23
24
+
### Use as Decorators
25
+
26
+
{meth}`register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` and {meth}`register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>` can also be used as _decorators_.
27
+
When used this way they behave a little differently.
28
+
29
+
{meth}`register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` will inspect the return type of the hook and register the hook for that type.
30
+
31
+
```python
32
+
@converter.register_structure_hook
33
+
defmy_int_hook(val: Any, _) -> int:
34
+
"""This hook will be registered for `int`s."""
35
+
returnint(val)
36
+
```
37
+
38
+
{meth}`register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>` will inspect the type of the first argument and register the hook for that type.
39
+
40
+
```python
41
+
from datetime import datetime
42
+
43
+
@converter.register_unstructure_hook
44
+
defmy_datetime_hook(val: datetime) -> str:
45
+
"""This hook will be registered for `datetime`s."""
46
+
return val.isoformat()
47
+
```
48
+
49
+
The non-decorator approach is still recommended when dealing with lambdas, hooks produced elsewhere, unannotated hooks and situations where type introspection doesn't work.
50
+
51
+
```{versionadded} 24.1.0
52
+
```
53
+
23
54
### Predicate Hooks
24
55
25
-
A predicate is a function that takes a type and returns true or false, depending on whether the associated hook can handle the given type.
56
+
A _predicate_ is a function that takes a type and returns true or false
57
+
depending on whether the associated hook can handle the given type.
26
58
27
59
The {meth}`register_unstructure_hook_func() <cattrs.BaseConverter.register_unstructure_hook_func>` and {meth}`register_structure_hook_func() <cattrs.BaseConverter.register_structure_hook_func>` are used
28
60
to link un/structuring hooks to arbitrary types. These hooks are then called _predicate hooks_, and are very powerful.
@@ -64,9 +96,11 @@ Here's an example showing how to use hook factories to apply the `forbid_extra_k
0 commit comments