MonkeyType is a Python library that automatically generates type annotations from sampled production types. It uses Python's `sys.setprofile` hook to record the types of function arguments, return values, and yield values at runtime. Based on this collected data, it can generate stub files (`.pyi`) or apply draft type annotations directly to existing Python code. The current stable version is 23.3.0, with a history of regular updates focusing on Python version compatibility and annotation accuracy.
pip install monkeytypeVerified import paths — ran on the pinned version, not inferred.
The quickstart demonstrates the typical workflow: first, run your Python code with `monkeytype run` to collect runtime type traces. Then, use `monkeytype stub` to generate a `.pyi` stub file with annotations, or `monkeytype apply` to inject the annotations directly into your Python source file.
Upgrade Python to 3.7+ and MonkeyType to the latest version.
Manually review and refine generated type annotations for correctness and abstraction.
For `script.py`, create `run_script.py`: `from script import main; main()`, then `monkeytype run run_script.py` and target `script` for stubs/apply.
Wrap module-level execution code in `if __name__ == '__main__':` blocks.
Upgrade MonkeyType to version 22.2.0 or newer.
Upgrade MonkeyType to version 22.2.0 or newer.
Ensure `libcst` is installed and meets the required version (`libcst>=0.3.5` for MonkeyType 20.5.0+). Running `pip install monkeytype` typically handles this.
Upgrade MonkeyType to version 23.3.0 or newer. Additionally, ensure your `monkeytype_config.py` is free of syntax or runtime errors.