A fast runtime type hint assistant for Python code.
Project description
RightTyper
RightTyper is a Python tool that generates types for your function
arguments and return values. RightTyper lets your code run at nearly full speed with
almost no memory overhead. As a result, you won't experience slow
downs in your code or large memory consumption while using it,
allowing you to integrate it with your standard tests and development
process. By virtue of its design, and in a significant departure from previous approaches,
RightTyper only captures the most commonly used types,
letting a type checker like mypy detect possibly incorrect type mismatches in your code.
You can run RightTyper with arbitrary Python programs and it will generate types for every function that gets executed. It works great in combination with pytest:
python3 -m righttyper -m pytest --continue-on-collection-errors /your/test/dir
In addition to generating types, RightTyper has the following features:
- It efficiently computes type annotation "coverage" for a file or directory of files
- It infers shape annotations for NumPy/JAX/PyTorch tensors, compatible with
jaxtypingandbeartypeortypeguard.
Performance Comparison
The graph below presents the overhead of using RightTyper versus two previous tools, MonkeyType and PyAnnotate, across a range of benchmarks. On average, RightTyper imposes only 30% overhead compared to running plain Python ("none"). On one popular package (black), RightTyper imposes only 20% overhead, while RightTyper slows down execution by over 37x. In extreme cases, MonkeyType runs over 3,000x slower than RightTyper.
Usage
Install RightTyper from pip as usual:
python3 -m pip install righttyper
To use RightTyper, simply run your script with python3 -m righttyper instead of python3:
python3 -m righttyper your_script.py [args...]
This will execute your_script.py with RightTyper's monitoring
enabled. The type signatures of all functions will be recorded and
output to a file named righttyper.out. The file contains, for every
function, the signature, and a diff of the original function with the
annotated version. It also optionally (with the --infer-shapes flag)
generates jaxtyping-compatible shape
annotations for NumPy/JAX/PyTorch tensors. Below is an example:
test-hints.py:
==============
def barnacle(x: numpy.ndarray) -> numpy.ndarray: ...
- def barnacle(x):
+ def barnacle(x: numpy.ndarray) -> numpy.ndarray:
# Shape annotations
@beartype
def barnacle(x: Float[numpy.ndarray, "10 dim0"]) -> Float[numpy.ndarray, "dim0"]: ...
def fooq(x: int, y: str) -> bool: ...
- def fooq(x: int, y) -> bool:
+ def fooq(x: int, y: str) -> bool:
? +++++
To add type hints directly to your code, use this command:
python3 -m righttyper --output-files --overwrite your_script.py [args...]
To do the same with pytest:
python3 -m righttyper --output-files --overwrite -m pytest your_directory
Below is the full list of options:
Usage: python -m righttyper [OPTIONS] [SCRIPT] [ARGS]...
RightTyper efficiently generates types for your function arguments and
return values.
Options:
--all-files Process any files encountered, including in
libraries (except for those specified in
--include-files)
--include-files TEXT Include only files matching the given regex
pattern.
--infer-shapes Produce tensor shape annotations (compatible
with jaxtyping).
--srcdir DIRECTORY Use this directory as the base for imports.
--overwrite / --no-overwrite Overwrite files with type information.
[default: no-overwrite]
--output-files / --no-output-files
Output annotated files (possibly
overwriting, if specified). [default: no-
output-files]
--ignore-annotations Ignore existing annotations and overwrite
with type information.
-m, --module Run the script as a module.
--verbose Print diagnostic information.
--generate-stubs Generate stub files (.pyi).
--type-coverage-by-directory DIRECTORY
Report per-directory type annotation
coverage for all Python files in a directory
and its children.
--type-coverage-by-file DIRECTORY
Report per-file type annotation coverage for
all Python files in a directory or its
children.
--type-coverage-summary DIRECTORY
Report uncovered and partially covered files
and functions when performing type
annotation coverage analysis.
--version Show the version and exit.
--target-overhead FLOAT Target overhead, as a percentage (e.g., 5).
--help Show this message and exit.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file righttyper-0.0.9.tar.gz.
File metadata
- Download URL: righttyper-0.0.9.tar.gz
- Upload date:
- Size: 53.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c3fe049df13cf04d0fba507528cca65b41856fe94efbe95ef94ffc37f70a21f
|
|
| MD5 |
ba9db722dfbf5b73d10e1f00c7ec0cce
|
|
| BLAKE2b-256 |
b8223feb1811e3cb9a184279ee3345afaf7887796f70c30cd258a924ff1b7bb7
|
File details
Details for the file righttyper-0.0.9-py3-none-any.whl.
File metadata
- Download URL: righttyper-0.0.9-py3-none-any.whl
- Upload date:
- Size: 44.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e681e683d79d48f9c8994ecac7e80e20bf11bbad59b7c534ad001ee08bbd657
|
|
| MD5 |
25160da0d52474904a54c3b63102c79c
|
|
| BLAKE2b-256 |
9dacb4fcbaa0df5b4eec2ba0a2f1ad96d1b48d419b96e193065ba6b494513530
|