I am aware of MonkeyType, but as far as I know they are "only" using unit tests to infer types. This means if there are no unit tests for a piece of code, MonkeyType cannot help.
But sometimes you can still guess the type from the structure. I've added examples below.
Is there something like that? Possibly a machine learning project which takes annotated code as input and builds a model to represent which variable names/methods typically represent which classes.
def a(b):
c, d = 0, 1
for _ in range(b):
c, d = d, c+d
return c
Even if you don't know what the code is doing, it is very reasonable to assume that b is an integer as it is used as an argument to range.
def a(b):
return b.split('.')[-1]
It is likely that b is a string, although a lot more is possible
Some parameter names also give clear indicators:
config => Dict[str, Any]index => intdate => date or datetimey => float or intThanks for asking! There's an experimental mypy feature, mypy suggest, that can do this (under limited circumstances).
If you need more help I recommend Gitter.
I'm sorry, there are other tools that do this too. In particular pytype implements your "example 2" and make-stub-files can do (3).
Thank you so much! I will have a look at all of them :-) (And thank you for developing / steering Python for so many years!)
Also, MonkeyType doesn't use exclusively unit tests鈥攊t can also be used at runtime (at some performance cost) to gather type information.
Most helpful comment
Also, MonkeyType doesn't use exclusively unit tests鈥攊t can also be used at runtime (at some performance cost) to gather type information.