Skip to content

hashberg-io/typing-validation

Repository files navigation

typing-validation: Validation using Type Hints

Python versions PyPI version PyPI status Checked with Mypy Documentation Status Python package status standard-readme compliant

Typing-validation is a small library to perform runtime validation of Python objects using PEP 484 type hints.

You can install the latest release from PyPI as follows:

pip install --upgrade typing-validation

The core functionality of this library is provided by the validate function:

>>> from typing_validation import validate

The validate function is invoked with a value and a type as its arguments and it returns nothing when the given value is valid for the given type:

>>> validate(12, int)
True # no error raised => 12 is a valid int

If the value is invalid for the given type, the validate function raises a TypeError:

>>> validate(12, str)
TypeError: Runtime validation error raised by validate(val, t), details below.
For type <class 'str'>, invalid value: 12

For nested types (e.g. parametric collection/mapping types), the full chain of validation failures is shown by the type error:

>>> validate([0, 1, "hi"], list[int])
TypeError: Runtime validation error raised by validate(val, t), details below.
For type list[int], invalid value at idx: 2
  For type <class 'int'>, invalid value: 'hi'

The function is_valid is a variant of the validate function which returns False in case of validation failure, instead of raising TypeError:

>>> from typing_validation import is_valid
>>> is_valid([0, 1, "hi"], list[int])
False

The function latest_validation_failure can be used to access detailed information immediately after a failure:

>>> from typing_validation import latest_validation_failure
>>> is_valid([0, 1, "hi"], list[int])
False
>>> failure = latest_validation_failure()
>>> print(failure)
Runtime validation error raised by validate(val, t), details below.
For type list[int], invalid value at idx: 2
  For type <class 'int'>, invalid value: 'hi'

Please note that latest_validation_failure clears the internal failure logs after returning the latest failure, so the latter must be manually stored if it needs to be accessed multiple times.

For the full API documentation, see https://typing-validation.readthedocs.io/

Please see CONTRIBUTING.md.

MIT © Hashberg Ltd.