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
Copy file name to clipboardExpand all lines: HISTORY.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
22
22
-[`typing.Self`](https://docs.python.org/3/library/typing.html#typing.Self) is now supported in _attrs_ classes, dataclasses, TypedDicts and the dict NamedTuple factories.
23
23
See [`typing.Self`](https://catt.rs/en/latest/defaulthooks.html#typing-self) for details.
Copy file name to clipboardExpand all lines: docs/basics.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,8 @@
3
3
```
4
4
5
5
All _cattrs_ functionality is exposed through a {class}`cattrs.Converter` object.
6
-
A global converter is provided for convenience as {data}`cattrs.global_converter` but more complex customizations should be performed on private instances, any number of which can be made.
6
+
A global converter is provided for convenience as {data}`cattrs.global_converter`
7
+
but more complex customizations should be performed on private instances, any number of which can be made.
Copy file name to clipboardExpand all lines: docs/customizing.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,6 @@ the hook for `int`, which should be already present.
19
19
## Custom (Un-)structuring Hooks
20
20
21
21
You can write your own structuring and unstructuring functions and register them for types using {meth}`Converter.register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` and {meth}`Converter.register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>`.
22
-
This approach is the most flexible but also requires the most amount of boilerplate.
23
22
24
23
{meth}`register_structure_hook() <cattrs.BaseConverter.register_structure_hook>` and {meth}`register_unstructure_hook() <cattrs.BaseConverter.register_unstructure_hook>` use a Python [_singledispatch_](https://docs.python.org/3/library/functools.html#functools.singledispatch) under the hood.
25
24
_singledispatch_ is powerful and fast but comes with some limitations; namely that it performs checks using `issubclass()` which doesn't work with many Python types.
@@ -30,10 +29,15 @@ Some examples of this are:
30
29
- generics (`MyClass[int]` is not a _subclass_ of `MyClass`)
31
30
- protocols, unless they are `runtime_checkable`
32
31
- various modifiers, such as `Final` and `NotRequired`
33
-
- newtypes and 3.12 type aliases
34
32
-`typing.Annotated`
35
33
36
-
... and many others. In these cases, predicate functions should be used instead.
34
+
... and many others. In these cases, [predicate hooks](#predicate-hooks) should be used instead.
35
+
36
+
Even though unions, [newtypes](https://docs.python.org/3/library/typing.html#newtype)
37
+
and [modern type aliases](https://docs.python.org/3/library/typing.html#type-aliases)
38
+
do not work with _singledispatch_,
39
+
these methods have special support for these type forms and can be used with them.
40
+
Instead of using _singledispatch_, predicate hooks will automatically be used instead.
0 commit comments