Skip to main content

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. It is inspired by and produces much the same results as Instagram's monkeytype. At the same time, RightTyper's sampling-based approach ends up being more flexible and up to fifty times faster. 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.

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.

Installation

To install the latest released version, use pip as shown below:

python3 -m pip install righttyper

To install the development (current) version of RightTyper from its repository, use pip as shown below:

python3 -m pip install git+https://github.com/RightTyper/righttyper

Usage

To use RightTyper, simply run your script with righttyper instead of python3:

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 annoations
@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:
?                   +++++

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.

righttyper: high performance

In the below example drawn from the pyperformance benchmark suite, monkeytype runs 40x slower than the original program or when running with righttyper (which runs under 3% slower).

% python3 bm_mdp          
Time elapsed:  6.106977417017333
% righttyper bm_mdp
Time elapsed:  6.299191833997611
% monkeytype run bm_mdp
Time elapsed:  184.57902495900635
# actual time elapsed was 275 seconds, spent post-processing

righttyper: low memory consumption

With monkeytype, this program also consumes 5GB of RAM; the original consumes just 21MB. That's an over 200x increase in memory consumption. monkeytype also leaves behind a 3GB SQLite file.

By contrast, righttyper's memory consumption is just a small increment over the original program: it consumes about 24MB, just 15% more.

NOTE: this is an alpha release and should not be considered production ready.

Requirements

  • Python 3.12 or higher

How it works

Monkeytype is slow for several reasons. First, it uses Python's setprofile functionality to track every single function call and return, gathers types for all arguments and the return value. It checks every element of every argument (an O(N) operation). Finally, it writes these into a SQLite database for later post-processing.

By contrast, RightTyper dynamically applies and removes type checking. It also only randomly samples arguments, letting it track argument types with low overhead. RightTyper always checks the very first invocation and exit of every function. It then performs adaptive sampling: RightTyper tracks its overhead of operation. As long as instrumentation overhead remains below a target (configurable with --target-overhead, by default 5%), it keeps tracking functions. When necessary to reduce overhead, it reduces its rate of function tracking. Whenever average overhead remains below the target, it continues function tracking.

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

righttyper-0.0.7.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

righttyper-0.0.7-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file righttyper-0.0.7.tar.gz.

File metadata

  • Download URL: righttyper-0.0.7.tar.gz
  • Upload date:
  • Size: 36.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for righttyper-0.0.7.tar.gz
Algorithm Hash digest
SHA256 cd94bb0c1437979c19d87a0c05ab5cddee91aba159cdd7e7b30d08a1624a066e
MD5 d0812164774df7e6a57f7b9951b5108d
BLAKE2b-256 abc48f2ad3ef88e06c1e539209a787a4b91e84360fb64e449563f09f0828a137

See more details on using hashes here.

File details

Details for the file righttyper-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: righttyper-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for righttyper-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 aed73e27e9d6ad65797f4746b32c3bbcd847404dae9e8beb1c73dc45caa5c365
MD5 065fb9dfd09fb8ea5c4d42d9d513920b
BLAKE2b-256 8d88f094f074a04f10b909722da0813464d5ce3770f89fc3c5a2d477d896ef70

See more details on using hashes here.

Supported by

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