{"id":13468654,"url":"https://github.com/ltworf/typedload","last_synced_at":"2025-03-26T05:31:15.069Z","repository":{"id":30979912,"uuid":"126558482","full_name":"ltworf/typedload","owner":"ltworf","description":"MIGRATED TO CODEBERG. Python library to load dynamically typed data into statically typed data structures","archived":true,"fork":false,"pushed_at":"2024-09-16T11:51:04.000Z","size":9810,"stargazers_count":259,"open_issues_count":3,"forks_count":9,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-29T22:56:38.640Z","etag":null,"topics":["dataclass","enums","json","json-schema","mypy","namedtuple","pydantic","python","python3","schema","types","typing"],"latest_commit_sha":null,"homepage":"https://ltworf.codeberg.page/typedload/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ltworf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"docs/SECURITY.md","support":"docs/supported_types.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"liberapay":"ltworf"}},"created_at":"2018-03-24T02:24:02.000Z","updated_at":"2024-10-17T17:08:25.000Z","dependencies_parsed_at":"2023-10-14T22:28:18.806Z","dependency_job_id":"1db6a394-d3b9-42ff-91e9-df09e38035ef","html_url":"https://github.com/ltworf/typedload","commit_stats":{"total_commits":1082,"total_committers":6,"mean_commits":"180.33333333333334","dds":"0.020332717190388205","last_synced_commit":"6b2df063a7624b8d8a6c0594cf40b31562490318"},"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltworf%2Ftypedload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltworf%2Ftypedload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltworf%2Ftypedload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltworf%2Ftypedload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ltworf","download_url":"https://codeload.github.com/ltworf/typedload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245597269,"owners_count":20641862,"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","enums","json","json-schema","mypy","namedtuple","pydantic","python","python3","schema","types","typing"],"created_at":"2024-07-31T15:01:15.841Z","updated_at":"2025-03-26T05:31:13.384Z","avatar_url":"https://github.com/ltworf.png","language":"Python","funding_links":["https://liberapay.com/ltworf","https://liberapay.com/ltworf/donate"],"categories":["Python"],"sub_categories":[],"readme":"[THE PROJECT MIGRATED TO CODEBERG](https://ltworf.codeberg.page/typedload/)\n\ntypedload\n=========\n\nLoad and dump json-like data into typed data structures in Python3, enforcing\na schema on the data.\n\nThis module provides an API to load dictionaries and lists (usually loaded\nfrom json) into Python's NamedTuples, dataclass, sets, enums, and various\nother typed data structures; respecting all the type-hints and performing\ntype checks or casts when needed.\n\nIt can also dump from typed data structures to json-like dictionaries and lists.\n\nIt is very useful for projects that use Mypy and deal with untyped data\nlike json, because it guarantees that the data will follow the specified schema.\n\nIt is released with a GPLv3 license but [it is possible to ask for LGPLv3](mailto:tiposchi@tiscali.it).\n\n![GPLv3 logo](docs/gpl3logo.png)\n\n[![Donate to LtWorf](docs/donate.svg)](https://liberapay.com/ltworf/donate)\n\nExample\n=======\n\nFor example this dictionary, loaded from a json:\n\n```python\ndata = {\n    'users': [\n        {\n            'username': 'salvo',\n            'shell': 'bash',\n            'sessions': ['pts/4', 'tty7', 'pts/6']\n        },\n        {\n            'username': 'lop'\n        }\n    ],\n}\n```\n\n\nCan be treated more easily if loaded into this type:\n\n```python\n@dataclasses.dataclass\nclass User:\n    username: str\n    shell: str = 'bash'\n    sessions: List[str] = dataclasses.field(default_factory=list)\n\nclass Logins(NamedTuple):\n    users: List[User]\n```\n\nAnd the data can be loaded into the structure with this:\n\n```python\nt_data = typedload.load(data, Logins)\n```\n\nAnd then converted back:\n\n```python\ndata = typedload.dump(t_data)\n```\n\nSupported types\n===============\n\nSince this is not magic, not all types are supported.\n\nThe following things are supported:\n\n * Basic python types (int, str, bool, float, NoneType)\n * NamedTuple\n * Enum\n * Optional[SomeType]\n * List[SomeType]\n * Dict[TypeA, TypeB]\n * Tuple[TypeA, TypeB, TypeC] and Tuple[SomeType, ...]\n * Set[SomeType]\n * Union[TypeA, TypeB]\n * dataclass\n * attr.s\n * ForwardRef (Refer to the type in its own definition)\n * Literal\n * TypedDict\n * datetime.date, datetime.time, datetime.datetime\n * re.Pattern\n * Path\n * IPv4Address, IPv6Address\n * typing.Any\n * typing.NewType\n * uuid.UUID\n\nUnions\n------\n\ntypedload works fine with untagged unions. However using Literal fields to tag them makes it much faster.\n\nUsing Mypy\n==========\n\nMypy and similar tools work without requiring any plugins.\n\n```python\n# This is treated as Any, no checks done.\ndata = json.load(f)\n\n# This is treated as Dict[str, int]\n# but there will be runtime errors if the data does not\n# match the expected format\ndata = json.load(f)  # type: Dict[str, int]\n\n# This is treated as Dict[str, int] and an exception is\n# raised if the actual data is not Dict[str, int]\ndata = typedload.load(json.load(f), Dict[str, int])\n```\n\nSo when using Mypy, it makes sense to make sure that the type is correct,\nrather than hoping the data will respect the format.\n\nExtending\n=========\n\nType handlers can easily be added, and existing ones can be replaced, so the library is fully cusomizable and can work with any type.\n\nInheriting a base class is not required.\n\nInstall\n=======\n\n* `pip install typedload`\n* `apt install python3-typedload`\n* Latest and greatest .deb file is in [releases](https://github.com/ltworf/typedload/releases)\n\nDocumentation\n=============\n\n* [Online documentation](https://ltworf.github.io/typedload/)\n* In the docs/ directory\n\nThe tests are hard to read but provide more in depth examples of\nthe capabilities of this module.\n\nUsed by\n=======\n\nAs dependency, typedload is used by those entities. Feel free to add to the list.\n\n* [Lyft](https://eng.lyft.com/python-upgrade-playbook-1479145d52f4)\n* Several universities around the world (via [Relational](https://ltworf.github.io/relational/))\n* People who love IRC (via [localslackirc](https://github.com/ltworf/localslackirc))\n* No clue but it gets thousands of downloads per day [according to pypi](https://pypistats.org/packages/typedload)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltworf%2Ftypedload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fltworf%2Ftypedload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltworf%2Ftypedload/lists"}