[![Latest version on
PyPi](https://badge.fury.io/py/attrs-strict.svg)](https://badge.fury.io/py/attrs-strict)
[![Supported Python
versions](https://img.shields.io/pypi/pyversions/attrs-strict.svg)](https://pypi.org/project/attrs-strict/)
[![PyPI - Implementation](https://img.shields.io/pypi/implementation/attrs-strict?style=flat-square)](https://pypi.org/project/attrs-strict)
[![Build Status](https://github.com/bloomberg/attrs-strict/workflows/check/badge.svg)](https://github.com/bloomberg/attrs-strict/actions)
[![Code style:
black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
# attrs runtime validation
`attrs-strict` is a Python package which contains runtime validation for
[`attrs`](https://github.com/python-attrs/attrs) data classes based on the types existing in the typing module.
## Rationale
The purpose of the library is to provide runtime validation for attributes specified in
[`attrs`](https://www.attrs.org/en/stable/) data classes. The types supported are all the builtin types and most of the
ones defined in the typing library. For Python 2, the typing module is available through the backport found
[`here`](https://pypi.org/project/typing/).
## Quick Start
Type enforcement is based on the `type` attribute set on any field specified in an `attrs` dataclass. If the type
argument is not specified, no validation takes place.
`pip install attrs-strict`
```python
from typing import List
import attr
from attrs_strict import type_validator
@attr.s
class SomeClass(object):
list_of_numbers = attr.ib(validator=type_validator(), type=List[int])
sc = SomeClass([1, 2, 3, 4])
print(sc)
SomeClass(list_of_numbers=[1, 2, 3, 4])
try:
SomeClass([1, 2, 3, "four"])
except ValueError as exception:
print(repr(exception))
```
```console
SomeClass(list_of_numbers=[1, 2, 3, 4])
<list_of_numbers must be typing.List[int] (got four that is a <class 'str'>) in [1, 2, 3, 'four']>
```
Nested type exceptions are validated accordingly, and a backtrace to the initial container is maintained to ease with
debugging. This means that if an exception occurs because a nested element doesn't have the correct type, the
representation of the exception will contain the path to the specific element that caused the exception.
```python
from typing import List, Tuple
import attr
from attrs_strict import type_validator
@attr.s
class SomeClass(object):
names = attr.ib(validator=type_validator(), type=List[Tuple[str, str]])
try:
SomeClass(names=[("Moo", "Moo"), ("Zoo", 123)])
except ValueError as exception:
print(exception)
```
```console
names must be typing.List[typing.Tuple[str, str]] (got 123 that is a <class 'int'>) in ('Zoo', 123) in [('Moo', 'Moo'), ('Zoo', 123)]
```
### What is currently supported ?
Currently, there's support for simple types and types specified in the `typing` module: `List`, `Dict`, `DefaultDict`,
`Set`, `Union`, `Tuple`, `NewType` `Callable`, `Literal` and any combination of them. This means that you can specify
nested types like `List[List[Dict[int, str]]]` and the validation would check if attribute has the specific type.
`Callable` will validate if the callable function's annotation matches the type definition. If type does not specify any
annotations then all callables will pass the validation against it. Support for `Callable` is not available for
`python2`.
`Literal` only allows using instances of `int`, `str`, `bool`, `Enum` or valid `Literal` types. Type checking `Literal`
with any other type as argument raises `attrs_strict._error.UnsupportedLiteralError`.
```python
def fully_annotated_function(self, a: int, b: int) -> str:
...
def un_annonated_function(a, b):
...
@attr.s
class Something(object):
a = attr.ib(
validator=type_validator(), type=typing.Callable
) # Will work for any callable
b = attr.ib(validator=type_validator(), type=typing.Callable[[int, int], str])
Something(a=un_annonated_function, b=fully_annotated_function)
```
`TypeVars` or `Generics` are not supported yet but there are plans to support this in the future.
## Building
For development, the project uses [`tox`](http://tox.readthedocs.org/) in order to install dependencies, run tests and
generate documentation. In order to be able to do this, you need tox `pip install tox` and after that invoke `tox` in
the root of the project.
## Installation
Run `pip install attrs-strict` to install the latest stable version from [PyPi](https://pypi.org/project/attrs-strict/).
Documentation is hosted on [readthedocs](https://attrs-strict.readthedocs.io/en/latest/).
For the latest version, on github `pip install git+https://github.com/bloomberg/attrs-strict`.
Raw data
{
"_id": null,
"home_page": "https://github.com/bloomberg/attrs-strict",
"name": "attrs-strict",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "",
"author": "Erik-Cristian Seulean",
"author_email": "eseulean@bloomberg.net",
"download_url": "https://files.pythonhosted.org/packages/5d/ee/b12773e4924b732330cba0c528a6a02740e5990fc5c9718a07d0a060b53d/attrs_strict-1.0.1.tar.gz",
"platform": null,
"description": "\n\n[![Latest version on\nPyPi](https://badge.fury.io/py/attrs-strict.svg)](https://badge.fury.io/py/attrs-strict)\n[![Supported Python\nversions](https://img.shields.io/pypi/pyversions/attrs-strict.svg)](https://pypi.org/project/attrs-strict/)\n[![PyPI - Implementation](https://img.shields.io/pypi/implementation/attrs-strict?style=flat-square)](https://pypi.org/project/attrs-strict)\n[![Build Status](https://github.com/bloomberg/attrs-strict/workflows/check/badge.svg)](https://github.com/bloomberg/attrs-strict/actions)\n[![Code style:\nblack](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n# attrs runtime validation\n\n`attrs-strict` is a Python package which contains runtime validation for\n[`attrs`](https://github.com/python-attrs/attrs) data classes based on the types existing in the typing module.\n\n\n\n## Rationale\n\nThe purpose of the library is to provide runtime validation for attributes specified in\n[`attrs`](https://www.attrs.org/en/stable/) data classes. The types supported are all the builtin types and most of the\nones defined in the typing library. For Python 2, the typing module is available through the backport found\n[`here`](https://pypi.org/project/typing/).\n\n## Quick Start\n\nType enforcement is based on the `type` attribute set on any field specified in an `attrs` dataclass. If the type\nargument is not specified, no validation takes place.\n\n`pip install attrs-strict`\n\n```python\nfrom typing import List\nimport attr\nfrom attrs_strict import type_validator\n\n\n@attr.s\nclass SomeClass(object):\n list_of_numbers = attr.ib(validator=type_validator(), type=List[int])\n\n\nsc = SomeClass([1, 2, 3, 4])\nprint(sc)\nSomeClass(list_of_numbers=[1, 2, 3, 4])\n\ntry:\n SomeClass([1, 2, 3, \"four\"])\nexcept ValueError as exception:\n print(repr(exception))\n```\n\n```console\nSomeClass(list_of_numbers=[1, 2, 3, 4])\n<list_of_numbers must be typing.List[int] (got four that is a <class 'str'>) in [1, 2, 3, 'four']>\n```\n\nNested type exceptions are validated accordingly, and a backtrace to the initial container is maintained to ease with\ndebugging. This means that if an exception occurs because a nested element doesn't have the correct type, the\nrepresentation of the exception will contain the path to the specific element that caused the exception.\n\n```python\nfrom typing import List, Tuple\nimport attr\nfrom attrs_strict import type_validator\n\n\n@attr.s\nclass SomeClass(object):\n names = attr.ib(validator=type_validator(), type=List[Tuple[str, str]])\n\n\ntry:\n SomeClass(names=[(\"Moo\", \"Moo\"), (\"Zoo\", 123)])\nexcept ValueError as exception:\n print(exception)\n```\n\n```console\nnames must be typing.List[typing.Tuple[str, str]] (got 123 that is a <class 'int'>) in ('Zoo', 123) in [('Moo', 'Moo'), ('Zoo', 123)]\n```\n\n### What is currently supported ?\n\nCurrently, there's support for simple types and types specified in the `typing` module: `List`, `Dict`, `DefaultDict`,\n`Set`, `Union`, `Tuple`, `NewType` `Callable`, `Literal` and any combination of them. This means that you can specify\nnested types like `List[List[Dict[int, str]]]` and the validation would check if attribute has the specific type.\n\n`Callable` will validate if the callable function's annotation matches the type definition. If type does not specify any\nannotations then all callables will pass the validation against it. Support for `Callable` is not available for\n`python2`.\n\n`Literal` only allows using instances of `int`, `str`, `bool`, `Enum` or valid `Literal` types. Type checking `Literal`\nwith any other type as argument raises `attrs_strict._error.UnsupportedLiteralError`.\n\n```python\ndef fully_annotated_function(self, a: int, b: int) -> str:\n ...\n\n\ndef un_annonated_function(a, b):\n ...\n\n\n@attr.s\nclass Something(object):\n a = attr.ib(\n validator=type_validator(), type=typing.Callable\n ) # Will work for any callable\n b = attr.ib(validator=type_validator(), type=typing.Callable[[int, int], str])\n\n\nSomething(a=un_annonated_function, b=fully_annotated_function)\n```\n\n`TypeVars` or `Generics` are not supported yet but there are plans to support this in the future.\n\n## Building\n\nFor development, the project uses [`tox`](http://tox.readthedocs.org/) in order to install dependencies, run tests and\ngenerate documentation. In order to be able to do this, you need tox `pip install tox` and after that invoke `tox` in\nthe root of the project.\n\n## Installation\n\nRun `pip install attrs-strict` to install the latest stable version from [PyPi](https://pypi.org/project/attrs-strict/).\nDocumentation is hosted on [readthedocs](https://attrs-strict.readthedocs.io/en/latest/).\n\nFor the latest version, on github `pip install git+https://github.com/bloomberg/attrs-strict`.\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Runtime validators for attrs",
"version": "1.0.1",
"project_urls": {
"Documentation": "https://github.com/bloomberg/attrs-strict/blob/main/README.md#attrs-runtime-validation",
"Homepage": "https://github.com/bloomberg/attrs-strict",
"Source": "https://github.com/bloomberg/attrs-strict",
"Tracker": "https://github.com/bloomberg/attrs-strict/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "51b451f8a8110319adbe8ef25ba63c4069511ba4474014abdf644afa1adbba31",
"md5": "cb6a0071ee3fc80ef3ee52d00665eb7a",
"sha256": "36ed5955d8798d4cf1d183362f151fb230a10d40558f78e2d943b63c1269c9d2"
},
"downloads": -1,
"filename": "attrs_strict-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cb6a0071ee3fc80ef3ee52d00665eb7a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 14103,
"upload_time": "2023-08-14T16:58:50",
"upload_time_iso_8601": "2023-08-14T16:58:50.222229Z",
"url": "https://files.pythonhosted.org/packages/51/b4/51f8a8110319adbe8ef25ba63c4069511ba4474014abdf644afa1adbba31/attrs_strict-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5deeb12773e4924b732330cba0c528a6a02740e5990fc5c9718a07d0a060b53d",
"md5": "ceb5c88a41b6d398f438bdb95ee7fa80",
"sha256": "e704863620146c5f2a8b601cd4574d148913db26fc0636f941fe4212e425eafe"
},
"downloads": -1,
"filename": "attrs_strict-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "ceb5c88a41b6d398f438bdb95ee7fa80",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 25069,
"upload_time": "2023-08-14T16:58:51",
"upload_time_iso_8601": "2023-08-14T16:58:51.771835Z",
"url": "https://files.pythonhosted.org/packages/5d/ee/b12773e4924b732330cba0c528a6a02740e5990fc5c9718a07d0a060b53d/attrs_strict-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-14 16:58:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bloomberg",
"github_project": "attrs-strict",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "attrs-strict"
}