Skip to main content

A fast runtime type hint assistant for Python code.

Project description

RightTyper

DownloadsDownloads

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 jaxtyping and beartype or typeguard.

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.

Overhead

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:
==============

barnacle

- def barnacle(x):
+ def barnacle(x: jaxtyping.Float64[np.ndarray, "10 D1"]) -> jaxtyping.Float64[np.ndarray, "D1"]:

fooq

- def fooq(x: int, y) -> bool:
+ def fooq(x: int, y: int) -> 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 [pytest-args...]

Below is the full list of options:

Usage: python -m righttyper [OPTIONS] [SCRIPT] [ARGS]...

Options:
  -m, --module MODULE             Run the given module instead of a script.
  --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
                                  pattern.
  --include-functions TEXT        Only annotate functions matching the given
                                  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.
  --verbose                       Print diagnostic information.
  --generate-stubs                Generate stub files (.pyi).
  --version                       Show the version and exit.
  --target-overhead FLOAT         Target overhead, as a percentage (e.g., 5).
  --sampling / --no-sampling      Whether to sample calls and types or to use
                                  every one seen.  [default: sampling]
  --inline-generics               Declare type variables inline for generics
                                  rather than separately.
  --type-coverage <CHOICE PATH>...
                                  Rather than run a script or module, report a
                                  choice of 'by-directory', 'by-file' or
                                  'summary' type annotation coverage for the
                                  given path.
  --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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

righttyper-0.0.11-cp313-cp313-win_amd64.whl (112.5 kB view details)

Uploaded CPython 3.13Windows x86-64

righttyper-0.0.11-cp313-cp313-manylinux_2_34_x86_64.whl (98.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

righttyper-0.0.11-cp313-cp313-macosx_13_0_universal2.whl (64.1 kB view details)

Uploaded CPython 3.13macOS 13.0+ universal2 (ARM64, x86-64)

righttyper-0.0.11-cp312-cp312-win_amd64.whl (112.5 kB view details)

Uploaded CPython 3.12Windows x86-64

righttyper-0.0.11-cp312-cp312-manylinux_2_34_x86_64.whl (98.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

righttyper-0.0.11-cp312-cp312-macosx_13_0_universal2.whl (64.1 kB view details)

Uploaded CPython 3.12macOS 13.0+ universal2 (ARM64, x86-64)

File details

Details for the file righttyper-0.0.11-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 eddb0508be913b10b5f1a5047023714b8e19bbe9888e0282dd07a0d3896dbf68
MD5 80ec467c8f54f5b6af40594a88985dc6
BLAKE2b-256 eaf9deeb2ce29d9aa34b1d3e265330e502c7f3a206db925ece83fd849d3182ce

See more details on using hashes here.

File details

Details for the file righttyper-0.0.11-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2c02c4a190a0c45df130b64a706beda97593b3f0c736965839c9964576bcf1c8
MD5 2cc94ca7728c0e6bb7bdaf63afe9b4b9
BLAKE2b-256 af405697a8e87fa9ad28e959f1918ee37299e98659a9965ac2d80b240ba576b2

See more details on using hashes here.

File details

Details for the file righttyper-0.0.11-cp313-cp313-macosx_13_0_universal2.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp313-cp313-macosx_13_0_universal2.whl
Algorithm Hash digest
SHA256 3cdcddbd4067fb5b418991afc5f545f72617a2c0a93be8195c755c7e7255a77a
MD5 1d8e08be2dc084edd7bbee9a880573d1
BLAKE2b-256 812c6cc95d5804d429b8f914818816280029cb3c8d6d17d9ace117b3151122af

See more details on using hashes here.

File details

Details for the file righttyper-0.0.11-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f4ff095036bb21d187a279a651d963f96c1d90e04483b5f9f661dc480e0f87fe
MD5 38a683704c3ed5c832b7a76d975a4f4e
BLAKE2b-256 22162b7c3c7e8c94333b6290ba52bb1c483d0e228c09191e67ea736e4b3af48b

See more details on using hashes here.

File details

Details for the file righttyper-0.0.11-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 b293f3fab1cda58866edda7e4420c66d833fb29d5bd3d5c48664b8f4776e53f7
MD5 1bde0856f797120d7e73a1570931c867
BLAKE2b-256 fe8ccce505d8ac96b5fb2dc1868c57cfaaf54652d69cfc5ebfb79b6e11464dd6

See more details on using hashes here.

File details

Details for the file righttyper-0.0.11-cp312-cp312-macosx_13_0_universal2.whl.

File metadata

File hashes

Hashes for righttyper-0.0.11-cp312-cp312-macosx_13_0_universal2.whl
Algorithm Hash digest
SHA256 91b1f8182cf0c6735a3abf7a76255c311d2006a9d4c81b918dee2b645224b664
MD5 d08320bd89ffb16b9f27b8de0982166d
BLAKE2b-256 f32ae16b655a12eca0da867eac9916fb14371d4d99fd74c7bc217996bb48d8b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page