Registry / testing / hypothesis-jsonschema

hypothesis-jsonschema

JSON →
library0.23.1pypypi✓ verified 22d ago

Hypothesis-jsonschema is a Python library that provides a Hypothesis strategy for generating test data that conforms to a given JSON schema. It is currently in version 0.23.1. As a 0.x release series, it may introduce backwards-incompatible changes before reaching 1.0. It supports JSONSchema drafts 04, 05, and 07, including resolving non-recursive references.

pip install hypothesis-jsonschema
INSTALL
IMPORT
SIG · HYPOTHESIS-JSONSCH
H
hypothesis-jsonschema
testingpythonv0.23.1
Install
3.3s avg
Import
3304ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.23.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 3.272s · 28.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.3s · import 3.336s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

from_schema
from hypothesis_jsonschema import from_schema

Demonstrates the core usage of `from_schema` to generate data matching simple JSON schemas. It includes examples for integers, strings with formats, and a note on common `hypothesis.settings` usage.

from hypothesis import given, settings, HealthCheck from hypothesis_jsonschema import from_schema # Basic example: generate integers within a range @given(from_schema({"type": "integer", "minimum": 1, "exclusiveMaximum": 10})) def test_integers(value): assert isinstance(value, int) assert 1 <= value < 10 # Example with string formats and avoiding null characters # (Note: custom_formats argument can be used for non-standard formats) @given(from_schema({'type': 'string', 'format': 'uuid'})) def test_uuid_string(value): import re assert isinstance(value, str) assert re.match(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', value) # To demonstrate, typically you'd run these with pytest or similar test runner # For standalone execution, you'd call the functions, but Hypothesis normally runs tests implicitly # Example of using settings to suppress health checks if needed (not recommended for general use) @given(from_schema({"type": "number"})) @settings(suppress_health_check=[HealthCheck.filter_too_much]) def test_numbers(value): assert isinstance(value, (int, float)) print("Quickstart examples defined. Run with pytest to execute.")
Debug
Known issues
breakingAs a 0.x series library, `hypothesis-jsonschema` adheres to semantic versioning but may introduce backwards-incompatible changes at any point before version 1.0. The primary source of such breaks often involves schemas that previously behaved ambiguously or produced incorrect values now raising explicit errors.
fix
Always pin to exact minor versions (e.g., `hypothesis-jsonschema==0.23.*`) and review changelogs carefully when upgrading between minor versions.
affects: 0.x series (current: 0.23.1)
gotchaThe library has a known limitation in its support for *recursive* references within JSON schemas (e.g., using `$ref` for self-referential schemas), which are commonly found in complex specifications like OpenAPI. While non-recursive references are supported since v0.11, full recursive support is an ongoing challenge.
fix
For schemas with recursive references, you might need to manually craft strategies for the recursive parts or consider alternative generation methods. Keep an eye on the project's issue tracker for updates on recursive `$ref` support.
affects: <1.0
gotchaThe 0.x versions of `hypothesis-jsonschema` generally require very recent versions of all its dependencies. This is a design choice by the maintainer to avoid complex compatibility workarounds, which means older dependency versions might not work as expected.
fix
Ensure your environment has the latest compatible versions of `hypothesis` and `jsonschema`. If encountering issues, try upgrading these dependencies first.
affects: 0.x series (current: 0.23.1)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'hypothesis_jsonschema'
The `hypothesis-jsonschema` library, or its core dependency `hypothesis`, is not installed in the Python environment.
fix
Install the library using pip: `pip install hypothesis-jsonschema`
ModuleNotFoundError: No module named 'jsonschema.compat'
This error typically occurs when an older version of `jsonschema` (pre-4.0) is expected, but a newer version (4.0+) is installed, or vice-versa, causing a module that was removed or refactored to be missing. This often happens due to dependency conflicts with other libraries that rely on a specific `jsonschema` version.
fix
Downgrade the `jsonschema` package to a compatible version, typically `pip install 'jsonschema<4.0'` if the dependent library requires older versions, or ensure all dependencies are updated to support `jsonschema` 4.0+ if that is the intended version.
jsonschema.exceptions.RefResolutionError: Unresolvable JSON pointer: 'definitions/...' (or similar unresolvable reference error)
This error occurs when the JSON schema contains `$ref` keywords pointing to definitions that the `jsonschema` resolver cannot locate within the provided schema or its specified base URI. This can be due to incorrect paths, external references not being provided, or issues with how `jsonschema` handles self-referencing or nested definitions across different versions.
fix
Ensure that all `$ref` pointers are correct and resolvable. For internal references, make sure the definitions are correctly placed, typically under a `definitions` or `$defs` keyword, and the paths match. For external references, provide a `RefResolver` with appropriate base URIs or local mappings if fetching remote schemas. Upgrading `jsonschema` (e.g., to 4.3.2 or newer) can sometimes resolve bugs related to reference resolution.
DeprecationWarning: jsonschema.exceptions.RefResolutionError is deprecated as of version 4.18.0. If you wish to catch potential reference resolution errors, directly catch referencing.exceptions.Unresolvable.
This is a warning indicating that the `jsonschema` library, a dependency of `hypothesis-jsonschema`, has updated its internal reference resolution mechanism. The old exception `jsonschema.exceptions.RefResolutionError` is deprecated in favor of `referencing.exceptions.Unresolvable` in `jsonschema` versions 4.18.0 and later.
fix
While `hypothesis-jsonschema` might handle this internally, if you are directly catching `jsonschema.exceptions.RefResolutionError` in your tests or application code, you should update your exception handling to catch `referencing.exceptions.Unresolvable` instead (after importing it from `referencing.exceptions`). Alternatively, update `hypothesis-jsonschema` to a version that officially supports and handles these changes in `jsonschema`.
Upgrade
Version history
0.23.1latest on PyPI · released Feb 28, 2024
Audit
Dependencies
hypothesisrequiredCore property-based testing framework that hypothesis-jsonschema extends.
jsonschemarequiredRequired for parsing and understanding JSON schemas (version 0.7.0 required >= 3.0).
Agent activity
7 hits · last 30 days
node
6
Resources
hypothesis-jsonschema — pip install hypothesis-jsonschema · libregistry