Internals

The following document is intended to be used by ValidX developers only.

Contracts

Contracts are used to validate parameters of validator itself during its initialization.

validx.contracts.expect(obj, attr, value, nullable=False, types=None, not_types=None, convert_to=None)[source]

Check, whether the value satisfies expectations

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
  • types (None, type or tuple) – define acceptable types of the value. Default: None — accept any type.
  • not_types – define implicitly unacceptable types of the value. Default: None — accept any type.
  • convert_to (type) – convert the value to specified type. Default: None — does not convert the value.
Raises:

TypeError

  • if types is not None and not isinstance(value, types);
  • if not_types is not None and isinstance(value, not_types).

validx.contracts.expect_flag(obj, attr, value)[source]

Check, whether the value satisfies expectations of boolean flag

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
Raises:

TypeError – if not isinstance(value, (bool, int, type(None))).

validx.contracts.expect_length(obj, attr, value, nullable=False)[source]

Check, whether the value satisfies expectations of integer length

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
Raises:
  • TypeError – if not isinstance(value, int).
  • ValueError – if value < 0.
validx.contracts.expect_str(obj, attr, value, nullable=False)[source]

Check, whether the value satisfies expectations of base string

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
Raises:

TypeError – if not isinstance(value, str).

validx.contracts.expect_callable(obj, attr, value, nullable=False)[source]

Check, whether the value satisfies expectations of callable

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
Raises:

TypeError – if not isinstance(value, collections.abc.Callable).

validx.contracts.expect_container(obj, attr, value, nullable=False, empty=False, item_type=None)[source]

Check, whether the value satisfies expectations of container

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
  • empty (bool) – accept empty container as a valid value. Default: False — does not accept empty container.
  • item_type (type) – check, whether each item of the container has specified type. Default: None — does not check items.
Raises:
  • TypeError
    • if not isinstance(value, collections.abc.Container);
    • if isinstance(value, (str, bytes));
    • if item_type is not None and isinstance(item, item_type), for item in value.
  • ValueError – if not empty and not value.
Returns:

passed container converted to frozenset, if items are hashable, otherwise to tuple.

validx.contracts.expect_sequence(obj, attr, value, nullable=False, empty=False, item_type=None)[source]

Check, whether the value satisfies expectations of sequence

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
  • empty (bool) – accept empty sequence as a valid value. Default: False — does not accept empty sequence.
  • item_type (type) – check, whether each item of the sequence has specified type. Default: None — does not check items.
Raises:
  • TypeError
    • if not isinstance(value, collections.abc.Sequence);
    • if isinstance(value, (str, bytes));
    • if item_type is not None and isinstance(item, item_type), for item in value.
  • ValueError – if not empty and not value.
Returns:

passed sequence converted to tuple.

validx.contracts.expect_mapping(obj, attr, value, nullable=False, empty=False, value_type=None)[source]

Check, whether the value satisfies expectations of mapping

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
  • empty (bool) – accept empty mapping as a valid value. Default: False — does not accept empty mapping.
  • value_type (type) – check, whether each value of the mapping has specified type. Default: None — does not check items.
Raises:
  • TypeError
    • if not isinstance(value, collections.abc.Sequence);
    • if isinstance(value, (str, bytes));
    • if value_type is not None and isinstance(val, value_type), for key, val in value.items().
  • ValueError – if not empty and not value.
Returns:

passed mapping converted to mappingproxy.

validx.contracts.expect_tuple(obj, attr, value, struct, nullable=False)[source]

Check, whether the value satisfies expectations of tuple of specific structure

Parameters:
  • obj – an object, which will set the value to its attribute. It is used to make error messages more specific.
  • attr (str) – name of an attribute of the object. It is used to make error messages more specific.
  • value – checked value itself.
  • struct (tuple) – tuple of types.
  • nullable (bool) – accept None as a valid value. Default: False — does not accept None.
Raises:
  • TypeError
    • if not isinstance(value, collections.abc.Sequence);
    • if isinstance(value, (str, bytes));
    • if not isinstance(item, item_type), for item_type, item in zip(struct, value).
  • ValueError – if len(value) != len(struct).
Returns:

passed sequence converted to tuple.