MonkeyType
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.
Example
Say some/module.py originally contains:
def add(a, b):
return a + b
And myscript.py contains:
from some.module import add add(1, 2)
Now we want to infer the type annotation of add in some/module.py by running myscript.py with MonkeyType. One way is to run:
$ monkeytype run myscript.py
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.
Running monkeytype stub some.module will output a stub:
def add(a: int, b: int) -> int: ...
Running monkeytype apply some.module will modify some/module.py 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 annotations that reflect the concrete types you use at runtime, but those annotations may not always match the full intended capability of the functions. For instance, add is capable of handling many more types than just integers. Similarly, MonkeyType may generate a concrete List annotation where an abstract Sequence or Iterable would be more appropriate. MonkeyType’s annotations are an informative first draft, to be checked and corrected by a developer.
Motivation
Readability and static analysis are the primary motivations for adding type annotations to code. It’s already common in many Python style guides to document the argument and return types for a function in its docstring; annotations are a standardized way to provide this documentation, which also permits static analysis by a typechecker such as mypy.
For more on the motivation and design of Python type annotations, see PEP 483 and PEP 484.
Requirements
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).
Installing
Install MonkeyType with pip:
pip install MonkeyType
How MonkeyType works
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.
Troubleshooting
Check if your issue is mentioned in the frequently asked questions list.
LICENSE
MonkeyType is BSD licensed.
Changelog
18.1.10
Display retype errors when stub application fails. Merge of #52, fixes #49.
Add --sample-count option to show the number of traces a given stub is based on. Merge of #50, fixes #7. Thanks Tai-Lin.
Add monkeytype run -m for running a module as a script. Merge of #41. Thanks Simon Gomizelj.
Add support for Django’s cached_property decorator. Merge of #46, fixes #9. Thanks Christopher J Wang.
Catch and log serialization exceptions instead of crashing. Fixes #38, merge of #39.
Fix bug in default code filter when Python lib paths are symlinked. Merge of #40. Thanks Simon Gomizelj.
17.12.3
Rewrite imports from _io module to io. (#1, merge of #32). Thanks Radhans Jadhao.
Add Config.cli_context() as a hook for custom CLI initialization and cleanup logic (#28; merge of #29). Thanks Rodney Folz.
17.12.2
Exclude “frozen importlib” functions in default code filter.
Fix passing args to script run with monkeytype run (#18; merge of #21). Thanks Rodney Folz.
Fix generated annotations for NewType types (#22; merge of #23). Thanks Rodney Folz.
17.12.1
Fix using MonkeyType outside a virtualenv (#16). Thanks Guido van Rossum for the report.
17.12.0
Initial public version.
Metadata
Release files for MonkeyType 18.1.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| MonkeyType-18.1.10.tar.gz | 23.0 kB | Without provenance | Details for MonkeyType-18.1.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| MonkeyType-18.1.10-py3-none-any.whl | Python 3 | none | any | Details for MonkeyType-18.1.10-py3-none-any.whl |
Total release size: 53.5 kB
Release files / MonkeyType-18.1.10.tar.gz
| Download URL | MonkeyType-18.1.10.tar.gz |
|---|---|
| Size | 23.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88d4852e38b7ba157dca3a8501fa421b9ca0f94fa9862be09ff5d5c4ad431b39
|
|
BLAKE2b-256 checksum How to use checksums |
da51c0c206a42479f4340a28581a11db5a0a459fe67dc69b83227cfcc93e9ef9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / MonkeyType-18.1.10-py3-none-any.whl
| Download URL | MonkeyType-18.1.10-py3-none-any.whl |
|---|---|
| Size | 30.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db867d155073d2e4e8a06e953203cc15bd797b2fc8721cab040ed62082eae774
|
|
BLAKE2b-256 checksum How to use checksums |
37417b54b49f84f5f882fb9800cdc36dad360ee3fc7fa8a2b1fcea1df880f1c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for MonkeyType 18.1.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.