Tool to generate Python types based on TypedDict from a JSON Schema
Project description
JSON Schema generate Python types
Tools to generate Python types based on TypedDict from a JSON schema
Quick start
install:
python3 -m pip install --user jsonschema-gentypes
Convert a JSON schema to a Python file contains the types:
jsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>
Config file
You can also write a config file named jsonschema-gentypes.yaml
with:
headers: >
# Automatically generated file from a JSON schema
# Used to correctly format the generated file
callbacks:
- - black
- - isort
generate:
- # JSON schema file path
source: jsonschema_gentypes/schema.json
# Python file path
destination: jsonschema_gentypes/configuration.py
# The name of the root element
root_name: Config
# Argument passed to the API
api_arguments:
additional_properties: Only explicit
# Rename an element
name_mapping: {}
# The minimum Python version that the code should support. By default the
# currently executing Python version is chosen. Note that the output
# may require typing_extensions to be installed.
python_version: '3.11'
And just run:
jsonschema-gentypes
Default
The default values are exported in the Python file, then you can do something like that:
value_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)
Limitations
Requires Python 3.8
See the issues with label "limitation".
Pre-commit hooks
This project provides pre-commit hooks to automatically generate the files.
repos:
- repo: https://github.com/camptocamp/jsonschema-gentypes
rev: <version> # Use the ref you want to point at
hools:
- id: jsonschema-gentypes
files: |
(?x)^(
jsonschema-gentypes\.yaml|
<schema_path>\.json
)$
See also the pre_commit section in the configuration to run the pre-commit just after the generation, for example with:
pre_commit:
enabled: true
arguments:
- --color=never
Contributing
Install the pre-commit hooks:
pip install pre-commit
pre-commit install --allow-missing-config
The prospector
tests should pass.
The code should be typed.
The code should be tested with pytests
.
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
Hashes for jsonschema_gentypes-2.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c125ca28c37ae011db888ce38cfc0fcbcd03b670bc890ffe72427477eafc86a |
|
MD5 | 1cfb28689febf85f9adbba0f47db8b00 |
|
BLAKE2b-256 | 72198984d45face2e9f86a3fb7084ef9c21b938dbc753934f2b1809398c86180 |
Hashes for jsonschema_gentypes-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53ec09eb06757881cf3ccfdb28069aefec0198b31d0c4fcb474e843ff7661171 |
|
MD5 | 865cd9ec45e12b393f699c4e24e7a636 |
|
BLAKE2b-256 | 9b0de08dfbc598f49a59063c34e4c4f066e6fdb89c92619ed7decb5806428659 |