typan -- Type checked python

This is a hobby project to investigate static type checking in Python. it is not useful code in its current state. It probably won't ever become useful code, but that doesn't matter, this is more exploratory.

Package contents

Currently the package contains 4 module: byteplay, typecheck, typan, and example. byteplay is a 3rd party module that makes it easy to inspect (and modify) python bytecode. typecheck contains the real guts of the code. It has the decorators used to annotate types as well as the code for checking types. typan is a script to perform static type checking from the command line. Finally, example contains some example code that uses type annotations.

Type decorators

The first step in type checking is to declare the types you want to use. (Note: I'm also looking at ways to do type inference, rather than explicit types.)

Currently you can annotate the return type, arguments to a function, and the types of local variables. For example:

@returns(float)
@localtypes(a=float)
def z():
    """Return 1234 as a float."""
    a = float("1234")
    return a

z is a function which has one local variable a, which must be a float. And it returns a float.

Type checking

Type checking is done by inspecting the bytecode of functions. Basically it tracks the type of objects when they are pushed onto the stack and verifies them when they are popped into a variable.

Current status

This is very rudimentary currently. I don't handle branches, tuple types, list types or anything similar. Yes this is a problem, but this is currently really just a sketch of a solution.

Download

Currently only available from my bzr repo:

bzr get http://benno.id.au/bzr/typan.dev