shpe
A lightweight static analyzer for tracking and validating NumPy tensor shapes without running the code. It is designed to both be a CLI tool that can be used in CI/CD pipelines and a VS Code extension for real-time shape inference and error messages during development.
Important Note for VS Code Extension: This extension requires the core Python CLI tool to function. Please make sure you run
pip install shpein your environment!
VS Code Extension Preview
CLI Usage
Alternatively, you can use the CLI tool to check for shape errors in your code.
shpe examples/intro.py
examples/intro.py:15: [Elementwise] cannot combine a (3, 2) and b (2, 2) with element-wise operator.
examples/intro.py:17: [MatMul] cannot multiply d (3, 2)and a (3, 2): inner dimensions must match (2 != 3).
examples/intro.py:18: [Annotation] f annotated as (3, 2), but expression has the shape (2, 3).
Found 3 error(s) across 1 file(s).
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
Installation
Prerequisites: Python 3.12+
-
For VS Code extension, search for
shpein the VS Code marketplace and install it. -
For both CLI and VS Code extension, install
shpe:
pip install shpe
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.1
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.1.tar.gz | 203.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| shpe-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 227.5 kB
Release files / shpe-0.2.1.tar.gz
| Download URL | shpe-0.2.1.tar.gz |
|---|---|
| Size | 203.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2f39702c39706395828d7604750469cf53cd08bc59eacca026389bcc7b188deb
|
|
BLAKE2b-256 checksum How to use checksums |
3e53e1d4aa9120c50143950e9fec36d000d10db60a43e0c2a3336241bc0eb3e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / shpe-0.2.1-py3-none-any.whl
| Download URL | shpe-0.2.1-py3-none-any.whl |
|---|---|
| Size | 23.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c92254f245f2a6f9931c74735a7b764e6a8501b72fba3d3715fbb8913a9833df
|
|
BLAKE2b-256 checksum How to use checksums |
66108a40dc2ce42bf28e15051d35e4f99f57d70262f25bd1b1ae52373f97a9f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency log