Skip to content

Commit

Permalink
Polish docs around #951
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Jun 13, 2022
1 parent 2257a0c commit a7e82b5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 1 addition & 3 deletions changelog.d/951.change.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
The error signature of _InValidator has been completed,
it now includes the attribute,
options and value as proclaimed in the docstring of _in().
``attrs.validators._in()``'s ``ValueError`` is not missing the attribute, expected options, and the value it got anymore.
36 changes: 18 additions & 18 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,24 @@ All objects from ``attrs.validators`` are also available from ``attr.validators`

.. doctest::

>>> import enum
>>> class State(enum.Enum):
... ON = "on"
... OFF = "off"
>>> @attrs.define
... class C:
... state = attrs.field(validator=attrs.validators.in_(State))
... val = attrs.field(validator=attrs.validators.in_([1, 2, 3]))
>>> C(State.ON, 1)
C(state=<State.ON: 'on'>, val=1)
>>> C("on", 1)
Traceback (most recent call last):
...
ValueError: 'state' must be in <enum 'State'> (got 'on')
>>> C(State.ON, 4)
Traceback (most recent call last):
...
ValueError: 'val' must be in [1, 2, 3] (got 4)
>>> import enum
>>> class State(enum.Enum):
... ON = "on"
... OFF = "off"
>>> @attrs.define
... class C:
... state = attrs.field(validator=attrs.validators.in_(State))
... val = attrs.field(validator=attrs.validators.in_([1, 2, 3]))
>>> C(State.ON, 1)
C(state=<State.ON: 'on'>, val=1)
>>> C("on", 1)
Traceback (most recent call last):
...
ValueError: 'state' must be in <enum 'State'> (got 'on'), Attribute(name='state', default=NOTHING, validator=<in_ validator with options <enum 'State'>>, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None, kw_only=False, inherited=False, on_setattr=None), <enum 'State'>, 'on')
>>> C(State.ON, 4)
Traceback (most recent call last):
...
ValueError: 'val' must be in [1, 2, 3] (got 4), Attribute(name='val', default=NOTHING, validator=<in_ validator with options [1, 2, 3]>, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None, kw_only=False, inherited=False, on_setattr=None), [1, 2, 3], 4)

.. autofunction:: attrs.validators.provides

Expand Down
4 changes: 4 additions & 0 deletions src/attr/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ def in_(options):
got.
.. versionadded:: 17.1.0
.. versionchanged:: 22.1.0
The ValueError was incomplete until now and only contained the human
readable error message. Now it contains all the information that has
been promised since 17.1.0.
"""
return _InValidator(options)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,10 @@ def test_fail(self):
"""
v = in_([1, 2, 3])
a = simple_attr("test")

with pytest.raises(ValueError) as e:
v(None, a, None)

assert (
"'test' must be in [1, 2, 3] (got None)",
a,
Expand Down

0 comments on commit a7e82b5

Please sign in to comment.