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
+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
@@ -8,6 +8,8 @@
8
8
-**Potentially breaking**: {py:func}`cattrs.gen.make_dict_structure_fn` and {py:func}`cattrs.gen.typeddicts.make_dict_structure_fn` will use the values for the `detailed_validation` and `forbid_extra_keys` parameters from the given converter by default now.
9
9
If you're using these functions directly, the old behavior can be restored by passing in the desired values directly.
- Introduce the `use_class_methods` strategy. Learn more [here](https://catt.rs/en/latest/strategies.html#using-class-specific-structure-and-unstructure-methods).
- The `omit` parameter of {py:func}`cattrs.override` is now of type `bool | None` (from `bool`).
12
14
`None` is the new default and means to apply default _cattrs_ handling to the attribute, which is to omit the attribute if it's marked as `init=False`, and keep it otherwise.
13
15
- Fix {py:func}`format_exception() <cattrs.v.format_exception>` parameter working for recursive calls to {py:func}`transform_error <cattrs.transform_error>`.
@@ -39,7 +41,6 @@
39
41
- Add support for `date` to preconfigured converters.
Copy file name to clipboardExpand all lines: README.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
@@ -149,6 +149,7 @@ _cattrs_ is based on a few fundamental design decisions.
149
149
150
150
- Un/structuring rules are separate from the models.
151
151
This allows models to have a one-to-many relationship with un/structuring rules, and to create un/structuring rules for models which you do not own and you cannot change.
152
+
(_cattrs_ can be configured to use un/structuring rules from models using the [`use_class_methods` strategy](https://catt.rs/en/latest/strategies.html#using-class-specific-structure-and-unstructure-methods).)
152
153
- Invent as little as possible; reuse existing ordinary Python instead.
153
154
For example, _cattrs_ did not have a custom exception type to group exceptions until the sanctioned Python [`exceptiongroups`](https://docs.python.org/3/library/exceptions.html#ExceptionGroup).
154
155
A side-effect of this design decision is that, in a lot of cases, when you're solving _cattrs_ problems you're actually learning Python instead of learning _cattrs_.
@@ -163,6 +164,7 @@ A foolish consistency is the hobgoblin of little minds so these decisions can an
163
164
-[Why I use attrs instead of pydantic](https://threeofwands.com/why-i-use-attrs-instead-of-pydantic/)
Copy file name to clipboardExpand all lines: docs/strategies.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -221,6 +221,7 @@ Child2(a=1, b=1)
221
221
```
222
222
223
223
Other customizations available see are (see {py:func}`include_subclasses()<cattrs.strategies.include_subclasses>`):
224
+
224
225
- The exact list of subclasses that should participate to the union with the `subclasses` argument.
225
226
- Attribute overrides that permit the customization of attributes un/structuring like renaming an attribute.
226
227
@@ -259,13 +260,12 @@ Child(a=1, b='foo')
259
260
260
261
```
261
262
262
-
263
-
264
-
### Using Class-Specific Structure and Unstructure Methods
263
+
## Using Class-Specific Structure and Unstructure Methods
265
264
266
265
_Found at {py:func}`cattrs.strategies.use_class_methods`._
267
266
268
-
The following strategy can be applied for both structuring and unstructuring (also simultaneously).
267
+
This strategy allows for un/structuring logic on the models themselves.
268
+
It can be applied for both structuring and unstructuring (also simultaneously).
269
269
270
270
If a class requires special handling for (un)structuring, you can add a dedicated (un)structuring
271
271
method:
@@ -295,11 +295,11 @@ MyClass(a=43)
295
295
{'c': 41}
296
296
```
297
297
298
-
Any class without a `_structure` or `_unstructure` method will use the default strategy for
299
-
structuring or unstructuring, respectively. Feel free to use other names.
298
+
Any class without a `_structure` or `_unstructure` method will use the default strategy for structuring or unstructuring, respectively.
299
+
Feel free to use other names.
300
+
The stategy can be applied multiple times (with different method names).
300
301
301
-
If you want to (un)structured nested objects, just append a converter parameter
302
-
to your (un)structuring methods and you will receive the converter there:
302
+
If you want to (un)structured nested objects, just append a converter parameter to your (un)structuring methods and you will receive the converter there:
0 commit comments