Skip to content

Commit 5449232

Browse files
authored
Small docs fix (#697)
1 parent 2ec13dd commit 5449232

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

docs/customizing.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Currently, the overrides only support generating dictionary un/structuring hooks
191191
and support `omit_if_default`, `forbid_extra_keys`, `rename` and `omit`.
192192

193193
### `omit_if_default`
194+
```{currentmodule} cattrs.gen
195+
```
194196

195197
This override can be applied on a per-class or per-attribute basis.
196198
The generated unstructuring hook will skip unstructuring values that are equal to their default or factory values.
@@ -205,7 +207,10 @@ The generated unstructuring hook will skip unstructuring values that are equal t
205207
... b: dict = Factory(dict)
206208

207209
>>> c = cattrs.Converter()
208-
>>> c.register_unstructure_hook(WithDefault, make_dict_unstructure_fn(WithDefault, c, b=override(omit_if_default=True)))
210+
>>> c.register_unstructure_hook(
211+
... WithDefault,
212+
... make_dict_unstructure_fn(WithDefault, c, b=override(omit_if_default=True)),
213+
... )
209214
>>> c.unstructure(WithDefault(1))
210215
{'a': 1}
211216
```
@@ -216,7 +221,7 @@ For example, consider a class with a `dateTime` field and a factory for it: skip
216221
So we apply the `omit_if_default` rule to the class, but not to the `dateTime` field.
217222

218223
```{note}
219-
The parameter to `make_dict_unstructure_function` is named ``_cattrs_omit_if_default`` instead of just ``omit_if_default`` to avoid potential collisions with an override for a field named ``omit_if_default``.
224+
The parameter to {meth}`make_dict_unstructure_fn` is named ``_cattrs_omit_if_default`` instead of just ``omit_if_default`` to avoid potential collisions with an override for a field named ``omit_if_default``.
220225
```
221226

222227
```{doctest}
@@ -226,11 +231,16 @@ So we apply the `omit_if_default` rule to the class, but not to the `dateTime` f
226231

227232
>>> @define
228233
... class TestClass:
229-
... a: Optional[int] = None
234+
... a: int | None = None
230235
... b: datetime = Factory(datetime.utcnow)
231236

232237
>>> c = cattrs.Converter()
233-
>>> hook = make_dict_unstructure_fn(TestClass, c, _cattrs_omit_if_default=True, b=override(omit_if_default=False))
238+
>>> hook = make_dict_unstructure_fn(
239+
... TestClass,
240+
... c,
241+
... _cattrs_omit_if_default=True,
242+
... b=override(omit_if_default=False)
243+
... )
234244
>>> c.register_unstructure_hook(TestClass, hook)
235245
>>> c.unstructure(TestClass())
236246
{'b': ...}
@@ -283,7 +293,7 @@ This is useful if an attribute name is a reserved keyword in Python.
283293

284294
>>> @define
285295
... class ExampleClass:
286-
... klass: Optional[int]
296+
... klass: int | None
287297

288298
>>> c = cattrs.Converter()
289299
>>> unst_hook = make_dict_unstructure_fn(ExampleClass, c, klass=override(rename="class"))

0 commit comments

Comments
 (0)