Giter Club home page Giter Club logo

jsonschema-gentypes's Introduction

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

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:

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:

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.

jsonschema-gentypes's People

Contributors

c2c-bot-gis-ci avatar chlandsi avatar dependabot[bot] avatar peteole avatar pre-commit-ci[bot] avatar renovate-bot avatar renovate[bot] avatar sbrunner avatar snocorp avatar untitaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonschema-gentypes's Issues

missing type is not inferred correctly

{"properties": {"foo": {"type": "string"}}, "required": "foo"} is recognized by json schema validators as being a JSON object, but jsonschema-gentypes converts that into Any

I cannot find any indication in the spec as to why type can be omitted. Perhaps it is an artifact of older specs?

Make more intellisense friendly (class + docstrings)

Perhaps I'm missing an option, but could the types generated use a class and docstings, instead of the constructor and comments, which don't show up in intellisense.

E.g. instead of:

# A Widget is a blah blah blah.

#  @see https://google.com
Hello = TypedDict(
    "Hello",
    {
        # The ID of the tenant this event is associated with and will be delivered to. For more on tenants @see https://apptrail.com/docs/applications/guide/working-with-tenants.
        #
        # pattern: ^[A-Za-z0-9_\-+=]*$
        #
        # required
        "id": str,
    }
)

generate:

class Hello(TypedDict):
    """
    A Widget is a blah blah blah.

    @see https://google.com
    """
    
    a: str
    """
    The ID of the widget, blah blah.
    
    pattern: ^[A-Za-z0-9_\-+=]*$
    
    required
    """

which enables:

Generated code import ordering is random

The list of imports are collected in a set() (I believe) and so their ordering is not stable. We should either sort them when emitting code or use a stable method of making them unique (e.g. a dict()). Having the code change every time it is regenerated makes for undesirable diff churn when generated code is checked into a repo and regularly regenerated as part of automated tooling.

Docstring of objects does not end up in generated code

All typeddicts do not contain any jsonschema description.

There seems to be a pretty serious confusion as to whether jsonschema_gentypes.Type is supposed to contain description property or whether _comments is supposed to be used.

Ignoring / not generating code for required fields when oneOf

I am using jsconschema-gentypes for synchronizing types between a rust codebase and python ( very usefull!)

However, currenctly struggling since jsonschema-gentypes seems to ignored required shared fields on objects that are also "onOf" (enums).

For example, the following schema should generate a "Deal" type that also has a "agio" (required) field - but it's completely missing in the generated python code:

{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Both", "type": "object", "required": [ "input", "output" ], "properties": { "input": { "$ref": "#/definitions/Deal" }, "output": { "$ref": "#/definitions/Deal" } }, "definitions": { "Deal": { "type": "object", "oneOf": [ { "type": "object", "required": [ "maturity" ], "properties": { "maturity": { "type": "string", "format": "date" } } } ], "required": [ "agio" ], "properties": { "agio": { "type": "number", "format": "double" } } } } }

I. the floowing is a valid object:

{ "input": { "maturity": "2022-12-02", "agio": 1 }, "output": { "maturity": "2022-12-02", "agio": 1 }, }

Please release sdists

Thanks for jsonschema-gentypes!

I'm interested in re-packaging jsonschema-gentypes on conda-forge. Typically, that ecosystem prefers to source upstreams from canonical upstreams, in this case a .tar.gz on PyPI. This is hopefully a single-line change!

Further on the wishlist would that it included the test suite.

In the meantime, will continue with the github tarball, but these have the shortcoming of being changeable, either due to the underlying tag changing, or Github changing how it does its packing.

`properties` and `additionalProperties`

When we have properties and additionalProperties like:

{
  "propterites": {
    "id": {"type": "number"}
  },
  "additionalProperties": {"type": "string"}
}

It is interpreted as:

TypeTyped = TypedDict(
    "TypeTyped",
    {
        "type": Union[int, float]
    },
    totaf=False,
)

Type = Union[TypeTyped, Dict[str, str]]

But it's not entirely correct.

See also: python/mypy#6131

Issues with $ref resolution in multiple schema files

It seems like resolving $refs in file's other than the root crashes in odd ways. The RefResolver.resolver base_url is always seems to be that of the root schema. I created a minimal reproducible example where the local anchor of "#/$defs/bar" is attempted to be resolved in foo.schema.json (which crashes) instead of being resolved in bar.schema.json.

foo.schema.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "./bar.schema.json"
}

bar.schema.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "bar": {
      "$ref": "#/$defs/bar"
    }
  },
  "$defs": {
    "bar": {
      "type": "string"
    }
  }
}
# ok!
$ jsonschema-gentypes --json-schema=bar.schema.json --python=bar.py
# errors
$ jsonschema-gentypes --json-schema=foo.schema.json --python=foo.py
Processing foo.schema.json
Traceback (most recent call last):
  File "/opt/python/3.10.8/lib/python3.10/site-packages/referencing/_core.py", line 186, in pointer
    contents = contents[segment]  # type: ignore[reportUnknownArgumentType]  # noqa: E501
