Skip to content

How to add types to a Python codebase

Tools

1. autotyping

https://pypi.org/project/autotyping/

Autotyping uses static analysis via libcst to add simple, “obvious” type annotations like None returns and literal defaults (x: int = 0). It’s safe and conservative with no runtime overhead, but its scope is limited to annotations it’s very confident about.

2. pyrefly infer

https://pyrefly.org/en/docs/autotype/

Pyrefly infer leverages Meta’s Pyrefly type checker to perform static type inference. It offers a powerful inference engine while remaining easy to set up.

3. infer-types

https://github.com/orsinium-labs/infer-types

Infer-types is a lightweight tool that leverages existing type checkers (mypy or pyright) to collect type information and apply annotations. It requires one of these type checkers to be installed.

4. MonkeyType

https://monkeytype.readthedocs.io/

MonkeyType takes a different approach by tracing types at runtime during test execution. This allows it to discover real-world types from actual usage. However, it requires good test coverage and may produce incorrect annotations if coverage is imperfect or if mocks and stubs are used.

Recommendations

Scenario Recommended Tool
Quick start, low risk autotyping
General purpose, easy setup pyrefly infer
Already using mypy/pyright infer-types
Strong test suite, no mocks monkeytype

A suggested workflow is to start with autotyping to add obvious annotations, then run pyrefly infer for deeper static inference. If you have comprehensive tests without heavy mocking, you can optionally use monkeytype as well. Finally, validate the results with mypy or pyright in strict mode.

References

Page last modified: 2025-11-26 16:08:15