Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept tuples in attrs.validators.optional #1122

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1122.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`attrs.validators.optional()` now also accepts a tuple of validators (in addition to lists of validators).
9 changes: 5 additions & 4 deletions src/attr/validators.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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