diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst index 24303f6a9..6ded5fe7b 100644 --- a/changelog.d/951.change.rst +++ b/changelog.d/951.change.rst @@ -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. diff --git a/docs/api.rst b/docs/api.rst index 74c482bc3..37392ee49 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -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=, val=1) - >>> C("on", 1) - Traceback (most recent call last): - ... - ValueError: 'state' must be in (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=, val=1) + >>> C("on", 1) + Traceback (most recent call last): + ... + ValueError: 'state' must be in (got 'on'), Attribute(name='state', default=NOTHING, validator=>, 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), , '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=, 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 diff --git a/src/attr/validators.py b/src/attr/validators.py index 3a4e72823..eece517da 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -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) diff --git a/tests/test_validators.py b/tests/test_validators.py index 3bec62b03..51fe2f41e 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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,