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.10-cp313-cp313-win_amd64.whl (112.8 kB view details)

Uploaded CPython 3.13Windows x86-64

righttyper-0.0.10-cp313-cp313-manylinux_2_34_x86_64.whl (98.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

righttyper-0.0.10-cp313-cp313-macosx_13_0_universal2.whl (64.4 kB view details)

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

righttyper-0.0.10-cp312-cp312-win_amd64.whl (112.8 kB view details)

Uploaded CPython 3.12Windows x86-64

righttyper-0.0.10-cp312-cp312-manylinux_2_34_x86_64.whl (98.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

righttyper-0.0.10-cp312-cp312-macosx_13_0_universal2.whl (64.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d6e472321bbc12a0b8220d4515484c7d5aaf015d46aa974c3c5a9f0fb2bbb0d5
MD5 eadda53028b0af2d257c27d23bf4822b
BLAKE2b-256 b954a76e15d192351d248bf62c5b19e6cd09ef421629958e7aabdc84de6feeeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 12f6df6551bdeed3fe5e2d81084cc3745c44f3741962eca7f8277d7f382df56e
MD5 ea73826bf6bbc3599dc7bb8310513665
BLAKE2b-256 8a3c789ded5d930a301003a943c656374ef705199c28b49b72b181c4b2f6f262

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp313-cp313-macosx_13_0_universal2.whl
Algorithm Hash digest
SHA256 b36cbd24192eedc179765df938d384e06c64c5799e76ad6da8bc01091af8388d
MD5 0d34d73144cb3caaef4461ca1190efe9
BLAKE2b-256 e88692261719d7eab4752fbc0cd52fb4bcbf20a61f91971afbea160c38281af0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8ea0235631e5d89406ddfbf0ad3e1787f954221dabbfb100ab5a6f8e6ab1df2b
MD5 9fd9aa7cdfa90da0276db4fa21314069
BLAKE2b-256 9064d507f27f524199b373f5a778153a90acb0f3e7dd89ff850d3651ce0338bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 05228777e63c877f3aaf3b1a477e22290942904ff201f4867662432f88512a0e
MD5 66220e2df006ba509fd5a44d8531f213
BLAKE2b-256 c95cfab5bacc1e141e4839d5c6b792a6f826f94fd73801fb643296812e871893

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for righttyper-0.0.10-cp312-cp312-macosx_13_0_universal2.whl
Algorithm Hash digest
SHA256 e65b1c0f98fa0ec6a265b8fa26f66f51481d7cb7e3ce7ea40b14796bbcff59fc
MD5 eac3735c362b52c1522ae9853b21e3e7
BLAKE2b-256 05ea57a8b614d7b520dbfc40eaf7ac12ed284af1b178e200e45fa413d019b7d9

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