Skip to content

Commit

Permalink
Rework example of or_ validator in api/docs.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
Libor committed Jul 17, 2024
1 parent c9da05f commit a0b6b24
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -466,18 +466,23 @@ All objects from ``attrs.validators`` are also available from ``attr.validators`

.. doctest::

>>> from typing import Union
>>> from typing import List, Union
>>> @define
... class C:
... val: Union[int, str] = field(validator=attrs.validators.or_(attrs.validators.instance_of(int), attrs.validators.instance_of(str)))
... val: Union[int, List[int]] = field(
... validator=attrs.validators.or_(
... attrs.validators.instance_of(int),
... attrs.validators.deep_iterable(attrs.validators.instance_of(int)),
... )
... )
>>> C(42)
C(val=42)
>>> C("42")
C(val='42')
>>> C(3.14)
>>> C([1, 2, 3])
C(val=[1, 2, 3])
>>> C(val='42')
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
ValueError: None of (<instance_of validator for type <class 'int'>>, <deep_iterable validator for iterables of <instance_of validator for type <class 'int'>>>) satisfied for value '42'

.. autofunction:: attrs.validators.not_

Expand Down

0 comments on commit a0b6b24

Please sign in to comment.