Skip to content

Commit 32cc097

Browse files
author
iron3oxide
committed
feat(validators._in()): Add default argument "verbose_value_error=False"
This should allow for backwards-compatibility.
1 parent 2f372b7 commit 32cc097

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/attr/validators.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def optional(validator):
288288
@attrs(repr=False, slots=True, hash=True)
289289
class _InValidator:
290290
options = attrib()
291+
verbose_value_error = attrib()
291292

292293
def __call__(self, inst, attr, value):
293294
try:
@@ -296,37 +297,46 @@ def __call__(self, inst, attr, value):
296297
in_options = False
297298

298299
if not in_options:
299-
raise ValueError(
300-
"'{name}' must be in {options!r} (got {value!r})".format(
301-
name=attr.name, options=self.options, value=value
302-
),
303-
attr,
304-
self.options,
305-
value,
306-
)
300+
if not self.verbose_value_error:
301+
raise ValueError(
302+
"'{name}' must be in {options!r} (got {value!r})".format(
303+
name=attr.name, options=self.options, value=value
304+
),
305+
)
306+
else:
307+
raise ValueError(
308+
"'{name}' must be in {options!r} (got {value!r})".format(
309+
name=attr.name, options=self.options, value=value
310+
),
311+
attr,
312+
self.options,
313+
value,
314+
)
307315

308316
def __repr__(self):
309317
return "<in_ validator with options {options!r}>".format(
310318
options=self.options
311319
)
312320

313321

314-
def in_(options):
322+
def in_(options, verbose_value_error=False):
315323
"""
316324
A validator that raises a `ValueError` if the initializer is called
317325
with a value that does not belong in the options provided. The check is
318326
performed using ``value in options``.
319327
320328
:param options: Allowed options.
321329
:type options: list, tuple, `enum.Enum`, ...
330+
:param verbose_value_error: Determines extent of the ValueError signature.
331+
:type verbose_value_error: bool
322332
323-
:raises ValueError: With a human readable error message, the attribute (of
324-
type `attrs.Attribute`), the expected options, and the value it
325-
got.
333+
:raises ValueError: With a human readable error message, and if
334+
verbose_value_error is set to True, the attribute (of type
335+
`attrs.Attribute`), the expected options, and the value it got.
326336
327337
.. versionadded:: 17.1.0
328338
"""
329-
return _InValidator(options)
339+
return _InValidator(options, verbose_value_error)
330340

331341

332342
@attrs(repr=False, slots=False, hash=True)

0 commit comments

Comments
 (0)