From 2257a0c60cba6a3cff0841c884981b5b7b99aef8 Mon Sep 17 00:00:00 2001 From: iron3oxide <98779754+iron3oxide@users.noreply.github.com> Date: Mon, 13 Jun 2022 07:57:47 +0200 Subject: [PATCH] Complete error signature of _InValidator (#951) The docstring of _in() says the ValueError would contain these, however it didn't. Adding said arguments enables uniform processing of ValueErrors raised by validators. One might for example want to extract the attr.name and the value that raised the error to show a custom, more user-friendly error message. --- changelog.d/951.change.rst | 3 +++ src/attr/validators.py | 5 ++++- tests/test_validators.py | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 changelog.d/951.change.rst diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst new file mode 100644 index 000000000..24303f6a9 --- /dev/null +++ b/changelog.d/951.change.rst @@ -0,0 +1,3 @@ +The error signature of _InValidator has been completed, +it now includes the attribute, +options and value as proclaimed in the docstring of _in(). diff --git a/src/attr/validators.py b/src/attr/validators.py index 6a1e198f8..3a4e72823 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -299,7 +299,10 @@ def __call__(self, inst, attr, value): raise ValueError( "'{name}' must be in {options!r} (got {value!r})".format( name=attr.name, options=self.options, value=value - ) + ), + attr, + self.options, + value, ) def __repr__(self): diff --git a/tests/test_validators.py b/tests/test_validators.py index f3fe69cc1..3bec62b03 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,7 +471,12 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args + assert ( + "'test' must be in [1, 2, 3] (got None)", + a, + [1, 2, 3], + None, + ) == e.value.args def test_fail_with_string(self): """ @@ -482,7 +487,12 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in 'abc' (got None)",) == e.value.args + assert ( + "'test' must be in 'abc' (got None)", + a, + "abc", + None, + ) == e.value.args def test_repr(self): """