Is there a tool that will analyze Python code and add type annotations to functions? A human would look at comments, the body of a function, and at code that calls a function to infer the types of function arguments. I wonder if this can be automated. The tool would not have to be 100% accurate to be useful as an aid in manually adding type annotations.
Look at
MonkeyType made bye Instagram.
Our journey to type checking 4 million lines of Python
So can test MonkeyType to see what it dos.
def double(number):
return number * 2.5
print(double(4))
print(double(5.5))
Output:
10.0
13.75
So can take integer or float as input,and output will be float.
Using MonkeyType:
λ monkeytype apply clean
After apply:
from typing import Union
def double(number: Union[float, int]) -> float:
return number * 2.5
print(double(4))
print(double(5.5))