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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typegen-0.1.1.tar.gz.
File metadata
- Download URL: typegen-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5231d7be1707473e39a3f50859189b6adcc8113eca59fd04818c8a9440a0e59e
|
|
| MD5 |
c0fc93b9ef7f02fa60da93eedf289abd
|
|
| BLAKE2b-256 |
fa3284c1573db28150ea3819f2451f63ad9b15aa7a5360e3fa3434c031352f0c
|
File details
Details for the file typegen-0.1.1-py3-none-any.whl.
File metadata
- Download URL: typegen-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f6cd7f5daf8a9062ec4518787fabcad28fee05294b68e87a8d7c523aca83f25
|
|
| MD5 |
538bdb124eaf0864b54bce48dc7741d7
|
|
| BLAKE2b-256 |
66c9bd7a9eb054f791b2ab879a995ea973bcfafb4a0cd974c362b8684b5c0054
|