Typing Stubs for jsonschema

4.26.0.20260408 · active · verified Thu Apr 09

This package provides static type annotations (stubs) for the `jsonschema` library, enabling tools like `mypy` to perform static analysis on code that uses `jsonschema`. It's part of the typeshed project, a community effort to add types to popular Python packages. The current version, `4.26.0.20260408`, typically tracks the `jsonschema` library's version, with a datestamp for stub updates.

Warnings

Install

Quickstart

Demonstrates basic `jsonschema` validation. When `types-jsonschema` is installed, static type checkers like `mypy` can analyze calls to `jsonschema` functions, providing type hints for arguments and return values, and catching potential type-related errors in your use of `jsonschema` at design time.

import jsonschema
from typing import Any

# Define a simple JSON schema
my_schema: dict[str, Any] = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0}
    },
    "required": ["name", "age"]
}

# Valid data instance
valid_data = {"name": "Alice", "age": 30}
jsonschema.validate(valid_data, my_schema)
print("Valid data validated successfully.")

# Invalid data instance (type mismatch)
invalid_data_type = {"name": "Bob", "age": "twenty"}
try:
    jsonschema.validate(invalid_data_type, my_schema)
except jsonschema.ValidationError as e:
    print(f"Caught expected validation error for invalid type: {e.message}")

# Note: With 'types-jsonschema' installed, a type checker like MyPy
# would analyze calls to `jsonschema.validate`, ensuring correct
# argument types are passed based on the stubs.

view raw JSON →