Skip to main content

shpy

CI

A lightweight static analyzer for tracking and validating NumPy tensor shapes without running the code.

VS Code Extension

Preview

The VS Code extension provides real-time shape inference and error messages.

Shpy VS Code Extension Demo

Installation

It is not yet published on VS Code Marketplace. To install the extension, clone the repository, install the package and build the VS Code extension:

git clone https://github.com/jiaquan-cheng/shpy.git
cd shpy
pip install -e .
make vscode-package

This compiles a .vsix file in the shpy-vscode directory, which you can install as an extension in VS Code.

Terminal

Installation

Prerequisites: Python 3.13+

To install shpy directly:

pip install git+https://github.com/jiaquan-cheng/shpy.git

Usage

shpy path/to/your/file_or_directory

You can use the --show-shapes flag to display the inferred shapes of all expressions in the code:

shpy path/to/your/file_or_directory --show-shapes

Features

  • Infers shapes from NumPy array (np.array([1, 2, 3])) and NumPy functions (np.zeros((3, 2)), a.T).
  • Validates shape annotations for NumPy arrays (c: Annotated[np.ndarray, (3, 2)]).
  • Validates NumPy operations for shape compatibility (a + b, a @ b).
  • Infers shapes for simple functions calls and function bodies (c = custom_func(a, b)).
  • Tracks scalar variables used in shape definitions (np.zeros((dim, 2))).

Checkout examples/demo.py for a more comprehensive demonstration of shpy's capabilities.

Limitations

We prioritize soundness over completeness, so shpy might miss errors. When shpy is uncertain it, does not infer the shape.

False positive:

  • We do not track inplace function modification like b = a.resize((3, 2)), so it might infer the wrong shape.

False negatives:

  • Only supports a subset of NumPy arrays and functions.
  • No control flow support (if, for, while), variables touched are not inferred.
  • No support for recursive functions.
  • To keep development simple,shpy identifies functions by suffix, so it may not trigger an error in cases like (var.expand_dims without np. prefix).

If you noticed any bugs or have any feature requests, please report them on GitHub Issues.

Development

Prerequisites: Python 3.13+, uv

To get started locally:

git clone https://github.com/jiaquan-cheng/shpy.git
cd shpy
make setup

We would recommend to using the VS Code extension or the --show-shapes flag to check the inferred shapes of your code while developing.

  • make : Runs the test suite and quality checks.
  • make lint : Runs Ruff and Mypy for code quality and type safety.
  • make format : Auto-format code.
  • make test : Runs Pytest.
  • make clean : Cleans up the project by removing build artifacts and caches.

Release files for shpz 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shpz 0.2.0
File Size Uploaded
shpz-0.2.0.tar.gz 201.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shpz 0.2.0
File Interpreter ABI Platform
shpz-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 224.6 kB

Release files / shpz-0.2.0.tar.gz

Download URL shpz-0.2.0.tar.gz
Size 201.0 kB
Tags Source
SHA-256 checksum
How to use checksums
087d92bcd9230fa4ff63318c9251f52270c3654debcab567097e1e22a0c11bc0
BLAKE2b-256 checksum
How to use checksums
398f7e3c89730a49e3f99023881bec658336dd6bcba18449e2fb3922e5bbccb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / shpz-0.2.0-py3-none-any.whl

Download URL shpz-0.2.0-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74331fb4fac7558917a62b828c965c6c3a84df5116a19979c2a9554b8023a490
BLAKE2b-256 checksum
How to use checksums
356af0218b7f755376831f3bd592b7def3a88ba8ee182c98c3fcbce7a13c4a0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page