Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Lightweight module to define serializable, schema-validated classes

Project Description

Lightweight Annotated Schema Serializable Objects

.. image::
:alt: Documentation Status

.. image::
:alt: Build Status

.. image::
:alt: Code Coverage

.. |knot-icon| image::

Gnarl |knot-icon| is a small module for `Python <>`_ which
allows defining classes with type-checked attributes, conforming to a
predetermined schema.

Classes with Gnarl |knot-icon| schemas can be used to:

* **Type-check** object attributes.
* **Validate** input data.
* **Deserialize** input data to application objects, with direct support for
deserializing `JSON <>`_.
* **Serialize** application objects to JSON_.


Define a class, with a schema attached to it used to type-check the

>>> from gnarl import Schemed
>>> class Point(Schemed):
... __schema__ = { "x": int, "y": int }

Now values can be created, using keyword arguments to set the values of the
attributes. Note how the attributes can be accessed normally:

>>> location = Point(x=-3, y=5)
>>> location.x, location.y
(-3, 5)

Attributes are type-checked:

>>> location.x = 6 # Succeds
>>> location.x = "invalid value" # Fails
Traceback (most recent call last):
gnarl.SchemaError: 'invalid value' should be instance of <class 'int'>
>>> location.x, location.y # Values remain unchanged
(6, 5)

Last, but not least, conversion to and from JSON is automatically supported:

>>> json_text = location.to_json(sort_keys=True)
>>> json_text
'{"x": 6, "y": 5}'
>>> value = Point.from_json(json_text)
>>> value.__class__.__name__


The stable releases are uploaded to `PyPI <>`_, so you
can install them and upgrade using ``pip``::

pip install gnarl

Alternatively, you can install development versions —at your own risk—
directly from the Git repository::

pip install -e git://


The documentation for Gnarl |knot-icon| is available at:

Note that the documentation is work in progress. In the meanwhile, you may
want to read the source code of the module itself for additional insight,
or even better, the code of `projects using the module`__.

__ users_


If you want to contribute, please use the usual GitHub workflow:

1. Clone the repository.
2. Hack on your clone.
3. Send a pull request for review.

If you do not have programming skills, you can still contribute by `reporting
issues <>`_ that you may


The following projects make use of Gnarl |gnarl-icon|:

* `intheam-python <>`__
* `pebbletime-python <>`__

(If you use it, please do not hesitate in editing this file and add a line to
this list.)

Release History

This version
History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(22.7 kB) Copy SHA256 Hash SHA256
Source None May 6, 2016

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting