typing-validation


Nametyping-validation JSON
Version 1.2.11.post1 PyPI version JSON
download
home_pagehttps://github.com/hashberg-io/typing-validation
SummaryA simple library for runtime type-checking.
upload_time2024-03-17 12:44:35
maintainer
docs_urlNone
authorhashberg
requires_python>=3.7
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
typing-validation: Validation using Type Hints
==============================================

.. image:: https://img.shields.io/badge/python-3.7+-green.svg
    :target: https://docs.python.org/3.7/
    :alt: Python versions

.. image:: https://img.shields.io/pypi/v/typing-validation.svg
    :target: https://pypi.python.org/pypi/typing-validation/
    :alt: PyPI version

.. image:: https://img.shields.io/pypi/status/typing-validation.svg
    :target: https://pypi.python.org/pypi/typing-validation/
    :alt: PyPI status

.. image:: http://www.mypy-lang.org/static/mypy_badge.svg
    :target: https://github.com/python/mypy
    :alt: Checked with Mypy

.. image:: https://readthedocs.org/projects/typing-validation/badge/?version=latest
    :target: https://typing-validation.readthedocs.io/en/latest/?badge=latest
    :alt: Documentation Status

.. image:: https://github.com/hashberg-io/typing-validation/actions/workflows/python-pytest.yml/badge.svg
    :target: https://github.com/hashberg-io/typing-validation/actions/workflows/python-pytest.yml
    :alt: Python package status

.. image:: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square
    :target: https://github.com/RichardLitt/standard-readme
    :alt: standard-readme compliant

Typing-validation is a small library to perform runtime validation of Python objects using `PEP 484 type hints <https://www.python.org/dev/peps/pep-0484/>`_.

.. contents::


Install
-------

You can install the latest release from `PyPI <https://pypi.org/project/typing-validation/>`_ as follows:

.. code-block::

    pip install --upgrade typing-validation


Usage
-----

The core functionality of this library is provided by the `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function:


>>> from typing_validation import validate

The `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.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 <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function raises a `TypeError <https://docs.python.org/3/library/exceptions.html#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 <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function which returns `False` in case of validation failure, instead of raising `TypeError <https://docs.python.org/3/library/exceptions.html#TypeError>`_:

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

