Python attr.validators() Examples
The following are 7
code examples of attr.validators().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
attr
, or try the search function
.
Example #1
Source File: test_validators.py From attrs with MIT License | 6 votes |
def test_noncallable_validators( self, member_validator, iterable_validator ): """ Raise `TypeError` if any validators are not callable. """ with pytest.raises(TypeError) as e: deep_iterable(member_validator, iterable_validator) value = 42 message = "must be callable (got {value} that is a {type_}).".format( value=value, type_=value.__class__ ) assert message in e.value.args[0] assert value == e.value.args[1] assert message in e.value.msg assert value == e.value.value
Example #2
Source File: test_validators.py From attrs with MIT License | 6 votes |
def test_repr_member_and_iterable(self): """ Returned validator has a useful `__repr__` when both member and iterable validators are set. """ member_validator = instance_of(int) member_repr = "<instance_of validator for type <{type} 'int'>>".format( type=TYPE ) iterable_validator = instance_of(list) iterable_repr = ( "<instance_of validator for type <{type} 'list'>>" ).format(type=TYPE) v = deep_iterable(member_validator, iterable_validator) expected_repr = ( "<deep_iterable validator for" " {iterable_repr} iterables of {member_repr}>" ).format(iterable_repr=iterable_repr, member_repr=member_repr) assert expected_repr == repr(v)
Example #3
Source File: utils.py From pythonfinder with MIT License | 5 votes |
def optional_instance_of(cls): # type: (Any) -> _OptionalValidator """ Return an validator to determine whether an input is an optional instance of a class. :return: A validator to determine optional instance membership. :rtype: :class:`~attr.validators._OptionalValidator` """ return attr.validators.optional(attr.validators.instance_of(cls))
Example #4
Source File: test_validators.py From attrs with MIT License | 5 votes |
def test_success(self): """ Succeeds if all wrapped validators succeed. """ v = and_(instance_of(int), always_pass) v(None, simple_attr("test"), 42)
Example #5
Source File: test_validators.py From attrs with MIT License | 5 votes |
def test_success_member_and_iterable(self): """ If both the member and iterable validators succeed, nothing happens. """ member_validator = instance_of(int) iterable_validator = instance_of(list) v = deep_iterable(member_validator, iterable_validator) a = simple_attr("test") v(None, a, [42])
Example #6
Source File: test_validators.py From attrs with MIT License | 5 votes |
def test_success(self): """ If both the key and value validators succeed, nothing happens. """ key_validator = instance_of(str) value_validator = instance_of(int) v = deep_mapping(key_validator, value_validator) a = simple_attr("test") v(None, a, {"a": 6, "b": 7})
Example #7
Source File: task_parameters.py From trains with Apache License 2.0 | 4 votes |
def param( validator=None, range=None, type=None, desc=None, metadata=None, *args, **kwargs ): """ A parameter inside a TaskParameters class. See TaskParameters for more information. :param validator: A validator or validators list. Any validator from attr.validators is applicable. :param range: The legal values range of the parameter. A tuple (min_limit, max_limit). None for no limitation. :param type: The type of the parameter. Supported types are int, str and float. None to place no limit of the type :param desc: A string description of the parameter, for future use. :param metadata: A dictionary metadata of the parameter, for future use. :param args: Additional arguments to pass to attr.attrib constructor. :param kwargs: Additional keyword arguments to pass to attr.attrib constructor. :return: An attr.attrib instance to use with TaskParameters class. Warning: Do not create an immutable param using args or kwargs. It will cause connect method of the TaskParameters class to fail. """ metadata = metadata or {} metadata["desc"] = desc validator = _canonize_validator(validator) if type: validator.append(validators.optional(validators.instance_of(type))) if range: validator.append(range_validator(*range)) return attr.ib(validator=validator, type=type, metadata=metadata, *args, **kwargs)