Skip to main content

Python Type Checking

Project description

Promises is a little Python toolkit for maintaining some sanity in dynamically typed languages. You can easily declare the signature of your functions using decorators:

from promises import accepts, returns

# implement f(x) = x + 1
def f(x):
    return x+1

Like it?

You can install the library via pip or just clone the github repository in order to use it in your project:

$ pip install Promises

Tiny Documentation

Promises opens up four very basic functions to you, all which effects can be chained on one another. They are the accepts, returns, rejects, requires, and even implements, throws, and exposes. All of them raise TypeError when the function wrapped is invoked with the wrong arguments with the exception of throws which raises a RuntimeError.


Declares that the decorated function will accept only arguments (either positional or keyword, it doesn’t matter during declaration) of the particular type. For example to implement a method that will only accept strings:

def method(s):


Declares that your function will only return objects of the given type. For example to make sure the function returns a list:

def addone(s):
    return [i+1 for i in s]

Note that the function will not be recursive so you can return containers containing any object you want, just make sure the container is of the specified type.


Logically negative version of the accepts function, that will accept either keyword or positional arguments that are not of the type. For example to implement a function that will reject lists:

def func(x):
    return x


Declares that the function requires one or more specific keyword arguments upon function invocation:

def greet(name="John"):
    return "Hello %s" % (name)


This function is inspired by the concept of interfaces in the Go language- in which you can pass in objects only if they implement or have the required methods.:

def copy(x):
    return x.copy()


Declares that your function exposes the given keyword arguments only. This is useful when you want to force your API user to explicitly set a previous value before another parameter, i.e.:

def make(obj, type_=int, bits=0):
    # ...


Declares that your decorated function does not allow the use of particular keyword arguments. This is useful to enforce the use of positional arguments to emphasize their importance:

def pythagoreas(a, b):
    return (a**2 + b**2) ** 0.5


Signature to declare that your function will throw the given exceptions, and if it doesn’t, the throws function will raise a RuntimeError. Usage:

def div(num, by=1):
    if by == 0:
        raise ValueError
    return num/by


Declares that the decorated function will only accept positional or keyword (again, like accepts and rejects, it doesn’t really matter during declaration) and will implement the methods defined in the given Implementation objects. Usage:

from promises.implementation import Implementation

stack = Implementation(object)

def f(x):
    # do something!

Running the tests

You can also run the test suite for the current version of the promises library by running the command below:

$ git clone ssh://
$ python promises/

Project details

Release history Release notifications

History Node


History Node


This version
History Node


History Node


History Node


History Node


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
Promises-0.0.5.tar.gz (6.4 kB) Copy SHA256 hash SHA256 Source None Jan 24, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page