Skip to content

Generate python code for typed dictionaries from JSON examples

License

Notifications You must be signed in to change notification settings

Emerentius/typed_dict_generator

Repository files navigation

Typed Dict Generator

This script generates Python code for TypedDicts from Json. TypedDicts are available in the typing module from 3.8+ and in typing_extensions before that.

Usage

$ poetry run python typed_dict_generator.py test_files/response.json 
Dict2 = TypedDict("Dict2", { "some_key": str })
HeterogenousList = TypedDict("HeterogenousList", { "quux": str })
Response = TypedDict("Response", { "name": str, "cond": bool, "floaty": float, "dict": Dict2, "some_list": List[str], "null": None, "heterogenous_list": List[Union[float, str, HeterogenousList]] })

The output can be copied into a python file with a few typing imports prepended. Let your autoformatter make the output pretty.

# If you're using a Python version older than 3.8, you will need
# to import TypedDict from typing_extensions or mypy_extensions
from typing import List, Union, TypedDict

Dict2 = TypedDict("Dict2", {"some_key": str})
HeterogenousList = TypedDict("HeterogenousList", {"quux": str})
Response = TypedDict(
    "Response",
    {
        "name": str,
        "cond": bool,
        "floaty": float,
        "dict": Dict2,
        "some_list": List[str],
        "null": None,
        "heterogenous_list": List[Union[float, str, HeterogenousList]],
    },
)

About

Generate python code for typed dictionaries from JSON examples

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages