{"id":19168488,"url":"https://github.com/bloomberg/attrs-strict","last_synced_at":"2025-07-22T01:32:35.245Z","repository":{"id":39627240,"uuid":"209799799","full_name":"bloomberg/attrs-strict","owner":"bloomberg","description":"Provides runtime validation of attributes specified in Python 'attr'-based data classes.","archived":false,"fork":false,"pushed_at":"2024-03-08T19:54:49.000Z","size":139,"stargazers_count":56,"open_issues_count":4,"forks_count":19,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-06-16T06:08:36.431Z","etag":null,"topics":["attributes","python-attrs"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bloomberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-20T13:41:16.000Z","updated_at":"2025-04-12T21:13:18.000Z","dependencies_parsed_at":"2023-11-30T21:24:42.920Z","dependency_job_id":"fbd024c8-665a-4ddc-ba0f-2cb4e11ea794","html_url":"https://github.com/bloomberg/attrs-strict","commit_stats":{"total_commits":74,"total_committers":16,"mean_commits":4.625,"dds":0.6891891891891893,"last_synced_commit":"7ac439c5d0b9a02748c06e55c8dff625b8882383"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/bloomberg/attrs-strict","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloomberg%2Fattrs-strict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloomberg%2Fattrs-strict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloomberg%2Fattrs-strict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloomberg%2Fattrs-strict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bloomberg","download_url":"https://codeload.github.com/bloomberg/attrs-strict/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bloomberg%2Fattrs-strict/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265782723,"owners_count":23827736,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributes","python-attrs"],"created_at":"2024-11-09T09:42:49.942Z","updated_at":"2025-07-22T01:32:35.219Z","avatar_url":"https://github.com/bloomberg.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- begin --\u003e\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\u003c!-- end --\u003e\n\n## Menu\n\n- [Rationale](#rationale)\n- [Quick start](#quick-start)\n- [Building](#building)\n- [Installation](#installation)\n- [Contributions](#contributions)\n- [License](#license)\n- [Code of Conduct](#code-of-conduct)\n- [Security Vulnerability Reporting](#security-vulnerability-reporting)\n\n\u003c!-- begin --\u003e\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\u003clist_of_numbers must be typing.List[int] (got four that is a \u003cclass 'str'\u003e) in [1, 2, 3, 'four']\u003e\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 \u003cclass 'int'\u003e) 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) -\u003e 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\u003c!-- end --\u003e\n\n## Contributions\n\nWe :heart: contributions.\n\nHave you had a good experience with this project? Why not share some love and contribute code, or just let us know about\nany issues you had with it?\n\nWe welcome issue reports [here](../../issues); be sure to choose the proper issue template for your issue, so that we\ncan be sure you're providing the necessary information.\n\nBefore sending a [Pull Request](../../pulls), please make sure you read our\n[Contribution Guidelines](https://github.com/bloomberg/.github/blob/main/CONTRIBUTING.md).\n\n## License\n\nPlease read the [LICENSE](LICENSE) file.\n\n## Code of Conduct\n\nThis project has adopted a [Code of Conduct](https://github.com/bloomberg/.github/blob/main/CODE_OF_CONDUCT.md). If\nyou have any concerns about the Code, or behavior which you have experienced in the project, please contact us at\nopensource@bloomberg.net.\n\n## Security Vulnerability Reporting\n\nIf you believe you have identified a security vulnerability in this project, please send email to the project team at\nopensource@bloomberg.net, detailing the suspected issue and any methods you've found to reproduce it.\n\nPlease do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've\nhad an opportunity to review and address them.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloomberg%2Fattrs-strict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbloomberg%2Fattrs-strict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbloomberg%2Fattrs-strict/lists"}