Tools for static type checking in Python
Edit 2016-11-11: Just use mypy. Type hints can be added gradually. In Python 3 source code, it verifies standard PEP 484 type hints. Types can still be expressed in Python 2 using special comments. Guido likes it.
This post was originally written a long time ago before mypy was a thing. I've preserved the post's original content below, even though it isn't quite accurate.
Original post:
You might want to check out some of the projects mentioned in this related StackOverflow post on static analysis for Python.
In summary:
- pylint
- PyFlakes
- PyChecker
Since Python uses duck typing extensively, things that might be called "type errors" in other languages might end up being "object X doesn't support method Y" in Python.
Edit 2011-05-17:
I agree with delnan that static typing is not possible for Python [apparently wrong]. But since our skepticism doesn't seem to deter you, I can only give you more information on the subject. I present:
- A discussion of type inference for Python. (Other links are from here.)
- Guido van van Rossum's articles on adding optional static typing: part 1 and part 2.
- RPython, a subset of Python that might stand a chance of being statically analyzed enough to do some form of type checking.
You may find mypy interesting. It has been proposed for inclusion in Python 3.5 by Guido.
Check out this post: PySonar: a Static Analyzer for Python. PySonar is a tool that infers types using abstract interpretation (partially executing) of code. It finds all possible execution paths of your program and finds all possible types of all variables.
There are basically three versions of PySonar:
- Open-sourced Java (Jython indexer)
- Closed-sourced Java (Hidden in Google)
- Open-sourced Python (mini-pysonar)
None of them (except of closed source one) is fully implemented. But the basic idea is that you can use it as a basis for your work.
I don't know if this helps but for what it's worth, Jeremy Siek at U of Colorado did some work on gradual typing, and I found this doing a quick search. http://www.wiki.jvmlangsummit.com/pdf/28_Siek_gradual.pdf
My guess (I might be wrong) is there isn't any promising open source tools you can find at the moment since his research looks relatively new.
Your best bet might be to contact the authors and ask if they can release their code to you.
There is the 'gradual' package for Python 3; see PIP or the Bitbucket Repo
Apparently this is an implementation by the group around Jeremy Siek who seems to be quite an authority in the field of gradual typing.
Some annotations are apparently necessary, here is an example:
from gradual import *
@typed
def calculate_total(a:int, b:int) -> int:
return a + b//100
As far as annotations go, this is not so bad. I have not used the package so I cannot speak to its quality, but the syntax (and the people behind it) certainly make it look promising.