{"id":15593768,"url":"https://github.com/seven7ty/typeshi","last_synced_at":"2025-04-28T10:46:58.618Z","repository":{"id":248874998,"uuid":"830106543","full_name":"seven7ty/typeshi","owner":"seven7ty","description":"Dict to TypedDict generation and conversion utilities for Python 📖✨","archived":false,"fork":false,"pushed_at":"2024-07-25T17:09:28.000Z","size":40,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-18T15:16:34.607Z","etag":null,"topics":["dict","python","python-package","python-packages","python-typing","python3","typeddict","typing"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/typeshi","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/seven7ty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"patreon":"statch","ko_fi":"seven7ty"}},"created_at":"2024-07-17T15:46:08.000Z","updated_at":"2024-07-25T17:08:53.000Z","dependencies_parsed_at":"2024-10-23T01:48:23.335Z","dependency_job_id":null,"html_url":"https://github.com/seven7ty/typeshi","commit_stats":{"total_commits":19,"total_committers":1,"mean_commits":19.0,"dds":0.0,"last_synced_commit":"1b26beaa780988eda7f9e05f7e810e73e67f937e"},"previous_names":["seven7ty/typeshi"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seven7ty%2Ftypeshi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seven7ty%2Ftypeshi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seven7ty%2Ftypeshi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seven7ty%2Ftypeshi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seven7ty","download_url":"https://codeload.github.com/seven7ty/typeshi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251298133,"owners_count":21566959,"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":["dict","python","python-package","python-packages","python-typing","python3","typeddict","typing"],"created_at":"2024-10-03T00:21:24.613Z","updated_at":"2025-04-28T10:46:58.581Z","avatar_url":"https://github.com/seven7ty.png","language":"Python","readme":"\u003ch1 align=\"center\"\u003e\u003ccode\u003etypeshi\u003c/code\u003e - a dead-simple TypedDict utility\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://pypi.org/project/typeshi/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/v/typeshi?color=3776AB\u0026logo=python\u0026style=for-the-badge\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/typeshi/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/dm/typeshi?color=3776AB\u0026logo=python\u0026style=for-the-badge\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\n_________________\nSometimes when working with Python typing we come across a situation when we need to quickly get a TypedDict object from an existing dict, and then maybe even make it into a self-contained declaration file -- `typeshi` does both of these things while exposing helpful function interfaces and utilities.\nYou can get started right away by looking at the docstrings of the exposed functions listed below in your favorite code editor, or get a general feel for `typeshi` first with the examples that follow.\n\n### Documented functions of `typeshi`\n- **`typeddict_from_dict`**\n- **`declaration_module_from_typeddict`**\n- `save_declaration_module_from_json`\n\n## Installation\n```\n$ pip install typeshi\n```\n\n## A quick example\n`typeshi` exposes two main functions, `typeddict_from_dict` and `declaration_module_from_typeddict`. `typeddict_from_dict` is used to get a TypedDict from a dict instance at runtime, whereas `declaration_module_from_typeddict` generates the contents of a declaration module from a TypedDict.\nThe most common usage of `typeshi` would be something like this:\n```py\n# Let's say I have some JSON file that I load into my app at runtime,\n# maybe it contains something like a locale or a config file...\n# For the sake of this example let this be some sort of locale\nwith open(MY_JSON) as fp:\n    my_dict = json.load(fp)\n\nprint(my_dict['greetings']['evening']['polite'])\n# ^ I can *guess* that this is a string (and that it exists at all),\n# but I'm lacking my Intellisense to be sure, let's change that\n```\n\n```py\n# I kind of wish I could develop easier with the help of type hints of my locale,\n# this is where typeshi comes in:\nimport typeshi\n\n# first, I'll generate my TypedDict\n# since in this example I'm thinking of a locale, I'll name my TypedDict \"Locale\" as well as tell typeshi\n# to convert applicable types into their typing.Literal counterparts, which will make my life easier while coding\ntd = typeshi.typeddict_from_dict(typeddict_name='Locale', original_dict=my_dict, literals_where_possible=True)\n\n# Now my TypedDict is already generated, but it doesn't do much for me if it's just sitting in memory...\n# I'll generate a declaration file so that I can import it and type hint my locale\ndeclaration = typeshi.declaration_module_from_typeddict(td)\n# okay, that's my declaration ready, now I'll just save it to a Python file of my choice...\nwith open('generated/locale_declaration.py', 'w+') as fp:\n    fp.write(declaration)\n```\n```py\n# a generated typeshi declaration file will look something like this:\n\n# generated/locale_declaration.py\nfrom typing import TypedDict, Literal\n\n\nclass GreetingsEvening(TypedDict):\n    polite: Literal['Good evening!']\n    ...\n\nclass Greetings(TypedDict):\n    evening: GreetingsEvening\n    ...\n\nclass Locale(TypedDict):\n    greetings: Greetings\n    ...\n```\n\n```py\n# Okay! With that done, I can now import my TypedDict and use it to type hint my locale\nfrom generated.locale_declaration import Locale\n\nmy_dict: Locale  # tell my editor that this is of my Locale type\nmy_dict['greetings']['evening']['polite']  # Literal['Good evening!']\n```\n\nNow, the locale example is obviously limiting, but the heart of `typeshi`, that being `typeddict_from_dict`, becomes very useful very quickly.\nIt supports custom hooks for types and the naming of nested TypedDict classes, making it very easy to use and flexible -- ***everything is documented within docstrings.***\n\nIt's worth noting the above example is significantly shortened by using the typeshi helper function `save_declaration_module_from_json` as follows:\n```py\nfrom typeshi import save_declaration_module_from_json\n\nsave_declaration_module_from_json(toplevel_cls_name='Locale', json_path='my_json.json',\n                                  declaration_path='generated/locale_declaration.py')\n```\n","funding_links":["https://patreon.com/statch","https://ko-fi.com/seven7ty"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseven7ty%2Ftypeshi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseven7ty%2Ftypeshi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseven7ty%2Ftypeshi/lists"}