{"id":13501365,"url":"https://github.com/erezsh/runtype","last_synced_at":"2025-04-05T22:08:09.277Z","repository":{"id":37769280,"uuid":"233709982","full_name":"erezsh/runtype","owner":"erezsh","description":"Utilities for run-time type validation and multiple dispatch","archived":false,"fork":false,"pushed_at":"2024-04-22T14:28:22.000Z","size":535,"stargazers_count":159,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-05-22T23:22:45.056Z","etag":null,"topics":["dataclass","multiple-dispatch","type-validation","typing"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erezsh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","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":"2020-01-13T22:50:34.000Z","updated_at":"2024-04-24T20:31:31.000Z","dependencies_parsed_at":"2023-11-14T04:26:22.429Z","dependency_job_id":"0adbf12c-8605-45c5-bc8b-afaf8dd49268","html_url":"https://github.com/erezsh/runtype","commit_stats":{"total_commits":301,"total_committers":3,"mean_commits":"100.33333333333333","dds":"0.49169435215946844","last_synced_commit":"b1b79c1b984e0dc23afd089b8d9ee99ff5c8be7b"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erezsh%2Fruntype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erezsh%2Fruntype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erezsh%2Fruntype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erezsh%2Fruntype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erezsh","download_url":"https://codeload.github.com/erezsh/runtype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406090,"owners_count":20933803,"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":["dataclass","multiple-dispatch","type-validation","typing"],"created_at":"2024-07-31T22:01:34.751Z","updated_at":"2025-04-05T22:08:09.234Z","avatar_url":"https://github.com/erezsh.png","language":"Python","readme":"![alt text](logo.png \"Logo\")\n\n\nRuntype is a collection of run-time type utilities for Python.\n\nIt is:\n\n:runner: Fast! Uses an internal typesystem for maximum performance.\n\n:brain: Smart! Supports `typing`, forward-references, constraints, auto-casting, and more.\n\n:gear: Configurative! Write your own type system, and use it with *dataclass* and *dispatch*.\n\n------\n\n### Modules\n\n- :star: [**validation**](https://runtype.readthedocs.io/en/latest/validation.html) - Provides a smarter alternative to `isinstance` and `issubclass`, with support for the `typing` module, and type constraints.\n\n- :star: [**dataclass**](https://runtype.readthedocs.io/en/latest/dataclass.html) - Adds run-time type validation to the built-in dataclass.\n\n    - Improves dataclass ergonomics.\n    - Supports most mypy constructs, like `typing` and forward-references (`foo: 'Bar'`).\n    - Supports automatic value casting, Pydantic-style. (Optional, off by default)\n    - Supports types with constraints. (e.g. `String(max_length=10)`)\n    - Supports optional sampling for faster validation of big lists and dicts.\n    - Twice faster than Pydantic-v1 with pure Python ([read here](https://runtype.readthedocs.io/en/latest/dataclass.html#compared-to-pydantic))\n\n- :star: [**dispatch**](https://runtype.readthedocs.io/en/latest/dispatch.html) - Provides fast multiple-dispatch for functions and methods, via a decorator.\n\n    - Dispatch on multiple arguments\n    - Full [specificity](https://runtype.readthedocs.io/en/latest/dispatch.html#specificity) resolution\n    - [Supports mypy](https://runtype.readthedocs.io/en/latest/dispatch.html#mypy-support), by utilizing the `@overload` decorator\n    - Inspired by Julia.\n\n- :star: [**type utilities**](https://runtype.readthedocs.io/en/latest/types.html) - Provides a set of classes to implement your own type-system.\n\n    - Supports generics, constraints, phantom types\n    - Used by runtype itself, to emulate the Python type-system.\n \n\n## Docs\n\nRead the docs here: https://runtype.readthedocs.io/\n\n## Install\n\n```bash\npip install runtype\n```\n\nNo dependencies.\n\nRequires Python 3.8 or up.\n\n[![codecov](https://codecov.io/gh/erezsh/runtype/branch/master/graph/badge.svg)](https://codecov.io/gh/erezsh/runtype)\n\n## Examples\n\n### Validation (Isa \u0026 Subclass)\n\nUse `isa` and `issubclass` as a smarter alternative to the builtin isinstance \u0026 issubclass -\n\n```python\nfrom runtype import isa, issubclass\n\nassert isa({'a': 1}, dict[str, int])        # == True\nassert not isa({'a': 'b'}, dict[str, int])  # == False\n\nassert issubclass(dict[str, int], typing.Mapping[str, int])     # == True\nassert not issubclass(dict[str, int], typing.Mapping[int, str]) # == False\n```\n\n### Dataclasses\n\n```python\nfrom runtype import dataclass\n\n@dataclass(check_types='cast')  # Cast values to the target type, when applicable\nclass Person:\n    name: str\n    birthday: datetime = None   # Implicit optional\n    interests: list[str] = []   # The list is copied for each instance\n\n\nprint( Person(\"Beetlejuice\") )\n#\u003e Person(name='Beetlejuice', birthday=None, interests=[])\nprint( Person(\"Albert\", \"1955-04-18T00:00\", ['physics']) )\n#\u003e Person(name='Albert', birthday=datetime.datetime(1955, 4, 18, 0, 0), interests=['physics'])\nprint( Person(\"Bad\", interests=['a', 1]) )\n# TypeError: [Person] Attribute 'interests' expected value of type list[str]. Instead got ['a', 1]\n#     Failed on item: 1, expected type str\n```\n\n### Multiple Dispatch\n\nRuntype dispatches according to the most specific type match -\n\n```python\nfrom runtype import multidispatch as md\n\n@md\ndef mul(a: list, b: list):\n    return [mul(i, j) for i, j in zip(a, b, strict=True)]\n@md\ndef mul(a: list, b: Any):\n    return [ai*b for ai in a]\n@md\ndef mul(a: Any, b: list):\n    return [bi*b for bi in b]\n@md\ndef mul(a: Any, b: Any):\n    return a * b\n\nassert mul(\"a\", 4)         == \"aaaa\"        # Any, Any\nassert mul([1, 2, 3], 2)   == [2, 4, 6]     # list, Any\nassert mul([1, 2], [3, 4]) == [3, 8]        # list, list\n\n```\n\nDispatch can also be used for extending the dataclass builtin `__init__`:\n\n```python\n@dataclass\nclass Point:\n    x: int = 0\n    y: int = 0\n\n    @md\n    def __init__(self, points: list | tuple):\n        # Call default constructor\n        self.__init__(*points)\n\n    @md\n    def __init__(self, points: dict):\n        # Call default constructor\n        self.__init__(points['x'], points['y'])\n\n# Test constructors\np0 = Point()                         # Default constructor\nassert p0 == Point(0, 0)             # Default constructor\nassert p0 == Point([0, 0])           # User constructor\nassert p0 == Point((0, 0))           # User constructor\nassert p0 == Point({\"x\": 0, \"y\": 0}) # User constructor\n```\n\n## Benchmarks\n\nRuntype beats its competition handily. It is significantly faster than both *beartype* and *plum*, and in some cases is even faster than regular Python code.\n\nSee the [benchmarks page](https://runtype.readthedocs.io/en/latest/benchmarks.html) in the documentation for detailed benchmarks.\n\n![alt text](bench1.jpg \"Validation Benchmark\")\n\n![alt text](bench2.jpg \"Dispatch Benchmark\")\n\n## License\n\nRuntype uses the [MIT license](LICENSE).\n\n## Contribute\n\nIf you like Runtype and want to see it grow, you can help by:\n\n- Reporting bugs or suggesting features\n\n- Submitting pull requests (better to ask me first)\n\n- Writing about runtype in a blogpost or even a tweet\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferezsh%2Fruntype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferezsh%2Fruntype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferezsh%2Fruntype/lists"}