Skip to main content

Auto generate typed dicts.

Project description

typegen Is a Typed Dict Generator

What it works for?

  • Basic dictionaries
  • Nested dictionaries
  • Dictionaries with Lists if the items are of same type
  • Any level of nesting as long as list items are of same type
  • NoneType values default to Optional[Any]

When it fails

Each generation gets tested by pytype. In the case it fails it will let you know and provide a link to typegen Issues.

In many cases of failure:

It created the type, but didn't import something needed like a datetime.

I'm looking for a clever way to handle the importing, but this should be a quick fix for now.

Usage:

usage: typegen [-h] -f FILE -v VAR -t TYPE_NAME [-o OUTPUT]

Import a variable from a file.

options:
  -h, --help            show this help message and exit
  -f FILE, --file FILE  Path to the python file (without .py extension)
  -v VAR, --var VAR     Variable name in the file
  -t TYPE_NAME, --type-name TYPE_NAME
                        Name for the type
  -o OUTPUT, --output OUTPUT
                        Path to the output file (default: out/<name of variable>.py)

Example:

File: tests/fish_obj.py
fish = {
    'fish': {
        "health":3,
        "location":{
            "city": "belo",
            "state": "kansas"
        },
        "items": [
            {"item_id": 1, "value":4, "name": "scales", "variations": ["weak", "strong"]},
            {"item_id": 2, "value":4, "name": "knife", "variations": ["weak", "strong"]},
            {"item_id": 1, "value":4, "name": "scales", "variations": ["weak", "strong"]}  
        ]
        
    }
}

Run

typegen -f tests/fish_obj -v fish -t FishType

Output:

File: out/FishType.py
from typing import TypedDict, List, Optional, Any

class ItemsList(TypedDict):
    item_id: int
    value: int
    name: str
    variations: List[str]

class Location(TypedDict):
    city: str
    state: str

class Fish(TypedDict):
    health: int
    location: Location
    items: List[ItemsList]

class FishType(TypedDict):
    fish: Fish

Maintenance

I will work to patch big issues with generating TypedDicts but will not expand the scope of the project for now.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typegen-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typegen-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file typegen-0.1.2.tar.gz.

File metadata

  • Download URL: typegen-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for typegen-0.1.2.tar.gz
Algorithm Hash digest
SHA256 35ccad03a069edae422505301751a8c50bf2a1b795875a46bdf5c4a011a43fa3
MD5 dffc858c8e7cbe38857e2f2909429d00
BLAKE2b-256 eb85832740fc4a27259642d6abb431da3ecaa21a5f6e0c159fc35826c6a91d4b

See more details on using hashes here.

File details

Details for the file typegen-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: typegen-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for typegen-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dd01d7ebe3bf5ee44d6290b08ec57ebffc6e1c0b7087497ef51e33a4056046d8
MD5 825e6912ad58469ca96a9dd23b646a5c
BLAKE2b-256 7c81f8b359f15a2ae47210120667c66ecca1dc4ab8c89f08a57cccf5d384dc62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page