Registry / serialization / warlock

warlock

JSON →
library2.1.0pypypi✓ verified 87d ago

Warlock is a Python object model designed for creating self-validating objects based on JSON Schema and JSON Patch. It ensures data integrity by automatically validating object mutations against a defined schema. The current version is 2.1.0, and releases occur infrequently, often with significant changes between major versions.

pip install warlock
INSTALL
IMPORT
SIG · WARLOCK
W
warlock
serializationpythonv2.1.0
Install
2.4s avg
Import
257ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.0 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.268s · 21.7MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.246s · 22MB
20MB installed
● package 20MB
Code
Verified usage

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

warlock
import warlock
The primary library import is straightforward.
model_factory
import warlock Country = warlock.model_factory(schema)
Models are created using the `model_factory` function from the top-level `warlock` module.

This quickstart demonstrates how to define a JSON Schema, create a Warlock model using `warlock.model_factory`, instantiate an object, modify its properties with automatic validation, and generate a JSON Patch document to track changes.

import warlock # Define your JSON schema schema = { 'name': 'Country', 'properties': { 'name': {'type': 'string'}, 'abbreviation': {'type': 'string'}, 'population': {'type': 'integer'} }, 'additionalProperties': False } # Create a Warlock model factory from the schema Country = warlock.model_factory(schema) # Create an object using your model sweden = Country(name='Sweden', abbreviation='SE', population=9453000) print(f"Country Name: {sweden.name}") print(f"Population: {sweden.population}") # Update a property, which is validated against the schema sweden.population = 10416000 print(f"New Population: {sweden.population}") # Generate a JSON Patch document to track changes sweden.population = 9453000 # Reset for patch example sweden.name = 'Swedish Kingdom' patch_document = sweden.patch print(f"Generated JSON Patch: {patch_document}")
Debug
Known issues
breakingWarlock 1.3.1 and later versions (including 2.x) upgraded their `jsonschema` dependency to be compatible with `jsonschema` v3+, supporting JSON Schema Draft 7. If your project relies on validation against Draft 4 of JSON Schema, you must explicitly pin your `jsonschema` dependency to `<3`.
fix
If your project requires JSON Schema Draft 4, explicitly add `jsonschema<3` to your project's dependencies alongside Warlock. Otherwise, ensure you are using a compatible `jsonschema` version (v3 or later).
affects: 1.3.1 - 2.x
gotchaAttempting to assign a value to an object property that does not conform to the schema's type or constraints (e.g., assigning an `int` to a `string` property) will raise a `warlock.core.InvalidOperation` error.
fix
Always ensure that values assigned to Warlock object properties match the type and constraints defined in the JSON Schema. Catch `warlock.core.InvalidOperation` for robust error handling.
affects: All versions
gotchaIf your JSON schema includes `'additionalProperties': False`, attempting to set any property on a Warlock object that is not explicitly defined in the schema will raise a `warlock.core.InvalidOperation` error.
fix
Review your schema to ensure all expected properties are defined, or set `'additionalProperties': True` in your schema if you intend to allow arbitrary properties. Catch `warlock.core.InvalidOperation` if this behavior is expected.
affects: All versions
Errors
Common errors & fixes
warlock.core.InvalidOperation: Unable to set 'name' to '5'
An attempt was made to assign a value (e.g., an integer) to a property that is defined as a different type (e.g., string) in the JSON schema.
fix
Ensure the assigned value's type matches the `type` specified in the JSON schema for that property. For example, assign a string for a `string` type property: `obj.name = 'New Name'`.
warlock.core.InvalidOperation: Unable to set 'overlord' to 'Bears'
This error typically occurs when the schema for the Warlock model includes `'additionalProperties': False`, preventing the creation of properties not explicitly defined in the schema.
fix
If the property is intended to exist, add it to the `properties` section of your JSON schema. If arbitrary properties should be allowed, set `'additionalProperties': True` in your schema.
ModuleNotFoundError: No module named 'warlock'
The `warlock` library is not installed in the current Python environment or the environment's path is not correctly configured.
fix
Install the library using pip: `pip install warlock`. If using a virtual environment, ensure it is activated.
Upgrade
Version history
2.1.0latest on PyPI · released May 30, 2025
Audit
Dependencies
jsonschemarequiredRequired for JSON Schema validation.
jsonpatchrequiredRequired for JSON Patch functionality.
PythonrequiredWarlock 2.x requires Python 3.9 or newer.
Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
warlock — pip install warlock · libregistry