# JSON Schema generate Python types
kk
Tools to generate Python types based on TypedDict from a JSON schema
## Quick start
install:
```bash
python3 -m pip install --user jsonschema-gentypes
```
Convert a JSON schema to a Python file contains the types:
```bash
jsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>
```
## Config file
You can also write a config file named `jsonschema-gentypes.yaml` with:
```yaml
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:
```bash
jsonschema-gentypes
```
## Default
The default values are exported in the Python file, then you can do something like that:
```python
value_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)
```
## Limitations
Requires Python 3.8
See the [issues with label "limitation"](https://github.com/camptocamp/jsonschema-gentypes/issues?q=is%3Aissue+is%3Aopen+label%3Alimitation).
## Pre-commit hooks
This project provides pre-commit hooks to automatically generate the files.
```yaml
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:
```yaml
pre_commit:
enabled: true
arguments:
- --color=never
```
## OpenAPI3
We can also generate types for OpenAPI3 schemas (automatically detected).
The result of our example in `tests/openapi3.json` can be used in pyramid with for example:
```python
import pyramid.request
from pyramid.view import view_config
from openaoi3 import *
def open_api(func):
def wrapper(request: pyramid.request.Request, **kwargs) -> Any:
typed_request = {}
try:
typed_request{'request_body'} = request.json
except Exception as e:
pass
typed_request{'path'} = request.matchdict
typed_request{'query'} = request.params
return = func(request, request_typed=typed_request, **kwargs)
return wrapper
@view_config(route_name="route_name", renderer="json")
@open_api
def view(
request: pyramid.request.Request,
request_typed: OgcapiCollectionsCollectionidGet,
) -> OgcapiCollectionsCollectionidGetResponse:
return {...}
```
## Contributing
Install the pre-commit hooks:
```bash
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`.
Raw data
{
"_id": null,
"home_page": "https://github.com/camptocamp/jsonschema-gentypes",
"name": "jsonschema-gentypes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "jsonschema, types",
"author": "Camptocamp",
"author_email": "info@camptocamp.com",
"download_url": "https://files.pythonhosted.org/packages/d9/b1/e2dbf18cfe9ffa6650717f1222ae9d8f9445ae0b3f43e8b31994172f3415/jsonschema_gentypes-2.9.3.tar.gz",
"platform": null,
"description": "# JSON Schema generate Python types\n\nkk\nTools to generate Python types based on TypedDict from a JSON schema\n\n## Quick start\n\ninstall:\n\n```bash\npython3 -m pip install --user jsonschema-gentypes\n```\n\nConvert a JSON schema to a Python file contains the types:\n\n```bash\njsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>\n```\n\n## Config file\n\nYou can also write a config file named `jsonschema-gentypes.yaml` with:\n\n```yaml\nheaders: >\n # Automatically generated file from a JSON schema\n# Used to correctly format the generated file\ncallbacks:\n - - black\n - - isort\ngenerate:\n - # JSON schema file path\n source: jsonschema_gentypes/schema.json\n # Python file path\n destination: jsonschema_gentypes/configuration.py\n # The name of the root element\n root_name: Config\n # Argument passed to the API\n api_arguments:\n additional_properties: Only explicit\n # Rename an element\n name_mapping: {}\n # The minimum Python version that the code should support. By default the\n # currently executing Python version is chosen. Note that the output\n # may require typing_extensions to be installed.\n python_version: '3.11'\n```\n\nAnd just run:\n\n```bash\njsonschema-gentypes\n```\n\n## Default\n\nThe default values are exported in the Python file, then you can do something like that:\n\n```python\nvalue_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)\n```\n\n## Limitations\n\nRequires Python 3.8\n\nSee the [issues with label \"limitation\"](https://github.com/camptocamp/jsonschema-gentypes/issues?q=is%3Aissue+is%3Aopen+label%3Alimitation).\n\n## Pre-commit hooks\n\nThis project provides pre-commit hooks to automatically generate the files.\n\n```yaml\nrepos:\n - repo: https://github.com/camptocamp/jsonschema-gentypes\n rev: <version> # Use the ref you want to point at\n hools:\n - id: jsonschema-gentypes\n files: |\n (?x)^(\n jsonschema-gentypes\\.yaml|\n <schema_path>\\.json\n )$\n```\n\nSee also the pre_commit section in the configuration to run the pre-commit just after the generation, for example with:\n\n```yaml\npre_commit:\n enabled: true\n arguments:\n - --color=never\n```\n\n## OpenAPI3\n\nWe can also generate types for OpenAPI3 schemas (automatically detected).\n\nThe result of our example in `tests/openapi3.json` can be used in pyramid with for example:\n\n```python\nimport pyramid.request\nfrom pyramid.view import view_config\nfrom openaoi3 import *\n\ndef open_api(func):\n def wrapper(request: pyramid.request.Request, **kwargs) -> Any:\n typed_request = {}\n try:\n typed_request{'request_body'} = request.json\n except Exception as e:\n pass\n typed_request{'path'} = request.matchdict\n typed_request{'query'} = request.params\n\n return = func(request, request_typed=typed_request, **kwargs)\n\n return wrapper\n\n\n@view_config(route_name=\"route_name\", renderer=\"json\")\n@open_api\ndef view(\n request: pyramid.request.Request,\n request_typed: OgcapiCollectionsCollectionidGet,\n) -> OgcapiCollectionsCollectionidGetResponse:\n return {...}\n```\n\n## Contributing\n\nInstall the pre-commit hooks:\n\n```bash\npip install pre-commit\npre-commit install --allow-missing-config\n```\n\nThe `prospector` tests should pass.\n\nThe code should be typed.\n\nThe code should be tested with `pytests`.\n\n",
"bugtrack_url": null,
"license": "BSD-2-Clause",
"summary": "Tool to generate Python types based on TypedDict from a JSON Schema",
"version": "2.9.3",
"project_urls": {
"Homepage": "https://github.com/camptocamp/jsonschema-gentypes",
"Repository": "https://github.com/camptocamp/jsonschema-gentypes"
},
"split_keywords": [
"jsonschema",
" types"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9279a0e4a589df06e9d5f9bb97982042e6a87970e8bd67c67f1fb1cb6a910415",
"md5": "63b5814a0b3b6ddcf34e9edbca058200",
"sha256": "4094778b463c352956f25401882987b1197d66597fbccd03a0dd39aeffe62625"
},
"downloads": -1,
"filename": "jsonschema_gentypes-2.9.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "63b5814a0b3b6ddcf34e9edbca058200",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 44969,
"upload_time": "2024-12-13T15:29:26",
"upload_time_iso_8601": "2024-12-13T15:29:26.908342Z",
"url": "https://files.pythonhosted.org/packages/92/79/a0e4a589df06e9d5f9bb97982042e6a87970e8bd67c67f1fb1cb6a910415/jsonschema_gentypes-2.9.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d9b1e2dbf18cfe9ffa6650717f1222ae9d8f9445ae0b3f43e8b31994172f3415",
"md5": "69a258faf3cc16c69adba637a5cd445a",
"sha256": "3ff49735aba5bd23e7f736e3553444226defdafe89a83dc2f2c6799a6d500f34"
},
"downloads": -1,
"filename": "jsonschema_gentypes-2.9.3.tar.gz",
"has_sig": false,
"md5_digest": "69a258faf3cc16c69adba637a5cd445a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 28913,
"upload_time": "2024-12-13T15:29:37",
"upload_time_iso_8601": "2024-12-13T15:29:37.640100Z",
"url": "https://files.pythonhosted.org/packages/d9/b1/e2dbf18cfe9ffa6650717f1222ae9d8f9445ae0b3f43e8b31994172f3415/jsonschema_gentypes-2.9.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-13 15:29:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "camptocamp",
"github_project": "jsonschema-gentypes",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "poetry",
"specs": [
[
"==",
"1.8.4"
]
]
},
{
"name": "pip",
"specs": [
[
"==",
"24.3.1"
]
]
},
{
"name": "poetry-dynamic-versioning",
"specs": [
[
"==",
"1.4.1"
]
]
},
{
"name": "poetry-plugin-export",
"specs": [
[
"==",
"1.8.0"
]
]
},
{
"name": "poetry-plugin-tweak-dependencies-version",
"specs": [
[
"==",
"1.5.2"
]
]
},
{
"name": "poetry-plugin-drop-python-upper-constraint",
"specs": [
[
"==",
"0.1.0"
]
]
}
],
"lcname": "jsonschema-gentypes"
}