Skip to content

Commit 2257a0c

Browse files
authored
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.
1 parent 985e8ef commit 2257a0c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

changelog.d/951.change.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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().

src/attr/validators.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ def __call__(self, inst, attr, value):
299299
raise ValueError(
300300
"'{name}' must be in {options!r} (got {value!r})".format(
301301
name=attr.name, options=self.options, value=value
302-
)
302+
),
303+
attr,
304+
self.options,
305+
value,
303306
)
304307

305308
def __repr__(self):

tests/test_validators.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,12 @@ def test_fail(self):
471471
a = simple_attr("test")
472472
with pytest.raises(ValueError) as e:
473473
v(None, a, None)
474-
assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args
474+
assert (
475+
"'test' must be in [1, 2, 3] (got None)",
476+
a,
477+
[1, 2, 3],
478+
None,
479+
) == e.value.args
475480

476481
def test_fail_with_string(self):
477482
"""
@@ -482,7 +487,12 @@ def test_fail_with_string(self):
482487
a = simple_attr("test")
483488
with pytest.raises(ValueError) as e:
484489
v(None, a, None)
485-
assert ("'test' must be in 'abc' (got None)",) == e.value.args
490+
assert (
491+
"'test' must be in 'abc' (got None)",
492+
a,
493+
"abc",
494+
None,
495+
) == e.value.args
486496

487497
def test_repr(self):
488498
"""

0 commit comments

Comments
 (0)