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.abcsmakes 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.s11ncontains modules (
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.checkprovides a decorator to verify that function arguments are of the type expected.
- Use dynamic dispatch by type
pytyp.spec.dispatchmodule lets you split complex functions into separate parts, depending on the arguments given.
- Use attributes instead of
, and vice versa
pytyp.spec.recordcontains a useful class that is both a dict and an object.
The ideas behind this library are described in the paper Algebraic ABCs.
This package is unused and largely unmaintained. Python went in a different direction with types.
Installation and Support¶
To install from PyPI:
This project is Python 3 only.
Testing type specifications (
>>> isinstance([1,2,None,3], Seq(Opt(int))) True >>> isinstance([1,2,None,3.0], Seq(Opt(int))) False
Creating Python classes from JSON data:
Verifying function arguments (
>>> def myfunction(a:int, b:str) -> int: ... return len(a * b) >>> myfunction(2, 'foo') 6 >>> myfunction('oops', 'banana') Traceback (most recent call last): ... TypeError: Type str inconsistent with 'oops'.