This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Project Description

typespec is two things:

1. A format specification for function annotations.

2. A module for parsing annotations that follow this specification.

The typespec format

The typespec format uses standard Python data structures to specify object
types. The following types are recognised:

* ``str`` - this is human readable documentation.

* ``type`` - this is a type/class that a conforming object may be an instance of. An object conforms to a typespec if it is an instance of one of the specified types or it passes validation of a validation class.

* ``tuple`` - this is a grouping of other strs, types and tuples. There is no semantic difference between grouped and ungrouped types but grouped strs are documentation that applies only to the types in the same grouping.

Other types are allowed but their meaning is undefined.

Simple Examples

Human readable documentation only - no type restrictions defined::

"some description of a value"

A value that must be a string::


A documented string value::

(str, "description of the meaning of the string contents")

A value that may be a string or a number::

(str, int, float)

A value that may be a string or a number with type specific documentation::

(str, "description of the meaning of the string contents"),
(int, float, "what does the number represent?")

Validation Classes

Sometimes it is desirable to check more than just the type of an object.
For this purpose, validation classes can be defined and used in typespecs.
Validation classes are classes that have a ``__validate__`` method.
Before an object is validated against a validation class, it is checked that
it is an instance of the base class (ValidationClass.__base__) of the
validation class. In order to validate a value, the validation class is
instantiated with that object as the only argument (so it should have an
``__init__`` method definition like: ``def __init__(self, val):``) and then the
``__validate__`` method is called on the resulting object.
If this returns a true value, the object is deemed valid else it is deemed


class PositiveInt(int):
def __validate__(self):
return self > 0

class PositiveFloat(float):
def __validate__(self):
return self > 0.0

class PositiveNumber(object):
def __init__(self, val):
self.val = val

def __validate__(self):
if isinstance(self.val, int):
return PositiveInt(self.val).__validate__()
if isinstance(self.val, float):
return PositiveFloat(self.val).__validate__()

class SingleCharacterString(str):
def __validate__(self):
return len(self) == 1

Using typespecs in Annotations

A typespec can be used in function annotations. For argument annotations, it
specifies the type of argument. For return annotations it specifies the type
of object returned/yielded.


def add_chars(
a : SingleCharacterString,
b : SingleCharacterString) -> SingleCharacterString:
Adds the ordinal values of 2 characters and returns the character with
that ordinal value.
return chr(ord(a) + ord(b))

def sum(a : (int, float), b : (int, float)) -> float:
return float(a + b)

def install_python(
path : (str, "The file path that python should be installed to"),
version : (str, "The version of python to install") = "3.2"
) -> (bool, "True if successful, False if unsuccessful"):
#python installation code

def ords(string : str) -> int:
'''yields the ordinal values of the characters in ``string``.'''
for c in string:
yield ord(c)

The typespec module

The typespec module provides simple utilities for validating values against

Basic Usage

Checking an object against a typespec::

>>> #use the in operator
>>> 1 in typespec.TypeSpec((int, float))
>>> "hello" in typespec.TypeSpec((int, float))

Listing types in a typespec::

>>> #simply iterate over the TypeSpec object
>>> sorted(t.__name__ for t in typespec.TypeSpec((int, str, "doc")))
['int', 'str']

Accessing human readable documentation::

>>> #use the doc method
>>> typespec.TypeSpec("some documentation").doc()
'some documentation'
>>> my_spec = typespec.TypeSpec((
... (str, "description of the meaning of the string contents"),
... (int, float, "what does the number represent?")
... ))
>>> my_spec.doc()
>>> my_spec.doc(str)
'description of the meaning of the string contents'
>>> my_spec.doc(int)
'what does the number represent?'
>>> my_spec.doc(float)
'what does the number represent?'

For more details see the module documentation.
Release History

Release History


This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
typespec-1.0.0.tar.gz (4.0 kB) Copy SHA256 Checksum SHA256 Source Aug 27, 2012

Supported By

WebFaction WebFaction Technical Writing 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 Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting