-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
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.
How would such a "type guessing" tool be used?
- One has a code base without type annotations and possibly with no or few tests
- The type guesser is run and annotates the code in a way so that mypy would not complain about it.
- The annotations are reviewed as the type guessing cannot be perfect
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.
Example 1: Guess by usage within known functions
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.
Example 2: Guess by method usage
def a(b):
return b.split('.')[-1]
It is likely that b is a string, although a lot more is possible
Example 3: Guess by name
Some parameter names also give clear indicators:
config => Dict[str, Any]index => intdate => date or datetimey => float or int
...