Skip to main content

PyPI version Python version PyPI downloads License Code style: Black

JSON Schema-powered type annotations

This package provides a way to automatically produce type annotations based on jsonschema-schemas.

Not all concepts covered by jsonschema are expressible within Python typing annotations. However, most things will work like you'd expect. Most types are translated trivially (integer, number, string, array, boolean and null). The interesting type is object, which is translated into a TypedDict.

Warning: This is based on the mypy plugin system, which is stated to have no backwards compatibility guarantee. New versions of mypy might not be supported immediately.

Example

A JSON schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://foo.qwerty/some/schema#",
    "title": "Foo Schema",
    "type": "object",
    "properties": {
        "title": {
            "type": "string"
        },
        "awesome": {
            "type": "number"
        }
    },
    "required": ["title"]
}

A TypedDict:

from typing import TYPE_CHECKING
from jsonschema_typed import JSONSchema

data: JSONSchema["path/to/schema.json"] = {"title": "baz"}

if TYPE_CHECKING:
    reveal_type(data)  # Revealed type is 'TypedDict('FooSchema', {'title': builtins.str,
                       #                                           'awesome'?: Union[builtins.int, builtins.float]})'
data["description"] = "there is no description"  # TypedDict "FooSchema" has no key 'description'
data["awesome"] = 42
data["awesome"] = None  # Argument 2 has incompatible type "None"; expected "Union[int, float]"

You can also get types of parts of a schema, as well as types of elements in arrays. Take a look at the test cases for more examples of usage.

Installation

pip install jsonschema-typed

You also need to enable the plugin(s) in your mypy.ini configuration file:

# mypy.ini
[mypy]
plugins = jsonschema_typed.plugin, jsonschema_typed.optional_typed_dict

# Due to a quirk of how these type hints are generated, mypy's caching breaks.
# Disabling caching might be required.
cache_dir = /dev/null

Requirements

The above installations resolves the dependencies, which consist of mypy and jsonschema (naturally). Testing has been done with versions:

  • mypy==0.761
  • jsonschema==3.2.0

Probably some older versions will also work. Report an issue if you need other versions.

Limitations

Metadata

Release files for jsonschema-typed 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for jsonschema-typed 0.8.1
File Interpreter ABI Platform Actions
jsonschema_typed-0.8.1-py3-none-any.whl Python 3 none any Details for jsonschema_typed-0.8.1-py3-none-any.whl

Release files / jsonschema_typed-0.8.1-py3-none-any.whl

Download URL jsonschema_typed-0.8.1-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52509fa251b390b56576be5a837382d6b8c60fab1ded4f235f0202b4af45006e
BLAKE2b-256 checksum
How to use checksums
fd396380d9561d071114ed02ea738ed90f0c85e6a96e9cad883922e3fef68bb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.2

Security for jsonschema-typed 0.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page