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?