Skip to content

Commit

Permalink
Add description of attrs.validators.or_ to docs/api.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
bibajz committed Jul 14, 2024
1 parent 8d12ad4 commit 2ad53b3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,25 @@ All objects from ``attrs.validators`` are also available from ``attr.validators`
x = field(validator=attrs.validators.and_(v1, v2, v3))
x = field(validator=[v1, v2, v3])

.. autofunction:: attrs.validators.or_

For example:

.. doctest::

>>> from typing import Union
>>> @define
... class C:
... val: Union[int, str] = field(validator=attrs.validators.or_(attrs.validators.instance_of(int), attrs.validators.instance_of(str)))
>>> C(42)
C(val=42)
>>> C("42")
C(val="42")
>>> C(3.14)
Traceback (most recent call last):
...
ValueError: None of (<instance_of validator for type <class 'int'>>, <instance_of validator for type <class 'str'>>) satisfied for value 3.14

.. autofunction:: attrs.validators.not_

For example:
Expand Down

0 comments on commit 2ad53b3

Please sign in to comment.