Typing Stubs for jsonschema
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
- gotcha This package provides only type hints for static analysis; it does not include any runtime code for the `jsonschema` library itself. You must install `jsonschema` separately to use its functionality.
- gotcha You do not directly import any symbols from `types_jsonschema`. Instead, you import from the `jsonschema` library (e.g., `import jsonschema`), and type checkers automatically discover and use the installed stubs.
- gotcha The versioning `X.Y.Z.YYYYMMDD` indicates the `jsonschema` version the stubs are primarily for (`X.Y.Z`) and the date the stubs were last updated. While `types-jsonschema` aims to be compatible, it might occasionally lag behind the latest `jsonschema` releases.
- breaking Significant breaking changes in `jsonschema` itself (e.g., between major versions) may render older `types-jsonschema` stubs inaccurate or incompatible, leading to type errors or missing types during static analysis.
Install
-
pip install types-jsonschema
Quickstart
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.