I'm working with a large existing Python codebase and would like to start adding in type annotations so I can get some level of static checking. I'm imagining something like Erlang, Strongtalk, or Typed Scheme/Racket.
I've seen quick-and-dirty decorators that insert dynamic checks based on function parameter and return type annotations, but I'm looking for something that is more robust and that performs checks at compile-time.
What tools are available right now for this kind of thing? I'm familiar with compilers and type checking and am definitely willing to improve an incomplete tool if it has a good foundation.
(Note: I'm not interested in a discussion of the pros/cons of static typing.)
EDIT: An example:
def put(d, k, v):
d[k] = v
I'd like to be able to annotate the put function as having type put<K,V>(dict<K,V>, K, V) -> None.
UPDATE: The new PEP 484 (Sep 2014) defines a standard for static typing and type annotations in Python 3.5+. There's a type-checking tool called mypy that is compatible with PEP 484.
d[k] = vword with every single objectdthat has__setitem__, for instance, not just with instance ofdict. Depending on the actual type ofd, there may be various incompatible requirements onk(e.g. hashable, has__index__) and even onv. And every single object has e.g.__hash__, some of them just "don't work" at runtime. Static typechecking is not possible in Python. - user395760__setitem__check seems like a standard structural type check. There are ways to specify a required relationship between the types ofdandk. The selective implementation of__hash__isn't ideal, but Java has the same issue withUnsupportedOperationException. And even if I can't solve every problem, it doesn't mean I can't have a static checker that is still useful for some things. - Kannan Goundan