{ "info": { "author": "RussBaz", "author_email": "RussBaz@users.noreply.github.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.5" ], "description": "| Branch\t| Status |\n| :-------:\t| :--- \t |\n| Master: \t| [![Build Status](https://img.shields.io/travis/RussBaz/enforce/master.svg)](https://travis-ci.org/RussBaz/enforce) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/github/RussBaz/enforce?branch=master&svg=true)](https://ci.appveyor.com/project/RussBaz/enforce) [![Coverage Status](https://img.shields.io/coveralls/RussBaz/enforce/master.svg)](https://coveralls.io/github/RussBaz/enforce?branch=master) [![Requirements Status](https://img.shields.io/requires/github/RussBaz/enforce/master.svg)](https://requires.io/github/RussBaz/enforce/requirements/?branch=master) [![PyPI version](https://img.shields.io/pypi/v/enforce.svg)](https://pypi.python.org/pypi/enforce)\t|\n| Dev: \t| [![Build Status](https://img.shields.io/travis/RussBaz/enforce/dev.svg)](https://travis-ci.org/RussBaz/enforce) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/github/RussBaz/enforce?branch=dev&svg=true)](https://ci.appveyor.com/project/RussBaz/enforce) [![Coverage Status](https://img.shields.io/coveralls/RussBaz/enforce/dev.svg)](https://coveralls.io/github/RussBaz/enforce?branch=dev) [![Requirements Status](https://img.shields.io/requires/github/RussBaz/enforce/dev.svg)](https://requires.io/github/RussBaz/enforce/requirements/?branch=dev)\t|\n\n# Enforce.py\n\n*__Enforce.py__* is a Python 3.5+ library for integration testing and data validation through configurable and optional runtime type hint enforcement. It uses the standard type hinting syntax (defined in PEP 484).\n\n**NOTICE:** Python versions 3.5.2 and earlier (3.5.0-3.5.2) are now deprecated. Only Python versions 3.5.3+ would be supported. Deprecated versions will no longer be officially supported in Enforce.py version 0.4.x.\n\n* [Overview](#overview)\n* [Installation](#installation)\n* [Usage](#usage)\n * [Features](#features)\n * [Basics](#basic-type-hint-enforcement)\n * [Callable](#callable-support)\n * [TypeVar and Generics](#typevar-and-generics)\n * [Class Decorator](#class-decorator)\n * [NamedTuple](#namedtuple)\n * [Configuration](#configuration)\n* [Changelog](#changelog)\n* [Contributing](#contributing)\n\n## Overview\n\n* Supports most of simple and nested types\n* Supports Callables, TypeVars and Generics\n* Supports invariant, covariant, contravariant and bivariant type checking\n * **Default** mode is *__invariant__* - the type has to match exactly, which is better suitable for testing but differs from Python's normal covariant type checking (a subclass can be used wherever a parent class is expected).\n* Can be applied to both functions and classes (in this case it will be applied to all methods of the class)\n* Highly configurable\n * Global on/off switch\n * Group configuration\n * Local override of groups\n * Type checking mode selection\n * Dynamic reconfiguration\n\n## Installation\n\nStable 0.3.x - Stable and ready for every day use version\n\n pip install enforce\n\nDev current - \"Bleeding edge\" features that, while are fairly consistent, may\nchange.\n\n pip install git+https://github.com/RussBaz/enforce.git@dev\n\n## Usage\n\nType enforcement is done using decorators around functions that you desire to be\nchecked. By default, this decorator will ensure that any variables passed into\nthe function call matches its declaration (invariantly by default). This includes integers, strings, etc.\nas well as lists, dictionaries, and more complex objects. Currently, the type checking is eager.\n\nNote, eager means that for a large nested structure, every item in that\nstructure will be checked. This may be a nightmare for performance! See\n[caveats](#caveats) for more details.\n\nYou can also apply the `runtime_validation` decorator around a class, and it\nwill enforce the types of every method in that class.\n\n**Note:** this is a development feature and is not as thoroughly tested as the function decorators.\n\n### Features\n\n#### Basic type hint enforcement\n\n```python\n>>> import enforce\n>>>\n>>> @enforce.runtime_validation\n... def foo(text: str) -> None:\n... print(text)\n>>>\n>>> foo('Hello World')\nHello World\n>>>\n>>> foo(5)\nTraceback (most recent call last):\n File \"\", line 1, in \n File \"/home/william/.local/lib/python3.5/site-packages/enforce/decorators.py\", line 106, in universal\n _args, _kwargs = enforcer.validate_inputs(parameters)\n File \"/home/william/.local/lib/python3.5/site-packages/enforce/enforcers.py\", line 69, in validate_inputs\n raise RuntimeTypeError(exception_text)\nenforce.exceptions.RuntimeTypeError: \n The following runtime type errors were encountered:\n Argument 'text' was not of type . Actual type was .\n>>>\n```\n\n#### Callable Support\n\n```python\n@runtime_validation\ndef foo(a: typing.Callable[[int, int], str]) -> str:\n return a(5, 6)\n\ndef bar(a: int, b: int) -> str:\n return str(a * b)\n\nclass Baz:\n def __call__(self, a: int, b: int) -> str:\n return bar(a, b)\n\nfoo(bar)\nfoo(Baz())\n```\n\n#### TypeVar and Generics\n\n```python\nT = typing.TypeVar('T', int, str)\n\n@runtime_validation\nclass Sample(typing.Generic[T]):\n def get(self, data: T) -> T:\n return data\n\n@runtime_validation\ndef foo(data: Sample[int], arg: int) -> int:\n return data.get(arg)\n\n@runtime_validation\ndef bar(data: T, arg: int) -> T:\n return arg\n\nsample_good = Sample[int]()\nsample_bad = Sample()\n\nwith self.assertRaises(TypeError):\n sample = Sample[list]()\n\nfoo(sample_good, 1)\n\nwith self.assertRaises(RuntimeTypeError):\n foo(sample_bad, 1)\n\nbar(1, 1)\n\nwith self.assertRaises(RuntimeTypeError):\n bar('str', 1)\n```\n\n#### Class Decorator\n\nApplying this decorator to a class will automatically apply the decorator to\nevery method in the class.\n\n```python\n@runtime_validation\nclass DoTheThing(object):\n def __init__(self):\n self.do_the_stuff(5, 6.0)\n\n def do_the_stuff(self, a: int, b: float) -> str:\n return str(a * b)\n```\n\n#### NamedTuple\n\nEnforce.py supports typed NamedTuples.\n\n```python\nMyNamedTuple = typing.NamedTuple('MyNamedTuple', [('param', int)])\n\n# Optionally making a NamedTuple typed\n# It will now enforce its type signature\n# and will throw exceptions if there is a type mismatch\n# MyNamedTuple(param='str') will now throw an exception\nMyNamedTuple = runtime_validation(MyNamedTuple)\n\n# This function now accepts only NamedTuple arguments\n@runtime_validation\ndef foo(data: MyNamedTuple):\n return data.param\n```\n\n### Configuration\n\nYou can assign functions to groups, and apply options on the group level.\n\n'None' leaves previous value unchanged.\n\nAll available global settings:\n```python\ndefault_options = {\n # Global enforce.py on/off switch\n 'enabled': None,\n # Group related settings\n 'groups': {\n # Dictionary of type {: }\n # Sets the status of specified groups\n # Enable - True, disabled - False, do not change - None\n 'set': {},\n # Sets the status of all groups to False before updating\n 'disable_previous': False,\n # Sets the status of all groups to True before updating\n 'enable_previous': False,\n # Deletes all the existing groups before updating\n 'clear_previous': False,\n # Updating the default group status - default group is not affected by other settings\n 'default': None\n },\n # Sets the type checking mode\n # Available options: 'invariant', 'covariant', 'contravariant', 'bivariant' and None\n 'mode': None\n }\n```\n\n```python\n# Basic Example\n@runtime_validation(group='best_group')\ndef foo(a: List[str]):\n pass\n\nfoo(1)\t# No exception as the 'best_group' was not explicitly enabled\n\n# Group Configuration\nenforce.config({'groups': {'set': {'best_group': True}}}) # Enabling group 'best_group'\n\nwith self.assertRaises(RuntimeTypeError):\n foo(1)\n\nenforce.config({\n 'groups': {\n 'set': {\n 'foo': True\n },\n 'disable_previous': True,\n 'default': False\n }\n }) # Disable everything but the 'foo' group\n\n# Using foo's settings\n@runtime_validation(group='foo')\ndef test1(a: str): return a\n\n# Using foo's settings but locally overriding it to stay constantly enabled\n@runtime_validation(group='foo', enabled=False)\ndef test2(a: str): return a\n\n# Using bar's settings - deactivated group -> no type checking is performed\n@runtime_validation(group='bar')\ndef test3(a: str): return a\n\n# Using bar's settings but overriding locally -> type checking enabled\n@runtime_validation(group='bar', enabled=True)\ndef test4(a: str): return a\n\nwith self.assertRaises(RuntimeTypeError):\n test1(1)\ntest2(1)\ntest3(1)\nwith self.assertRaises(RuntimeTypeError):\n test4(1)\n\nfoo(1)\n\nenforce.config({'enabled': False}) # Disables enforce.py\n\ntest1(1)\ntest2(1)\ntest3(1)\ntest4(1)\nfoo(1)\n\nenforce.config({'enabled': True}) # Re-enables enforce.py\n\nenforce.config(reset=True) # Resets global settings to their default state\n```\n\n### Caveats\n\nCurrently, iterators, generators and coroutines type checks are not supported (mostly).\nHowever, it is still possible to check if an object is iterable.\n\nWe are still working on the best approach for lazy type checking (checking list items only when accessed)\nand lazy type evaluation (accepting strings as type hints).\n\nCurrently, the type checker will examine every object in a list. This means that\nfor large structures performance can be a nightmare.\n\nClass decorators are not as well tested, and you may encounter a bug or two.\nPlease report an issue if you do find one and we'll try to fix it as quickly as\npossible.\n\n## Changelog\n\n### 0.3.4 - 11.06.2017\n* Further improved exception messages and their consistency\n* General bug fixes\n\n### 0.3.3 - 23.04.2017\n\n* Improved support for Dictionaries\n* Fixed some thread safety issues\n\n### 0.3.2 - 29.01.2017\n\n* Added support for Python 3.5.3 and 3.6.0\n* Added support for NamedTuple\n* Added support for Set\n* New exception message generation system\n* Fixed failing nested lists type checking\n\n### 0.3.1 - 17.09.2016\n\n* Added support for Callable classes (classes with \\_\\_call\\_\\_ method are now treated like any other Callable object)\n* Fixed bugs in processing callables without specified return type\n\n## Contributing\n\nPlease check out our active issues on our Github page to see what work needs to\nbe done, and feel free to create a new issue if you find a bug.\n\nActual development is done in the 'dev' branch, which is merged to master at\nmilestones.\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/RussBaz/enforce", "keywords": "typechecker validation testing runtime type-hints typing decorators", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "enforce", "package_url": "https://pypi.org/project/enforce/", "platform": "", "project_url": "https://pypi.org/project/enforce/", "project_urls": { "Homepage": "https://github.com/RussBaz/enforce" }, "release_url": "https://pypi.org/project/enforce/0.3.4/", "requires_dist": [ "wrapt" ], "requires_python": "", "summary": "Python 3.5+ library for integration testing and data validation through configurable and optional runtime type hint enforcement.", "version": "0.3.4" }, "last_serial": 2942333, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "82bf1a873a8208fd7dcc904a41cd8f90", "sha256": "6aa08a3abfb9ef3721312a51b86904e8e5a9b71d987a20469b35d68d840c897a" }, "downloads": -1, "filename": "enforce-0.1.0.zip", "has_sig": false, "md5_digest": "82bf1a873a8208fd7dcc904a41cd8f90", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4258, "upload_time": "2015-10-18T15:24:16", "url": "https://files.pythonhosted.org/packages/8f/41/b7d5b0be32ec19f8cbdb96e66bb12a140342cf8fd503b57aac5dbb2d05cc/enforce-0.1.0.zip" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "77134bccb982ccdf5220af3c5eaa8791", "sha256": "ef28b281fb751301aa9b05354b36f591f5a7bb22c9ac298838ad341accdd17fd" }, "downloads": -1, "filename": "enforce-0.2.0-py3.5.egg", "has_sig": false, "md5_digest": "77134bccb982ccdf5220af3c5eaa8791", "packagetype": "bdist_egg", "python_version": "3.5", "requires_python": null, "size": 36487, "upload_time": "2016-08-19T17:59:05", "url": "https://files.pythonhosted.org/packages/4b/70/d6153a49d1019de93836468c0297740fff07096f1c8af85413b920b00f9d/enforce-0.2.0-py3.5.egg" }, { "comment_text": "", "digests": { "md5": "87a24d981d0d0fb19521f87890acc6a0", "sha256": "dbaa76bdaa15d6473eda85f470d3f7f40ae36a042c514ec624d2c62437ce0501" }, "downloads": -1, "filename": "enforce-0.2.0-py3-none-any.whl", "has_sig": false, "md5_digest": "87a24d981d0d0fb19521f87890acc6a0", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 15271, "upload_time": "2016-08-19T17:59:02", "url": "https://files.pythonhosted.org/packages/66/ec/21911fcca6e9906a2230802e57639887583b58e54262f44226cf373a0522/enforce-0.2.0-py3-none-any.whl" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "83001526ddf9add7f11d2e1bf0eb9ebf", "sha256": "055b9bfbeefcf2452267bf07702d0afe780f0a7f236e49057c4b39830873e1d9" }, "downloads": -1, "filename": "enforce-0.3.0-py3-none-any.whl", "has_sig": false, "md5_digest": "83001526ddf9add7f11d2e1bf0eb9ebf", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 19098, "upload_time": "2016-09-13T22:47:45", "url": "https://files.pythonhosted.org/packages/20/60/1fc2cb5f8ab0551a3b3f8308a0cd8f7f55d916e81418b89aee6aff85d1d0/enforce-0.3.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4889e7f8f13a7ad960c93e5975d2f84d", "sha256": "45a866fc3c30b2a4060ce10b8154417c3082723103da8d2db1ca3ef0a52bb4bd" }, "downloads": -1, "filename": "enforce-0.3.0.zip", "has_sig": false, "md5_digest": "4889e7f8f13a7ad960c93e5975d2f84d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 25865, "upload_time": "2016-09-13T22:47:47", "url": "https://files.pythonhosted.org/packages/c4/92/f20e757de2603a704da6381aed41face06d86ae794c3482a67fbb5a61646/enforce-0.3.0.zip" } ], "0.3.1": [ { "comment_text": "", "digests": { "md5": "4dd060ebe16ec083d26e4cec0e35c686", "sha256": "8a83f3516826e4efdadbc653514f29d17c83b8c717a63909a208e13e3b586d0f" }, "downloads": -1, "filename": "enforce-0.3.1-py3-none-any.whl", "has_sig": false, "md5_digest": "4dd060ebe16ec083d26e4cec0e35c686", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 19137, "upload_time": "2016-09-17T17:16:50", "url": "https://files.pythonhosted.org/packages/e0/fa/9ce0986e1feb259876e61c94d04b3ff2c428643190e53d5e626c96c5d327/enforce-0.3.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6b8779a316256706a9b0d0e3837d49e4", "sha256": "36ef54c4c763fb829929669332c255f58930fc86dfe3e9c9f3a72b274588ab2b" }, "downloads": -1, "filename": "enforce-0.3.1.zip", "has_sig": false, "md5_digest": "6b8779a316256706a9b0d0e3837d49e4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 26194, "upload_time": "2016-09-17T17:16:54", "url": "https://files.pythonhosted.org/packages/16/c3/9b9244ae12e6f596c925f2c53a4d5a445ac1ca44000b9d06884168fb642d/enforce-0.3.1.zip" } ], "0.3.2": [ { "comment_text": "", "digests": { "md5": "b1ff5cb4c13282c8fef878eb3f3b13c3", "sha256": "80a9527f67aaa97ff38cc483ea4c66247e365b499001136111e9b84cb1aaaaa0" }, "downloads": -1, "filename": "enforce-0.3.2-py3-none-any.whl", "has_sig": false, "md5_digest": "b1ff5cb4c13282c8fef878eb3f3b13c3", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 21690, "upload_time": "2017-01-29T17:35:54", "url": "https://files.pythonhosted.org/packages/51/bc/ba2febfde7efa629e34ce86cc51ef11278a5f91d83e77b73d65cc2881267/enforce-0.3.2-py3-none-any.whl" } ], "0.3.3": [ { "comment_text": "", "digests": { "md5": "7465a325f92ce6d92cefd0d6f08281fa", "sha256": "7a3c5a203a8a034d74e3d65561a8cdadd16c5a0a8680057cc589e27a525f8658" }, "downloads": -1, "filename": "enforce-0.3.3-py3-none-any.whl", "has_sig": false, "md5_digest": "7465a325f92ce6d92cefd0d6f08281fa", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 22163, "upload_time": "2017-04-23T10:55:23", "url": "https://files.pythonhosted.org/packages/f9/dd/45f656ec13695837f1328bcf1f618e71e9148135d1542c0c414a6a4e1be5/enforce-0.3.3-py3-none-any.whl" } ], "0.3.4": [ { "comment_text": "", "digests": { "md5": "caee16504db5f98da43b6b4ca9b263cc", "sha256": "cdb7720e25d0bd7ad2aaf2311197d5a28d4d2afe73ecc2f74b5d28faee3f68f2" }, "downloads": -1, "filename": "enforce-0.3.4-py3-none-any.whl", "has_sig": false, "md5_digest": "caee16504db5f98da43b6b4ca9b263cc", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 30098, "upload_time": "2017-06-11T16:27:30", "url": "https://files.pythonhosted.org/packages/87/9e/42546df65b1f66ec268f2f531e063c1b05934dfcdf595c0222cd5dad151c/enforce-0.3.4-py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "caee16504db5f98da43b6b4ca9b263cc", "sha256": "cdb7720e25d0bd7ad2aaf2311197d5a28d4d2afe73ecc2f74b5d28faee3f68f2" }, "downloads": -1, "filename": "enforce-0.3.4-py3-none-any.whl", "has_sig": false, "md5_digest": "caee16504db5f98da43b6b4ca9b263cc", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 30098, "upload_time": "2017-06-11T16:27:30", "url": "https://files.pythonhosted.org/packages/87/9e/42546df65b1f66ec268f2f531e063c1b05934dfcdf595c0222cd5dad151c/enforce-0.3.4-py3-none-any.whl" } ] }