Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move conditional clause to beginning for validator/converter docs. #1213

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ Traceback (most recent call last):
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')
```

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.
If using the old-school {func}`attr.s` decorator, validators only run on initialization by default.
If using the newer {func}`attrs.define` and friends, validators run on initialization *and* on attribute setting.
This behavior can be changed using the *on_setattr* argument.

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

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

Check out {ref}`converters` for more details.
Expand Down
3 changes: 3 additions & 0 deletions docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ This can be useful for doing type-conversions on values that you don't want to f
>>> o = C("1")
>>> o.x
1
>>> o.x = "2"
>>> o.x
2
```

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