Welcome to Pytyp’s documentation!

Utilities that help you write declarative code: instead of saying how something should be done, you describe what the results look like (examples).

Pytyp can help you:

Describe Python 3 data in more detail
pytyp.spec.abcs makes it easier to say what you want, and to write libraries that support a declarative style.
Map serialised data to Python objects (and back again)
pytyp.s11n contains modules (pytyp.s11n.json and pytyp.s11n.yaml) that transform JSON and YAML data. This is a good example of declarative code - you say what classes you want, and the routine works out how to construct them.
Verify function arguments
pytyp.spec.check provides a decorator to verify that function arguments are of the type expected.
Use dynamic dispatch by type
pytyp.spec.dispatch module lets you split complex functions into separate parts, depending on the arguments given.
Use attributes instead of [], and vice versa
pytyp.spec.record contains a useful class that is both a dict and an object.


The ideas behind this library are described in the paper Algebraic ABCs.

Installation and Support

To install from PyPI:

easy_install pytyp


This project is Python 3 only.

For source see github; for support raise an issue there or email Andrew Cooke.

Indices and tables


Testing type specifications (pytyp.spec.abcs):

>>> isinstance([1,2,None,3], Seq(Opt(int)))
>>> isinstance([1,2,None,3.0], Seq(Opt(int)))

Creating Python classes from JSON data:

Verifying function arguments (pytyp.spec.check):

>>> def myfunction(a:int, b:str) -> int:
...     return len(a * b)
>>> myfunction(2, 'foo')
>>> myfunction('oops', 'banana')
Traceback (most recent call last):
TypeError: Type str inconsistent with 'oops'.

Table Of Contents

Next topic

Type Specifications (pytyp.spec.abcs)

This Page