Skip to content

Commit

Permalink
Accept tuples in attrs.validators.optional
Browse files Browse the repository at this point in the history
Fixes #937
  • Loading branch information
hynek committed Apr 5, 2023
1 parent 5a7d978 commit 591a745
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/attr/validators.py
Expand Up @@ -270,15 +270,16 @@ def optional(validator):
which can be set to ``None`` in addition to satisfying the requirements of
the sub-validator.
:param validator: A validator (or a list of validators) that is used for
non-``None`` values.
:type validator: callable or `list` of callables.
:param Callable | tuple[Callable] | list[Callable] validator: A validator
(or validators) that is used for non-``None`` values.
.. versionadded:: 15.1.0
.. versionchanged:: 17.1.0 *validator* can be a list of validators.
.. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
"""
if isinstance(validator, list):
if isinstance(validator, (list, tuple)):
return _OptionalValidator(_AndValidator(validator))

return _OptionalValidator(validator)


Expand Down
4 changes: 3 additions & 1 deletion src/attr/validators.pyi
Expand Up @@ -51,7 +51,9 @@ def instance_of(
def instance_of(type: Tuple[type, ...]) -> _ValidatorType[Any]: ...
def provides(interface: Any) -> _ValidatorType[Any]: ...
def optional(
validator: Union[_ValidatorType[_T], List[_ValidatorType[_T]]]
validator: Union[
_ValidatorType[_T], List[_ValidatorType[_T]], Tuple[_ValidatorType[_T]]
]
) -> _ValidatorType[Optional[_T]]: ...
def in_(options: Container[_T]) -> _ValidatorType[_T]: ...
def and_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
Expand Down
12 changes: 11 additions & 1 deletion tests/test_validators.py
Expand Up @@ -384,7 +384,12 @@ def test_repr(self, ifoo):


@pytest.mark.parametrize(
"validator", [instance_of(int), [always_pass, instance_of(int)]]
"validator",
[
instance_of(int),
[always_pass, instance_of(int)],
(always_pass, instance_of(int)),
],
)
class TestOptional:
"""
Expand Down Expand Up @@ -437,6 +442,11 @@ def test_repr(self, validator):
"<optional validator for _AndValidator(_validators=[{func}, "
"<instance_of validator for type <class 'int'>>]) or None>"
).format(func=repr(always_pass))
elif isinstance(validator, tuple):
repr_s = (
"<optional validator for _AndValidator(_validators=({func}, "
"<instance_of validator for type <class 'int'>>)) or None>"
).format(func=repr(always_pass))
else:
repr_s = (
"<optional validator for <instance_of validator for type "
Expand Down
9 changes: 9 additions & 0 deletions tests/typing_example.py
Expand Up @@ -236,6 +236,15 @@ class Validated:
p: Any = attr.ib(
validator=attr.validators.not_(attr.validators.in_("abc"), msg=None)
)
q: Any = attr.ib(
validator=attrs.validators.optional(attrs.validators.instance_of(C))
)
r: Any = attr.ib(
validator=attrs.validators.optional([attrs.validators.instance_of(C)])
)
s: Any = attr.ib(
validator=attrs.validators.optional((attrs.validators.instance_of(C),))
)


@attr.define
Expand Down

0 comments on commit 591a745

Please sign in to comment.