Skip to main content

MyPy Type Checking for NumPy/Jax/PyTorch Einsum Operations

mypy_einsum is a Mypy plugin for type checking np.einsum, jax.numpy.einsum, and torch.einsum operations.

The Einstein summation convention can be used to compute many multi-dimensional, linear algebraic array operations. einsum provides a succinct way of representing these.

However, since einsum equations are passed as a string, it is very easy to overlook typos or other bugs as linters are unable to help. mypy_einsum is a Mypy plugin that that is able to statically verify the correctness of einsum equations with needing to execute the code.

Installation

mypy_einsum can be installed with pip:

pip install mypy-einsum

Setup

To enable the plugin, add it to you projects Mypy configuration file. Usually mypy.ini:

[mypy]
plugins = mypy_einsum

or pyproject.toml:

[tool.mypy]
plugins = ["mypy_einsum"]

Example

Can you spot the 🐛 without running the code?

import numpy as np

a = np.arange(9).reshape(3, 3)

np.einsum("ik,kj->ij", a)

mypy_einsum will catch it for you:

❯ mypy example.py --pretty
example.py:5: error: Number of einsum subscripts must be equal to the
number of operands.  [einsum]
    np.einsum("ik,kj->ij", a)
              ^~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

After fixing it mypy will succeed 🎉:

np.einsum("ik,kj->ij", a, a)
❯ mypy example.py
Success: no issues found in 1 source file

Supported Operations

Reporting Issues and Contributing

mypy_einsum aims to never raise warnings for valid einsum operations. If you encounter a warning that you believe is incorrect, or think mypy_einsum is not reporting an error please let us know. Contributions are very welcome!

Metadata

Release files for mypy-einsum 1.0.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 mypy-einsum 1.0.0
File Size Uploaded
mypy-einsum-1.0.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mypy-einsum 1.0.0
File Interpreter ABI Platform
mypy_einsum-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / mypy-einsum-1.0.0.tar.gz

Download URL mypy-einsum-1.0.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e79b0a2b1d77300bd1ea99a977ca2b51a35d20845efac165349f0f0e5156a7bb
BLAKE2b-256 checksum
How to use checksums
04a54c32eeaaf6199778f6018d1bf6814e4fbbfc3c57f2301b00cc10b2c3eb90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / mypy_einsum-1.0.0-py3-none-any.whl

Download URL mypy_einsum-1.0.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
210651116788ee55bcc1b7ae441d4caa2ece52c52d8152fd192529b99b44523b
BLAKE2b-256 checksum
How to use checksums
886026e1a24d12f57be8ddef68b383a927a445b92cfe7692bf158eea0250326f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

1.0.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