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
- Customizing un/structuring of _attrs_ classes, dataclasses, TypedDicts and dict NamedTuples is now possible by using `Annotated[T, override()]` on fields.
20
+
See [here](https://catt.rs/en/stable/customizing.html#using-typing-annotated-t-override) for more details.
Copy file name to clipboardExpand all lines: docs/customizing.md
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -410,6 +410,42 @@ ClassWithInitFalse(number=2)
410
410
411
411
```
412
412
413
+
## Using `typing.Annotated[T, override(...)]`
414
+
415
+
The un/structuring process for _attrs_ classes, dataclasses, TypedDicts anddict NamedTuples can be customized by annotating the fields using `typing.Annotated[T, override()]`.
These customizations are automatically recognized by every {class}`Converter <cattrs.Converter>`.
431
+
They can still be overriden explicitly, see [](#custom-un-structuring-hooks).
432
+
433
+
```{attention}
434
+
One of the fundamental [design decisions](why.md#design-decisions) of _cattrs_ is that serialization rules should be separate from the models themselves;
435
+
by using this feature you're going against the spirit of this design decision.
436
+
437
+
However, software is written in many different context andwith different constraints; and practicality _sometimes_ beats purity.
438
+
_Sometimes_, it's not worth introducing a mapping layer to rename one field.
439
+
_Sometimes_, you're busy prototyping and will clean up your code later.
440
+
441
+
The danger isnotany single compromise, but their accumulation.
442
+
One of the most important skills as a software engineer is knowing when the cost of a trade-off has crossed the line and it's time to do things properly.
Copy file name to clipboardExpand all lines: docs/defaulthooks.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -452,11 +452,11 @@ Tuples can be structured into classes using {meth}`structure_attrs_fromtuple() <
452
452
A(a='string', b=2)
453
453
```
454
454
455
-
Loading from tuples can be made the default by creating a new {class}`Converter <cattrs.Converter>`with`unstruct_strat=cattr.UnstructureStrategy.AS_TUPLE`.
455
+
Loading from tuples can be made the default by creating a new {class}`Converter <cattrs.Converter>`with`unstruct_strat=cattrs.UnstructureStrategy.AS_TUPLE`.
@@ -620,6 +620,12 @@ The {mod}`cattrs.cols` module contains hook factories for un/structuring named t
620
620
621
621
[PEP593](https://www.python.org/dev/peps/pep-0593/) annotations (`typing.Annotated[type, ...]`) are supported and are handled using the first type present in the annotated type.
622
622
623
+
Additionally, `typing.Annotated` types containing `cattrs.override()` are recognized and used by the _attrs_, dataclass, TypedDict anddict NamedTuple hook factories.
624
+
625
+
```{versionchanged} NEXT
626
+
`Annotated[T, override()]`is now used by the _attrs_, dataclass, TypedDict anddict NamedTuple hook factories.
0 commit comments