Python doesn't check types at compile time because it can't, at least in some circumstances. But has anyone come up with a mechanism to do compile-time type checking based on extra annotations by the user? Something like pylint which uses extra guarantees by the author? I'm thinking of something like:
#guarantee(argument=int, return_type=int)
def f(x):
return x + 3
#guarantee(argument=int, return_type=str)
def g(x):
return "%d times" % x
y = f(6)
# works, z = "9 times"
z = g(y)
# error
a = f(z)
This checker would interpret the comments above each function, realize that f(x)
is only supposed to accept int
but z comes from g(x)
so it's a str
. Is there any product which does something similar to this?
PEP 3107 was recently finalized (recently being sometime in the last year) which introduces annotations to variables, and functions. Unfortunately (as you can see from the number of the pep) this only applies to Python 3.x so any checker (or even code) you write to take advantage of this will be Python 3 only (which really isn't a bad thing).
You mention pylint so I assume you don't actually want the checks run at compile time, but instead checked after compilation. This would be an awesome tool to discuss over at the code-quality mailing list.