KeyError: '$defs'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/resolver.py", line 174, in lookup
    return self.resolver.lookup(uri).contents
  File "/opt/python/3.10.8/lib/python3.10/site-packages/referencing/_core.py", line 594, in lookup
    return retrieved.value.pointer(pointer=fragment, resolver=resolver)
  File "/opt/python/3.10.8/lib/python3.10/site-packages/referencing/_core.py", line 188, in pointer
    raise exceptions.PointerToNowhere(ref=pointer, resource=self)
referencing.exceptions.PointerToNowhere: '/$defs/bar' does not exist within {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$ref': './bar.schema.json'}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/codespace/.python/current/bin/jsonschema-gentypes", line 8, in <module>
    sys.exit(main())
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/cli.py", line 105, in main
    process_config(config)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/cli.py", line 322, in process_config
    add_type(schema_all, gen.get("root_name", "Root"), force_name="root_name" in gen)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/cli.py", line 135, in __call__
    base_type = self.api.get_type(self.resolver.auto_resolve(schema), name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api.py", line 163, in get_type
    the_type = self.build_type(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_06.py", line 68, in build_type
    return super().build_type(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api.py", line 428, in build_type
    return self._get_type(schema, schema_type, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api.py", line 463, in _get_type
    return handler(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_04.py", line 127, in object
    struct = {
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_04.py", line 128, in <dictcomp>
    prop: self.get_type(sub_schema, proposed_name + " " + prop, auto_alias=False)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api.py", line 163, in get_type
    the_type = self.build_type(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_06.py", line 68, in build_type
    return super().build_type(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api.py", line 310, in build_type
    return self.ref(schema_core, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_2020_12.py", line 71, in ref
    return super().ref(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_2019_09.py", line 105, in ref
    return super().ref(schema, proposed_name)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/api_draft_04.py", line 373, in ref
    resolved = self.resolver.lookup(ref)
  File "/workspaces/jsonschema-gentypes/jsonschema_gentypes/resolver.py", line 185, in lookup
    raise UnRedolvedException(f"Ref '{uri}' not found") from exception
jsonschema_gentypes.resolver.UnRedolvedException: Ref '#/$defs/bar' not found

doesn't find any types

I'm having an issue where when I run the program it outputs a python file but claims there are no types

# Automatically generated file from a JSON schema


from typing import Any

Config = Any
""" WARNING: we get an schema without any type """

The input is the following (note that it was generated by https://www.npmjs.com/package/typescript-json-schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "BoundingRegion": {
            "properties": {
                "__typename": {
                    "const": "BoundingRegion",
                    "type": "string"
                },
                "bottomLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "bottomRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "pageNum": {
                    "type": "number"
                },
                "topLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "topRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "ContextDomain": {
            "properties": {
                "__typename": {
                    "const": "ContextDomain",
                    "type": "string"
                },
                "context": {
                    "type": "string"
                },
                "contextType": {
                    "type": "string"
                },
                "documentId": {
                    "type": "string"
                },
                "embeddedText": {
                    "type": "string"
                },
                "page": {
                    "properties": {
                        "__typename": {
                            "const": "PageRange",
                            "type": "string"
                        },
                        "end": {
                            "type": "number"
                        },
                        "start": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "query": {
                    "type": "string"
                },
                "ranking": {
                    "type": "number"
                },
                "section": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "Exact": {
            "type": "object"
        },
        "Filter": {
            "properties": {
                "__typename": {
                    "const": "Filter",
                    "type": "string"
                },
                "key": {}
            },
            "type": "object"
        },
        "Incremental": {
            "anyOf": [
                {
                    "$ref": "#/definitions/T_2"
                },
                {
                    "$ref": "#/definitions/{[PinkeyofT]?:Pextends\"$fragmentName\"|\"__typename\"?T[P]:never;}"
                }
            ]
        },
        "InputMaybe": {
            "type": "object"
        },
        "MakeEmpty": {
            "type": "object"
        },
        "MakeMaybe": {
            "allOf": [
                {
                    "$ref": "#/definitions/Omit<T,K>_1"
                },
                {
                    "$ref": "#/definitions/{[SubKeyinK]:T[SubKey];}"
                }
            ]
        },
        "MakeOptional": {
            "allOf": [
                {
                    "$ref": "#/definitions/Omit<T,K>"
                },
                {
                    "$ref": "#/definitions/{[SubKeyinK]?:T[SubKey];}"
                }
            ]
        },
        "Maybe": {
            "type": "object"
        },
        "Omit<T,K>": {
            "type": "object"
        },
        "Omit<T,K>_1": {
            "type": "object"
        },
        "PageRange": {
            "properties": {
                "__typename": {
                    "const": "PageRange",
                    "type": "string"
                },
                "end": {
                    "type": "number"
                },
                "start": {
                    "type": "number"
                }
            },
            "type": "object"
        },
        "PayloadElement": {
            "properties": {
                "__typename": {
                    "const": "PayloadElement",
                    "type": "string"
                },
                "id": {},
                "vector": {
                    "items": {
                        "type": "number"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        },
        "PineconeParallelUpsertParams": {
            "properties": {
                "__typename": {
                    "const": "PineconeParallelUpsertParams",
                    "type": "string"
                },
                "iterablePayload": {
                    "items": {
                        "properties": {
                            "__typename": {
                                "const": "PayloadElement",
                                "type": "string"
                            },
                            "id": {},
                            "vector": {
                                "items": {
                                    "type": "number"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    },
                    "type": "array"
                },
                "poolThreads": {
                    "type": "number"
                }
            },
            "type": "object"
        },
        "PineconeQueryParams": {
            "properties": {
                "__typename": {
                    "const": "PineconeQueryParams",
                    "type": "string"
                },
                "filter": {
                    "properties": {
                        "__typename": {
                            "const": "Filter",
                            "type": "string"
                        },
                        "key": {}
                    },
                    "type": "object"
                },
                "includeMetadata": {
                    "type": "boolean"
                },
                "includeValues": {
                    "type": "boolean"
                },
                "sparseVector": {
                    "properties": {
                        "__typename": {
                            "const": "SparseVector",
                            "type": "string"
                        },
                        "key": {}
                    },
                    "type": "object"
                },
                "topK": {
                    "type": "number"
                },
                "vector": {
                    "items": {
                        "type": "number"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        },
        "Point": {
            "properties": {
                "__typename": {
                    "const": "Point",
                    "type": "string"
                },
                "x": {
                    "type": "number"
                },
                "y": {
                    "type": "number"
                }
            },
            "type": "object"
        },
        "Scalars": {
            "description": "All built-in and custom scalars, mapped to their actual values",
            "properties": {
                "Boolean": {
                    "properties": {
                        "input": {
                            "type": "boolean"
                        },
                        "output": {
                            "type": "boolean"
                        }
                    },
                    "type": "object"
                },
                "Float": {
                    "properties": {
                        "input": {
                            "type": "number"
                        },
                        "output": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "ID": {
                    "properties": {
                        "input": {
                            "type": "string"
                        },
                        "output": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "Int": {
                    "properties": {
                        "input": {
                            "type": "number"
                        },
                        "output": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "JSON": {
                    "properties": {
                        "input": {},
                        "output": {}
                    },
                    "type": "object"
                },
                "String": {
                    "properties": {
                        "input": {
                            "type": "string"
                        },
                        "output": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "SparseData": {
            "properties": {
                "__typename": {
                    "const": "SparseData",
                    "type": "string"
                },
                "indices": {
                    "items": {
                        "type": "number"
                    },
                    "type": "array"
                },
                "values": {
                    "items": {
                        "type": "number"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        },
        "SparseDenseVectors": {
            "properties": {
                "__typename": {
                    "const": "SparseDenseVectors",
                    "type": "string"
                },
                "dense": {
                    "items": {
                        "type": "number"
                    },
                    "type": "array"
                },
                "sparse": {
                    "properties": {
                        "__typename": {
                            "const": "SparseData",
                            "type": "string"
                        },
                        "indices": {
                            "items": {
                                "type": "number"
                            },
                            "type": "array"
                        },
                        "values": {
                            "items": {
                                "type": "number"
                            },
                            "type": "array"
                        }
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "SparseVector": {
            "properties": {
                "__typename": {
                    "const": "SparseVector",
                    "type": "string"
                },
                "key": {}
            },
            "type": "object"
        },
        "T_2": {
            "type": "object"
        },
        "Table": {
            "properties": {
                "__typename": {
                    "const": "Table",
                    "type": "string"
                },
                "bottomLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "bottomRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "boundingRegion": {
                    "properties": {
                        "__typename": {
                            "const": "BoundingRegion",
                            "type": "string"
                        },
                        "bottomLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "bottomRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "pageNum": {
                            "type": "number"
                        },
                        "topLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "topRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "type": "object"
                },
                "caption": {
                    "properties": {
                        "__typename": {
                            "const": "TextBlock",
                            "type": "string"
                        },
                        "area": {
                            "type": "number"
                        },
                        "boundingRegion": {
                            "properties": {
                                "__typename": {
                                    "const": "BoundingRegion",
                                    "type": "string"
                                },
                                "bottomLeft": {
                                    "properties": {
                                        "__typename": {
                                            "const": "Point",
                                            "type": "string"
                                        },
                                        "x": {
                                            "type": "number"
                                        },
                                        "y": {
                                            "type": "number"
                                        }
                                    },
                                    "type": "object"
                                },
                                "bottomRight": {
                                    "properties": {
                                        "__typename": {
                                            "const": "Point",
                                            "type": "string"
                                        },
                                        "x": {
                                            "type": "number"
                                        },
                                        "y": {
                                            "type": "number"
                                        }
                                    },
                                    "type": "object"
                                },
                                "pageNum": {
                                    "type": "number"
                                },
                                "topLeft": {
                                    "properties": {
                                        "__typename": {
                                            "const": "Point",
                                            "type": "string"
                                        },
                                        "x": {
                                            "type": "number"
                                        },
                                        "y": {
                                            "type": "number"
                                        }
                                    },
                                    "type": "object"
                                },
                                "topRight": {
                                    "properties": {
                                        "__typename": {
                                            "const": "Point",
                                            "type": "string"
                                        },
                                        "x": {
                                            "type": "number"
                                        },
                                        "y": {
                                            "type": "number"
                                        }
                                    },
                                    "type": "object"
                                }
                            },
                            "type": "object"
                        },
                        "content": {
                            "type": "string"
                        },
                        "endsWithoutClosure": {
                            "type": "boolean"
                        },
                        "isListItem": {
                            "type": "boolean"
                        },
                        "nearestParentHeading": {
                            "type": "string"
                        },
                        "pageNum": {
                            "type": "number"
                        },
                        "role": {
                            "$ref": "#/definitions/TextBlockRoleType"
                        },
                        "startsMidSentence": {
                            "type": "boolean"
                        }
                    },
                    "type": "object"
                },
                "cells": {
                    "items": {
                        "properties": {
                            "__typename": {
                                "const": "TableCell",
                                "type": "string"
                            },
                            "bottomLeft": {
                                "properties": {
                                    "__typename": {
                                        "const": "Point",
                                        "type": "string"
                                    },
                                    "x": {
                                        "type": "number"
                                    },
                                    "y": {
                                        "type": "number"
                                    }
                                },
                                "type": "object"
                            },
                            "bottomRight": {
                                "properties": {
                                    "__typename": {
                                        "const": "Point",
                                        "type": "string"
                                    },
                                    "x": {
                                        "type": "number"
                                    },
                                    "y": {
                                        "type": "number"
                                    }
                                },
                                "type": "object"
                            },
                            "boundingRegion": {
                                "properties": {
                                    "__typename": {
                                        "const": "BoundingRegion",
                                        "type": "string"
                                    },
                                    "bottomLeft": {
                                        "properties": {
                                            "__typename": {
                                                "const": "Point",
                                                "type": "string"
                                            },
                                            "x": {
                                                "type": "number"
                                            },
                                            "y": {
                                                "type": "number"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "bottomRight": {
                                        "properties": {
                                            "__typename": {
                                                "const": "Point",
                                                "type": "string"
                                            },
                                            "x": {
                                                "type": "number"
                                            },
                                            "y": {
                                                "type": "number"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "pageNum": {
                                        "type": "number"
                                    },
                                    "topLeft": {
                                        "properties": {
                                            "__typename": {
                                                "const": "Point",
                                                "type": "string"
                                            },
                                            "x": {
                                                "type": "number"
                                            },
                                            "y": {
                                                "type": "number"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "topRight": {
                                        "properties": {
                                            "__typename": {
                                                "const": "Point",
                                                "type": "string"
                                            },
                                            "x": {
                                                "type": "number"
                                            },
                                            "y": {
                                                "type": "number"
                                            }
                                        },
                                        "type": "object"
                                    }
                                },
                                "type": "object"
                            },
                            "columnIndex": {
                                "type": "number"
                            },
                            "columnSpan": {
                                "type": "number"
                            },
                            "content": {
                                "type": "string"
                            },
                            "pageNum": {
                                "type": "number"
                            },
                            "rowIndex": {
                                "type": "number"
                            },
                            "rowSpan": {
                                "type": "number"
                            },
                            "topLeft": {
                                "properties": {
                                    "__typename": {
                                        "const": "Point",
                                        "type": "string"
                                    },
                                    "x": {
                                        "type": "number"
                                    },
                                    "y": {
                                        "type": "number"
                                    }
                                },
                                "type": "object"
                            },
                            "topRight": {
                                "properties": {
                                    "__typename": {
                                        "const": "Point",
                                        "type": "string"
                                    },
                                    "x": {
                                        "type": "number"
                                    },
                                    "y": {
                                        "type": "number"
                                    }
                                },
                                "type": "object"
                            },
                            "type": {
                                "$ref": "#/definitions/TableCellType"
                            }
                        },
                        "type": "object"
                    },
                    "type": "array"
                },
                "colCount": {
                    "type": "number"
                },
                "latexFormat": {
                    "type": "string"
                },
                "nearestParentHeading": {
                    "type": "string"
                },
                "pageNum": {
                    "type": "number"
                },
                "plainTextTable": {
                    "type": "string"
                },
                "rowCount": {
                    "type": "number"
                },
                "topLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "topRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "TableCell": {
            "properties": {
                "__typename": {
                    "const": "TableCell",
                    "type": "string"
                },
                "bottomLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "bottomRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "boundingRegion": {
                    "properties": {
                        "__typename": {
                            "const": "BoundingRegion",
                            "type": "string"
                        },
                        "bottomLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "bottomRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "pageNum": {
                            "type": "number"
                        },
                        "topLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "topRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "type": "object"
                },
                "columnIndex": {
                    "type": "number"
                },
                "columnSpan": {
                    "type": "number"
                },
                "content": {
                    "type": "string"
                },
                "pageNum": {
                    "type": "number"
                },
                "rowIndex": {
                    "type": "number"
                },
                "rowSpan": {
                    "type": "number"
                },
                "topLeft": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "topRight": {
                    "properties": {
                        "__typename": {
                            "const": "Point",
                            "type": "string"
                        },
                        "x": {
                            "type": "number"
                        },
                        "y": {
                            "type": "number"
                        }
                    },
                    "type": "object"
                },
                "type": {
                    "$ref": "#/definitions/TableCellType"
                }
            },
            "type": "object"
        },
        "TableCellType": {
            "enum": [
                "ColumnHeader",
                "Content",
                "Description",
                "RowHeader",
                "StubHead"
            ],
            "type": "string"
        },
        "TextBlock": {
            "properties": {
                "__typename": {
                    "const": "TextBlock",
                    "type": "string"
                },
                "area": {
                    "type": "number"
                },
                "boundingRegion": {
                    "properties": {
                        "__typename": {
                            "const": "BoundingRegion",
                            "type": "string"
                        },
                        "bottomLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "bottomRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "pageNum": {
                            "type": "number"
                        },
                        "topLeft": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        },
                        "topRight": {
                            "properties": {
                                "__typename": {
                                    "const": "Point",
                                    "type": "string"
                                },
                                "x": {
                                    "type": "number"
                                },
                                "y": {
                                    "type": "number"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "type": "object"
                },
                "content": {
                    "type": "string"
                },
                "endsWithoutClosure": {
                    "type": "boolean"
                },
                "isListItem": {
                    "type": "boolean"
                },
                "nearestParentHeading": {
                    "type": "string"
                },
                "pageNum": {
                    "type": "number"
                },
                "role": {
                    "$ref": "#/definitions/TextBlockRoleType"
                },
                "startsMidSentence": {
                    "type": "boolean"
                }
            },
            "type": "object"
        },
        "TextBlockRoleType": {
            "enum": [
                "ABSTRACT",
                "FOOTNOTE",
                "NONE",
                "PAGE_FOOTER",
                "PAGE_HEADER",
                "PAGE_NUMBER",
                "SECTION_HEADING",
                "TABLE",
                "TABLE_CAPTION",
                "TITLE"
            ],
            "type": "string"
        },
        "{[PinkeyofT]?:Pextends\"$fragmentName\"|\"__typename\"?T[P]:never;}": {
            "type": "object"
        },
        "{[SubKeyinK]:T[SubKey];}": {
            "type": "object"
        },
        "{[SubKeyinK]?:T[SubKey];}": {
            "type": "object"
        }
    }
}

Preserving case when generating $ref classes

Using Python 3.10.12 and version jsonschema-gentypes 2.4.0

So for this schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "SubresourceUris": {
      "type": "object",
      "properties": {
        "feedback": {
          "type": "string"
        }
      },
      "required": ["feedback"],
      "title": "SubresourceUris"
    }
  },
  "type": "object",
  "properties": {
    "subresource_uris": {
      "$ref": "#/definitions/SubresourceUris"
    }
  },
  "required": [
    "subresource_uris"
  ],
  "title": "ResponseType"
}

when I run:

from jsonschema_gentypes.cli import process_config


config = {
    "python_version": None,
    "generate": [
      {
            "source": "./input.json",
            "destination": "./output.py",
        }
    ],
}
process_config(config)  # type: ignore

I get back the following:

from typing import TypedDict
from typing_extensions import Required


class Responsetype(TypedDict, total=False):
    """ ResponseType. """

    subresource_uris: Required["Subresourceuris"]
    """
    SubresourceUris.

    Required property
    """



class Subresourceuris(TypedDict, total=False):
    """ SubresourceUris. """

    feedback: Required[str]
    """ Required property """

Is there any way to have the output match the case of the input, and have the class be named SubresourceUris instead?

Love the library by the way! It's very nice.

Lookup references from referenced file

I found, that looking up $defs in a file referenced by another file uses the original resolver and the uri designator does not get prefixed.
Attached a minimal example.

Here the most usefull part of the exception:

referencing.exceptions.PointerToNowhere: '/$defs/def_child' does not exist within {'$schema': 'https://json-schema.org/draft/2019-09/schema', 'type': 'object', 'properties': {'child': {'$schema': 'https://json-schema.org/draft/2019-09/schema', '$defs': {'def_child': {'type': 'object'}}, 'allOf': [{}]}}, 'required': ['child']}

minimal_example.zip

Can't handle .. paths + ref path resolution properly

If you run jsonschema-gentypes with a path like --json=../path/to/some.json, and that file has relative $ref entries in it, resolution will fail.

Repro:

$ cat base.json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "sub.json"
}
$ cat sub.json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type":"object",
  "properties": { "x": { "type": "string" } }
}
$ jsonschema-gentypes --json=base.json --python=out.py
Processing base.json
$ cd subdir
$ jsonschema-gentypes --json=../base.json --python=out.py
Processing ../base.json
Traceback (most recent call last):
  File "/Users/dbushong/.pyenv/versions/3.8.14/lib/python3.8/site-packages/referencing/_core.py", line 417, in get_or_retrieve
    resource = registry._retrieve(uri)
  File "/Users/dbushong/.pyenv/versions/3.8.14/lib/python3.8/site-packages/jsonschema_gentypes/resolver.py", line 83, in _open_uri_resolver
    my_resource = referencing.Resource.from_contents(_open_uri(uri))
  File "/Users/dbushong/.pyenv/versions/3.8.14/lib/python3.8/site-packages/jsonschema_gentypes/resolver.py", line 63, in _open_uri
    with open(uri, encoding="utf-8") as open_file:
FileNotFoundError: [Errno 2] No such file or directory: 'sub.json'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dbushong/.pyenv/versions/3.8.14/lib/python3.8/site-packages/referencing/_core.py", line 667, in lookup
    retrieved = self._registry.get_or_retrieve(uri)
  File "/Users/dbushong/.pyenv/versions/3.8.14/lib/python3.8/site-packages/referencing/_core.py", line 424, in get_or_retrieve
    raise exceptions.Unretrievable(ref=uri) from error
referencing.exceptions.Unretrievable: 'sub.json'

Note that it works fine if you don't have .. in your path

Variable name collision with enum case variants

If you have an enum with duplicate-when-case-folded elements, the generated variables will collide.

Min Repro:

$ cat in.json
{"enum":["a","A"]}
$ jsonschema-gentypes --json-schema=in.json --python=out.py
Processing in.json
$ cat out.py
from typing import Literal, Union


_Root = Union[Literal['a'], Literal['A']]
_ROOT_A: Literal['a'] = "a"
"""The values for the '_Root' enum"""
_ROOT_A: Literal['A'] = "A"
"""The values for the '_Root' enum"""

Support for `patternProperties` when there is only one entry

I am working on a project where the JSON schema describes an object that has a single pattern property of type object. This property key is roughly the identifier for the sub-object. In this case the type structure is very similar to when additionalProperties is a dictionary. I've written a simple proof-of-concept and it appears to work nicely in the happy path and could provide a PR or sample code if interested.

Here is an example fragment of a JSON schema that has this pattern:

    "providers": {
      "type": "object",
      "patternProperties": {
        "^[a-z0-9_]{2,64}$": {
          "type": "object",
          "properties": {
           ...
          }
        }
      },

Would you be willing to add support for this into your library?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • Update all patch versions (1.2) (patch) (python, types-pyyaml)
  • Update dependency types-requests to v2.31.0.20240403 (2.3)
  • Update dependency types-requests to v2.31.0.20240403 (2.4)
  • Update dependency types-requests to v2.31.0.20240403 (master)
  • Update dependency prospector-profile-duplicated to v1.3.0 (master)
  • Update dependency prospector-profile-duplicated to v1.3.0 (master)
  • Lock file maintenance (master)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

Branch 1.2
asdf
.tool-versions
  • python 3.8.18
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.2.17
  • ruamel.yaml <0.18.0
  • setuptools >=65.5.1
  • cryptography >=39.0.1
  • pyopenssl >=22.1.0
requirements.txt
  • poetry ==1.6.1
  • poetry-core >=1.1.0a7
  • cryptography >=41.0.0
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.6.2
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • PyYAML 6.0.1
  • pyupgrade 2.37.3
  • black 24.3.0
  • isort 5.10.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.7.7
  • pylint 2.14.5
  • mypy 0.961
  • flake8 4.0.1
  • pytest 7.1.3
  • pytest-cov 3.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.12
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
regex
.pre-commit-config.yaml
  • prettier 2.8.8
ci/config.yaml
  • camptocamp/c2cciutils 1.2.17
Branch 1.3
asdf
.tool-versions
  • python 3.8.18
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.2.17
  • ruamel.yaml ==0.17.40
  • setuptools >=65.5.1
  • pip ==23.3.2
  • protobuf >=3.18.3
  • certifi >=2022.12.7
  • cryptography >=39.0.1
requirements.txt
  • poetry ==1.6.1
  • cryptography >=41.0.0
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.9.1
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • PyYAML 6.0.1
  • pyupgrade 2.37.3
  • black 24.3.0
  • isort 5.10.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.7.7
  • pytest 7.1.3
  • pytest-cov 3.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.12
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
regex
.pre-commit-config.yaml
  • prettier 2.8.8
ci/config.yaml
  • camptocamp/c2cciutils 1.2.17
Branch 1.4
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.4.16
  • setuptools >=65.5.1
  • certifi >=2022.12.7
  • cryptography >=41.0.0
requirements.txt
  • poetry ==1.6.1
  • poetry-core ==1.7.0
  • pip ==23.3.2
  • cryptography >=41.0.0
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.15.0
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • PyYAML 6.0.1
  • pyupgrade 3.0.0
  • black 24.3.0
  • isort 5.10.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.7.7
  • pytest 7.1.3
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
regex
ci/config.yaml
  • camptocamp/c2cciutils 1.4.16
Branch 1.5
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • pyupgrade 3.3.2
  • black 24.3.0
  • isort 5.12.0
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
regex
.pre-commit-config.yaml
  • ruamel.yaml 0.17.40
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • prettier 2.8.8
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 1.6
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.2.2
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.2.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.22.0
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.0.2
  • asottile/pyupgrade v3.3.2
  • PyCQA/isort 5.12.0
  • psf/black 23.3.0
  • PyCQA/prospector v1.9.0
regex
.pre-commit-config.yaml
  • pre-commit 3.2.2
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 1.7
dockerfile
Dockerfile
  • ubuntu 22.04
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/clean.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.2.2
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • ruamel.yaml 0.17.40
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.2.2
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.22.0
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.0.2
  • asottile/pyupgrade v3.3.2
  • PyCQA/isort 5.12.0
  • psf/black 23.3.0
  • PyCQA/prospector v1.9.0
regex
.pre-commit-config.yaml
  • pre-commit 3.2.2
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 2.0
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.2.2
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.2.2
  • referencing 0.27.4
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.22.0
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.0.2
  • asottile/pyupgrade v3.3.2
  • PyCQA/isort 5.12.0
  • psf/black 23.3.0
  • PyCQA/prospector v1.9.0
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.2.2
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 2.1
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.2.2
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.2.2
  • referencing 0.27.4
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.22.0
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.0.2
  • asottile/pyupgrade v3.3.2
  • PyCQA/isort 5.12.0
  • psf/black 23.3.0
  • PyCQA/prospector v1.9.0
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.2.2
  • prettier 2.8.8
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 2.2
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.2.2
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.17.3
  • typing-extensions 4.5.0
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.2.2
  • referencing 0.27.4
  • certifi 2023.7.22
  • urllib3 1.26.18
  • prospector 1.9.0
  • pytest 7.2.2
  • pytest-cov 4.0.0
  • types-requests 2.28.11.17
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v2.7.1
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.22.0
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.0.2
  • asottile/pyupgrade v3.3.2
  • PyCQA/isort 5.12.0
  • psf/black 23.3.0
  • PyCQA/prospector v1.9.0
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.2.2
  • prettier 2.8.8
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 2.3
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v3
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v6
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.5.10
  • pre-commit ==3.3.3
requirements.txt
  • poetry ==1.6.1
  • pip ==23.3.2
poetry
pyproject.toml
  • python >=3.8,<4
  • jsonschema 4.18.6
  • typing-extensions 4.7.1
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.3.3
  • referencing 0.30.2
  • urllib3 2.0.7
  • prospector 1.10.3
  • pytest 7.4.4
  • pytest-cov 4.1.0
  • types-requests 2.31.0.20240311
  • types-pyyaml 6.0.12.20240311
  • prospector-profile-duplicated 0.1.0
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v3.0.3
  • pre-commit/pre-commit-hooks v4.4.0
  • sbrunner/hooks 0.5.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.23.3
  • sirwart/ripsecrets v0.1.7
  • PyCQA/autoflake v2.2.1
  • asottile/pyupgrade v3.10.1
  • PyCQA/isort 5.12.0
  • psf/black 23.7.0
  • PyCQA/prospector v1.10.3
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.3.3
  • prettier 3.0.3
  • poetry 1.6.1
  • poetry 1.6.1
  • pyjson5 1.6.6
  • prospector-profile-duplicated 0.1.0
ci/config.yaml
  • camptocamp/c2cciutils 1.5.10
Branch 2.4
github-actions
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/checkout v4
  • actions/cache v3
  • actions/upload-artifact v4
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v4
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.6.18
  • pre-commit ==3.6.2
  • poetry-plugin-drop-python-upper-constraint ==0.1.0
  • poetry-plugin-export ==1.6.0
  • poetry-plugin-tweak-dependencies-version ==1.5.2
  • poetry-dynamic-versioning ==1.2.0
requirements.txt
  • poetry ==1.7.1
  • pip ==23.3.2
  • poetry-dynamic-versioning ==1.2.0
  • poetry-plugin-export ==1.6.0
  • poetry-plugin-tweak-dependencies-version ==1.5.2
  • poetry-plugin-drop-python-upper-constraint ==0.1.0
poetry
pyproject.toml
  • python >=3.9,<4
  • jsonschema 4.20.0
  • typing-extensions 4.9.0
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.6.2
  • referencing 0.32.1
  • prospector 1.10.3
  • pytest 7.4.4
  • pytest-cov 4.1.0
  • types-requests 2.31.0.20240311
  • types-pyyaml 6.0.12.20240311
  • prospector-profile-duplicated 0.5.1
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v3.1.0
  • pre-commit/pre-commit-hooks v4.5.0
  • sbrunner/hooks 0.6.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.9.0.6
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.27.4
  • sirwart/ripsecrets v0.1.7
  • asottile/pyupgrade v3.15.2
  • PyCQA/autoflake v2.2.1
  • PyCQA/isort 5.13.2
  • psf/black 23.12.1
  • PyCQA/prospector v1.10.3
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.6.2
  • prettier 3.1.1
  • poetry 1.7.1
  • poetry 1.7.1
  • pyjson5 1.6.6
  • prospector-profile-duplicated 0.5.1
ci/config.yaml
  • camptocamp/c2cciutils 1.6.18
Branch master
github-actions
.github/workflows/audit.yaml
  • actions/checkout v4
  • andstor/file-existence-action v3
  • asdf-vm/actions v3
  • andstor/file-existence-action v3
  • ubuntu 22.04
.github/workflows/backport.yaml
  • ubuntu 22.04
.github/workflows/codeql.yaml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • ubuntu 22.04
.github/workflows/delete-old-workflows-run.yaml
  • MajorScruffy/delete-old-workflow-runs v0.3.0
  • ubuntu 22.04
.github/workflows/main.yaml
  • actions/setup-python v5
  • actions/checkout v4
  • actions/checkout v4
  • actions/cache v4
  • actions/upload-artifact v4
  • ubuntu 22.04
  • ubuntu 22.04
.github/workflows/pr-checks.yaml
  • actions/checkout v4
  • ubuntu 22.04
.github/workflows/pull-request-automation.yaml
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v7
  • actions/github-script v7
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry-core >=1.0.0
pip_requirements
ci/requirements.txt
  • c2cciutils ==1.6.18
  • pre-commit ==3.7.0
  • poetry-plugin-drop-python-upper-constraint ==0.1.0
  • poetry-plugin-export ==1.7.1
  • poetry-plugin-tweak-dependencies-version ==1.5.2
  • poetry-dynamic-versioning ==1.2.0
requirements.txt
  • poetry ==1.8.2
  • pip ==24.0
  • poetry-dynamic-versioning ==1.2.0
  • poetry-plugin-export ==1.7.1
  • poetry-plugin-tweak-dependencies-version ==1.5.2
  • poetry-plugin-drop-python-upper-constraint ==0.1.0
poetry
pyproject.toml
  • python >=3.9,<4
  • jsonschema 4.21.1
  • typing-extensions 4.10.0
  • requests 2.31.0
  • PyYAML 6.0.1
  • pinyin 0.4.0
  • romkan 0.2.1
  • romanize 1.0.2
  • pre-commit 3.7.0
  • referencing 0.34.0
  • prospector 1.10.3
  • prospector-profile-duplicated 1.2.0
  • prospector-profile-utils 1.7.2
  • pytest 8.1.1
  • pytest-cov 5.0.0
  • types-requests 2.31.0.20240311
  • types-pyyaml 6.0.12.20240311
pre-commit
.pre-commit-config.yaml
  • pre-commit/mirrors-prettier v3.1.0
  • pre-commit/pre-commit-hooks v4.5.0
  • sbrunner/hooks 1.0.0
  • codespell-project/codespell v2.2.6
  • shellcheck-py/shellcheck-py v0.10.0.1
  • jumanjihouse/pre-commit-hooks 3.0.0
  • python-jsonschema/check-jsonschema 0.28.1
  • sirwart/ripsecrets v0.1.7
  • asottile/pyupgrade v3.15.2
  • PyCQA/autoflake v2.3.1
  • PyCQA/isort 5.13.2
  • psf/black 24.3.0
  • PyCQA/prospector v1.10.3
  • sbrunner/jsonschema-validator 0.1.0
regex
.pre-commit-config.yaml
  • pre-commit 3.7.0
  • prettier 3.2.5
  • poetry 1.8.2
  • poetry 1.8.2
  • pyjson5 1.6.6
  • prospector-profile-duplicated 1.2.0
  • prospector-profile-utils 1.7.2
ci/config.yaml
  • camptocamp/c2cciutils 1.6.18

  • Check this box to trigger a request for Renovate to run again on this repository

List of unused tags in types

List of all tags that actually not listed in another limitation issue and is not take in account in the types (a comment is added)

  • default
  • readOnly
  • writeOnly
  • multipleOf
  • maximum
  • exclusiveMaximum
  • minimum
  • exclusiveMinimum
  • maxLength
  • minLength
  • pattern
  • maxItems
  • minItems
  • uniqueItems
  • contains
  • maxProperties
  • minProperties
  • patternProperties
  • dependencies
  • propertyNames
  • format
  • contentMediaType
  • contentEncoding
  • not
  • oneOf: thread as Union

`root_name` does not affect subtypes or docstrings

When setting the root_name in the generation config, sub types of the root still get the original root name as their prefix. I would expect the prefixes of the sub types to match the provided root_name.

Example:

{
  "title": "MyObject",
  "type": "object",
  "required": ["child"],
  "properties": {
    "child": {
      "type": "object",
      "properties": {
        "foo": {"type": "string"},
      }
    }
  }
}

If we set the root_name to CustomRootName the generated code looks like this:

from typing import TypedDict
from typing_extensions import Required


class CustomRootName(TypedDict, total=False):
    """ MyObject. """

    child: Required["_MyobjectChild"]
    """ Required property """



class _MyobjectChild(TypedDict, total=False):
    foo: str

I was able to hack in a fix by adding this:

            if "root_name" in gen:
                schema_all["title"] = gen["root_name"]

Right before calling add_type on this line:

add_type(schema_all, gen.get("root_name", "Root"), force_name="root_name" in gen)

Then the resulting code looks as expected:

from typing import TypedDict
from typing_extensions import Required


class CustomRootName(TypedDict, total=False):
    """ CustomRootName. """

    child: Required["_CustomRootNameChild"]
    """ Required property """



class _CustomRootNameChild(TypedDict, total=False):
    foo: str

Depends on requests, but doesn't declare it

jsonschema-gentypes attempts to import requests, but pip install jsonschema-gentypes won't install it

I would also like to disable all network access of this tool via CLI flag

Proposal: Workaround for mypy's incorrect implementation of NotRequired

We have a schema definition with two fields, foo_required and foo_optional.

{"properties": {"foo_required": {}, "foo_optional": {}}, "required": ["foo_required"]}

Now consider this code:

x["foo_optional"]

VSCode/pyright does error on the above line because it might crash. Unfortunately, mypy does not.

This issue is tracked in python/mypy#12094 -- unfortunately the issue is open for more than a year.

In a comment I came up with an idea for a workaround where we actually construct multiple TypedDicts and combine them with a Union. This contraption will also cause mypy to find the problem in the above code snippet.

class MyDict(TypedDict, total=False):
    foo_required: Required[int]
    foo_optional: NotRequired[int]


class MinimalDict(TypedDict, total=False):
    foo_required: Required[int]

MyDict2 = Union[MyDict, MinimalDict]

Do you think this would be reasonable to emit by default from jsonschema-gentypes? Or is it too hacky?

We are also considering running pyright in CI to catch those errors, but it would be a bigger project for us.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.