shpe
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.
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/shpe.git
cd shpe
pip install -e .
make vscode-package
This compiles a .vsix file in the shpe-vscode directory, which you can install as an extension in VS Code.
Terminal
Installation
Prerequisites: Python 3.12+
To install shpe directly:
pip install git+https://github.com/jiaquan-cheng/shpe.git
Usage
shpe path/to/your/file_or_directory
You can use the --show-shapes flag to display the inferred shapes of all expressions in the code:
shpe 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 shpe's capabilities.
Limitations
We prioritize soundness over completeness, so shpe might miss errors. When shpe 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,
shpeidentifies functions by suffix, so it may not trigger an error in cases like (var.expand_dimswithoutnp.prefix).
If you noticed any bugs or have any feature requests, please report them on GitHub Issues.
Development
Prerequisites: Python 3.12+, uv
To get started locally:
git clone https://github.com/jiaquan-cheng/shpe.git
cd shpe
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.
Release files for shpe 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| shpe-0.2.0.tar.gz | 202.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| shpe-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 225.9 kB
Release files / shpe-0.2.0.tar.gz
| Download URL | shpe-0.2.0.tar.gz |
|---|---|
| Size | 202.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ee827026fd2e759e5a7ecaa9fe875c944501884fc8897a6035b337482c1c6fc6
|
|
BLAKE2b-256 checksum How to use checksums |
16b22f6ed15ed51ae89e287a46cd962cd98039a5c9b75592fa23804a39cbad5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / shpe-0.2.0-py3-none-any.whl
| Download URL | shpe-0.2.0-py3-none-any.whl |
|---|---|
| Size | 23.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cbecc5687624376dd3a44a49611ab2c55a7ed0212fcc0b82b228c70308fc664a
|
|
BLAKE2b-256 checksum How to use checksums |
68fccec2286617c58ae5debc5da1552cc007591b9b48cd4da6ad1e4345f1cfee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|