Skip to content

Commit e3dbd4f

Browse files
committed
Add narrative docs
1 parent ea8bef5 commit e3dbd4f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

docs/init.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ A converter will override an explicit type annotation or `type` argument.
361361
{'return': None, 'x': <class 'str'>}
362362
```
363363

364+
If you need more control over the conversion process, you can wrap the converter with a {class}`attrs.Converter` and ask for the instance and/or the field that are being initialized:
365+
366+
```{doctest}
367+
>>> def complicated(value, self_, field):
368+
... return int(value) * self_.factor + field.metadata["offset"]
369+
>>> @define
370+
... class C:
371+
... factor = 5 # not an *attrs* field
372+
... x = field(
373+
... metadata={"offset": 200},
374+
... converter=attrs.Converter(
375+
... complicated,
376+
... takes_self=True, takes_field=True
377+
... ))
378+
>>> C("42")
379+
C(x=410)
380+
```
381+
364382

365383
## Hooking Yourself Into Initialization
366384

0 commit comments

Comments
 (0)