jsonschema-gentypes


Namejsonschema-gentypes JSON
Version 2.4.0 PyPI version JSON
download
home_pagehttps://github.com/camptocamp/jsonschema-gentypes
SummaryTool to generate Python types based on TypedDict from a JSON Schema
upload_time2024-01-15 16:37:34
maintainer
docs_urlNone
authorCamptocamp
requires_python>=3.9
licenseBSD-2-Clause
keywords jsonschema types
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # JSON Schema generate Python types

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": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "jsonschema,types",
    "author": "Camptocamp",
    "author_email": "info@camptocamp.com",
    "download_url": "https://files.pythonhosted.org/packages/d7/2b/1f734131e4b973a36510e90917fcebffc7176d3dbe59b5fc7abda8d56d19/jsonschema_gentypes-2.4.0.tar.gz",
    "platform": null,
    "description": "# JSON Schema generate Python types\n\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.4.0",
    "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": "635264db08b8affde526bf29285df49793d7d31458318f04b7e6ccf7d7a69827",
                "md5": "77897eb047b0c167e1d75aea31fa80ad",
                "sha256": "5c4fdeee656bc92da09c24968d6e2c43a8a97e93c5f52a3d816e71b5bdf492bd"
            },
            "downloads": -1,
            "filename": "jsonschema_gentypes-2.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "77897eb047b0c167e1d75aea31fa80ad",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 42373,
            "upload_time": "2024-01-15T16:37:33",
            "upload_time_iso_8601": "2024-01-15T16:37:33.146121Z",
            "url": "https://files.pythonhosted.org/packages/63/52/64db08b8affde526bf29285df49793d7d31458318f04b7e6ccf7d7a69827/jsonschema_gentypes-2.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d72b1f734131e4b973a36510e90917fcebffc7176d3dbe59b5fc7abda8d56d19",
                "md5": "998bae4a0e811e0508da43527ad1fde7",
                "sha256": "c01ddbcff39968e65eda9498a264ff4bb396b5018ba872c04b430f1c7dc3e84d"
            },
            "downloads": -1,
            "filename": "jsonschema_gentypes-2.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "998bae4a0e811e0508da43527ad1fde7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 26552,
            "upload_time": "2024-01-15T16:37:34",
            "upload_time_iso_8601": "2024-01-15T16:37:34.762136Z",
            "url": "https://files.pythonhosted.org/packages/d7/2b/1f734131e4b973a36510e90917fcebffc7176d3dbe59b5fc7abda8d56d19/jsonschema_gentypes-2.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-15 16:37:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "camptocamp",
    "github_project": "jsonschema-gentypes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "jsonschema-gentypes"
}
        
Elapsed time: 0.17185s