The function `latest_validation_failure <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation_failure.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 <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation_failure.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.


API
---

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


Contributing
------------

Please see `<CONTRIBUTING.md>`_.


License
-------

`MIT © Hashberg Ltd. <LICENSE>`_

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hashberg-io/typing-validation",
    "name": "typing-validation",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "hashberg",
    "author_email": "sg495@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/41/3a/54c9cfccf648d2664f1cec026174112dbbc9495c0141261102b21596a6b5/typing-validation-1.2.11.post1.tar.gz",
    "platform": null,
    "description": "\r\ntyping-validation: Validation using Type Hints\r\n==============================================\r\n\r\n.. image:: https://img.shields.io/badge/python-3.7+-green.svg\r\n    :target: https://docs.python.org/3.7/\r\n    :alt: Python versions\r\n\r\n.. image:: https://img.shields.io/pypi/v/typing-validation.svg\r\n    :target: https://pypi.python.org/pypi/typing-validation/\r\n    :alt: PyPI version\r\n\r\n.. image:: https://img.shields.io/pypi/status/typing-validation.svg\r\n    :target: https://pypi.python.org/pypi/typing-validation/\r\n    :alt: PyPI status\r\n\r\n.. image:: http://www.mypy-lang.org/static/mypy_badge.svg\r\n    :target: https://github.com/python/mypy\r\n    :alt: Checked with Mypy\r\n\r\n.. image:: https://readthedocs.org/projects/typing-validation/badge/?version=latest\r\n    :target: https://typing-validation.readthedocs.io/en/latest/?badge=latest\r\n    :alt: Documentation Status\r\n\r\n.. image:: https://github.com/hashberg-io/typing-validation/actions/workflows/python-pytest.yml/badge.svg\r\n    :target: https://github.com/hashberg-io/typing-validation/actions/workflows/python-pytest.yml\r\n    :alt: Python package status\r\n\r\n.. image:: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square\r\n    :target: https://github.com/RichardLitt/standard-readme\r\n    :alt: standard-readme compliant\r\n\r\nTyping-validation is a small library to perform runtime validation of Python objects using `PEP 484 type hints <https://www.python.org/dev/peps/pep-0484/>`_.\r\n\r\n.. contents::\r\n\r\n\r\nInstall\r\n-------\r\n\r\nYou can install the latest release from `PyPI <https://pypi.org/project/typing-validation/>`_ as follows:\r\n\r\n.. code-block::\r\n\r\n    pip install --upgrade typing-validation\r\n\r\n\r\nUsage\r\n-----\r\n\r\nThe core functionality of this library is provided by the `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function:\r\n\r\n\r\n>>> from typing_validation import validate\r\n\r\nThe `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.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:\r\n\r\n>>> validate(12, int)\r\nTrue # no error raised => 12 is a valid int\r\n\r\nIf the value is invalid for the given type, the `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function raises a `TypeError <https://docs.python.org/3/library/exceptions.html#TypeError>`_:\r\n\r\n>>> validate(12, str)\r\nTypeError: Runtime validation error raised by validate(val, t), details below.\r\nFor type <class 'str'>, invalid value: 12\r\n\r\nFor nested types (e.g. parametric collection/mapping types), the full chain of validation failures is shown by the type error:\r\n\r\n>>> validate([0, 1, \"hi\"], list[int])\r\nTypeError: Runtime validation error raised by validate(val, t), details below.\r\nFor type list[int], invalid value at idx: 2\r\n  For type <class 'int'>, invalid value: 'hi'\r\n\r\n\r\nThe function `is_valid` is a variant of the `validate <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation.validate>`_ function which returns `False` in case of validation failure, instead of raising `TypeError <https://docs.python.org/3/library/exceptions.html#TypeError>`_:\r\n\r\n>>> from typing_validation import is_valid\r\n>>> is_valid([0, 1, \"hi\"], list[int])\r\nFalse\r\n\r\nThe function `latest_validation_failure <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation_failure.latest_validation_failure>`_ can be used to access detailed information immediately after a failure:\r\n\r\n>>> from typing_validation import latest_validation_failure\r\n>>> is_valid([0, 1, \"hi\"], list[int])\r\nFalse\r\n>>> failure = latest_validation_failure()\r\n>>> print(failure)\r\nRuntime validation error raised by validate(val, t), details below.\r\nFor type list[int], invalid value at idx: 2\r\n  For type <class 'int'>, invalid value: 'hi'\r\n\r\nPlease note that `latest_validation_failure <https://typing-validation.readthedocs.io/en/latest/api/typing_validation.validation.html#typing_validation.validation_failure.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.\r\n\r\n\r\nAPI\r\n---\r\n\r\nFor the full API documentation, see https://typing-validation.readthedocs.io/\r\n\r\n\r\nContributing\r\n------------\r\n\r\nPlease see `<CONTRIBUTING.md>`_.\r\n\r\n\r\nLicense\r\n-------\r\n\r\n`MIT \u00a9 Hashberg Ltd. <LICENSE>`_\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A simple library for runtime type-checking.",
    "version": "1.2.11.post1",
    "project_urls": {
        "Bug Tracker": "https://github.com/hashberg-io/typing-validation/issues",
        "Homepage": "https://github.com/hashberg-io/typing-validation"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "76445618aaf177f3aecc6011247363fcdc684481c365067b84e00130ca6abad2",
                "md5": "feb578648c8fff2df409b161d38add76",
                "sha256": "4c491d5f842a48dbb581f2613151e5ab5f9bba5490e655ca2dfcdb7b4030543a"
            },
            "downloads": -1,
            "filename": "typing_validation-1.2.11.post1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "feb578648c8fff2df409b161d38add76",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 20680,
            "upload_time": "2024-03-17T12:44:32",
            "upload_time_iso_8601": "2024-03-17T12:44:32.749988Z",
            "url": "https://files.pythonhosted.org/packages/76/44/5618aaf177f3aecc6011247363fcdc684481c365067b84e00130ca6abad2/typing_validation-1.2.11.post1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "413a54c9cfccf648d2664f1cec026174112dbbc9495c0141261102b21596a6b5",
                "md5": "c5095d1e61b8aa8502292c6d21685c41",
                "sha256": "d6b378e09ec36218995f34e92f205ecd6014b8aed2cbb85c4c492c0e03d4379f"
            },
            "downloads": -1,
            "filename": "typing-validation-1.2.11.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "c5095d1e61b8aa8502292c6d21685c41",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 774193,
            "upload_time": "2024-03-17T12:44:35",
            "upload_time_iso_8601": "2024-03-17T12:44:35.259919Z",
            "url": "https://files.pythonhosted.org/packages/41/3a/54c9cfccf648d2664f1cec026174112dbbc9495c0141261102b21596a6b5/typing-validation-1.2.11.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-17 12:44:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hashberg-io",
    "github_project": "typing-validation",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "typing-validation"
}
        
Elapsed time: 0.28384s