Skip to content

Commit a7e82b5

Browse files
committed
Polish docs around #951
1 parent 2257a0c commit a7e82b5

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

changelog.d/951.change.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
The error signature of _InValidator has been completed,
2-
it now includes the attribute,
3-
options and value as proclaimed in the docstring of _in().
1+
``attrs.validators._in()``'s ``ValueError`` is not missing the attribute, expected options, and the value it got anymore.

docs/api.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -566,24 +566,24 @@ All objects from ``attrs.validators`` are also available from ``attr.validators`
566566

567567
.. doctest::
568568

569-
>>> import enum
570-
>>> class State(enum.Enum):
571-
... ON = "on"
572-
... OFF = "off"
573-
>>> @attrs.define
574-
... class C:
575-
... state = attrs.field(validator=attrs.validators.in_(State))
576-
... val = attrs.field(validator=attrs.validators.in_([1, 2, 3]))
577-
>>> C(State.ON, 1)
578-
C(state=<State.ON: 'on'>, val=1)
579-
>>> C("on", 1)
580-
Traceback (most recent call last):
581-
...
582-
ValueError: 'state' must be in <enum 'State'> (got 'on')
583-
>>> C(State.ON, 4)
584-
Traceback (most recent call last):
585-
...
586-
ValueError: 'val' must be in [1, 2, 3] (got 4)
569+
>>> import enum
570+
>>> class State(enum.Enum):
571+
... ON = "on"
572+
... OFF = "off"
573+
>>> @attrs.define
574+
... class C:
575+
... state = attrs.field(validator=attrs.validators.in_(State))
576+
... val = attrs.field(validator=attrs.validators.in_([1, 2, 3]))
577+
>>> C(State.ON, 1)
578+
C(state=<State.ON: 'on'>, val=1)
579+
>>> C("on", 1)
580+
Traceback (most recent call last):
581+
...
582+
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')
583+
>>> C(State.ON, 4)
584+
Traceback (most recent call last):
585+
...
586+
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)
587587

588588
.. autofunction:: attrs.validators.provides
589589

src/attr/validators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ def in_(options):
325325
got.
326326
327327
.. versionadded:: 17.1.0
328+
.. versionchanged:: 22.1.0
329+
The ValueError was incomplete until now and only contained the human
330+
readable error message. Now it contains all the information that has
331+
been promised since 17.1.0.
328332
"""
329333
return _InValidator(options)
330334

tests/test_validators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,10 @@ def test_fail(self):
469469
"""
470470
v = in_([1, 2, 3])
471471
a = simple_attr("test")
472+
472473
with pytest.raises(ValueError) as e:
473474
v(None, a, None)
475+
474476
assert (
475477
"'test' must be in [1, 2, 3] (got None)",
476478
a,

0 commit comments

Comments
 (0)