Skip to content

Commit 5aea241

Browse files
BrianPughhynek
andauthored
Move conditional clause to beginning for validator/converter docs. (#1213)
Move conditional clause to beginning for validator/converter docs. Provide converter example. Co-authored-by: Hynek Schlawack <hs@ox.cx>
1 parent 4c2b9e3 commit 5aea241

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

docs/examples.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ Traceback (most recent call last):
467467
TypeError: ("'x' must be <type 'int'> (got '42' that is a <type 'str'>).", Attribute(name='x', default=NOTHING, factory=NOTHING, validator=<instance_of validator for type <type 'int'>>, type=None, kw_only=False), <type 'int'>, '42')
468468
```
469469

470-
Please note that if you use {func}`attr.s` (and **not** {func}`attrs.define`) to define your class, validators only run on initialization by default -- not when you set an attribute.
470+
If using the old-school {func}`attr.s` decorator, validators only run on initialization by default.
471+
If using the newer {func}`attrs.define` and friends, validators run on initialization *and* on attribute setting.
471472
This behavior can be changed using the *on_setattr* argument.
472473

473474
Check out {ref}`validators` for more details.
@@ -485,10 +486,13 @@ This can be useful for doing type-conversions on values that you don't want to f
485486
>>> o = C("1")
486487
>>> o.x
487488
1
489+
>>> o.x = "2"
490+
>>> o.x
491+
2
488492
```
489493

490-
Please note that converters only run on initialization when using the old-school {func}`attr.s` decorator.
491-
They do run by default with {func}`attrs.define` and friends.
494+
If using the old-school {func}`attr.s` decorator, converters only run on initialization by default.
495+
If using the newer {func}`attrs.define` and friends, converters run on initialization *and* on attribute setting.
492496
This behavior can be changed using the *on_setattr* argument.
493497

494498
Check out {ref}`converters` for more details.

docs/init.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ This can be useful for doing type-conversions on values that you don't want to f
307307
>>> o = C("1")
308308
>>> o.x
309309
1
310+
>>> o.x = "2"
311+
>>> o.x
312+
2
310313
```
311314

312315
Converters are run *before* validators, so you can use validators to check the final form of the value.

0 commit comments

Comments
 (0)