MonkeyType collects runtime types of function arguments and return values, and can automatically generate stub files or even add draft type annotations directly to your Python code based on the types collected at runtime.
Run a script under call-trace logging of functions and methods in all imported modules:
$ monkeytype run myscript.py
Or enable call-trace logging for a block of code:
import monkeytype
with monkeytype.trace():
...
By default this will dump call traces into a sqlite database in the file monkeytype.sqlite3
in the current working directory. You can then use the monkeytype
command to generate a stub file for a module, or apply the type annotations directly to your code:
$ monkeytype stub some.module
$ monkeytype apply some.module
If some/module.py
originally contained:
def add(a, b):
return a + b
And myscript.py
contains:
from some.module import add
add(1, 2)
Then you'd get a stub like this from monkeytype stub some.module
:
def add(a: int, b: int) -> int: ...
And if you run monkeytype apply some.module
, some/module.py
will be modified to:
def add(a: int, b: int) -> int:
return a + b
This example demonstrates both the value and the limitations of MonkeyType. With MonkeyType, it's very easy to add type annotations that reflect the concrete types you use at runtime, but those annotations may not always match the full intended capability of the functions (e.g. this add
function is capable of handling many more types than just integers, or MonkeyType may generate a List
annotation where Sequence
or Iterable
would be more appropriate). MonkeyType's annotations are intended as a first draft, to be checked and corrected by a developer.
MonkeyType requires Python 3.6+ and the retype library (for applying type stubs to code files). It generates only Python 3 type annotations (no type comments).
Install MonkeyType with pip:
pip install MonkeyType
MonkeyType uses the sys.setprofile hook provided by Python to interpose on function calls, function returns, and generator yields, and record the types of arguments / return values / yield values.
It generates stub files based on that data, and can use retype to apply those stub files directly to your code.
See the full documentation for details.
Check if your issue is mentioned in the frequently asked questions list.
MonkeyType is BSD licensed.