MCPcopy
hub / github.com/python-attrs/attrs / optional

Function optional

src/attr/validators.py:214–232  ·  view source on GitHub ↗

A validator that makes an attribute optional. An optional attribute is one which can be set to `None` in addition to satisfying the requirements of the sub-validator. Args: validator (typing.Callable | tuple[typing.Callable] | list[typing.Callable]):

(validator)

Source from the content-addressed store, hash-verified

212
213
214def optional(validator):
215 """
216 A validator that makes an attribute optional. An optional attribute is one
217 which can be set to `None` in addition to satisfying the requirements of
218 the sub-validator.
219
220 Args:
221 validator
222 (typing.Callable | tuple[typing.Callable] | list[typing.Callable]):
223 A validator (or validators) that is used for non-`None` values.
224
225 .. versionadded:: 15.1.0
226 .. versionchanged:: 17.1.0 *validator* can be a list of validators.
227 .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
228 """
229 if isinstance(validator, (list, tuple)):
230 return _OptionalValidator(_AndValidator(validator))
231
232 return _OptionalValidator(validator)
233
234
235@attrs(repr=False, slots=True, unsafe_hash=True)

Callers 6

test_successMethod · 0.90
test_failMethod · 0.90
test_reprMethod · 0.90
_DeepIterableClass · 0.70
_DeepMappingClass · 0.70

Calls 2

_OptionalValidatorClass · 0.85
_AndValidatorClass · 0.85

Tested by 4

test_successMethod · 0.72
test_failMethod · 0.72
test_reprMethod · 0.72