Skip to main content

Runtime Shape Validation for Size-Annotated Python Code

The sizecheck package provides a decorator that automatically adds runtime shape checking to Python functions based on size-annotated variable names using AST transformation.

For previous versions of the documentation, substitute latest in the documentation URL above for the desired version.

Overview

When writing PyTorch or NumPy code, it's common to use naming conventions that indicate tensor shapes, as in this Medium post. For example, if a tensor weights has shape N × K, you might name the variable weights_NK. This library automatically validates that tensors match their annotated shapes at runtime by analyzing and modifying your function's Abstract Syntax Tree (AST).

Key Features

  • AST-based transformation: Automatically injects shape checks into function arguments and variable assignments
  • Intuitive naming convention: Use underscores to indicate tensor shapes
  • Framework agnostic: Works with PyTorch, NumPy, Jax, and any other libraries that use .shape to indicate tensor shapes.
  • Comprehensive checking: Validates both function parameters and intermediate assignments
  • Clear error messages: Provides detailed information when shapes don't match

Quick Start

import torch
from sizecheck import sizecheck

@sizecheck
def matrix_multiply(a_NK, b_KM):
    """Matrix multiplication with automatic shape checking."""
    result_NM = torch.matmul(a_NK, b_KM)
    return result_NM

# This works fine
a_NK = torch.randn(3, 4)  # N=3, K=4
b_KM = torch.randn(4, 5)  # K=4, M=5
result = matrix_multiply(a_NK, b_KM)  # Shape: (3, 5)

# This raises an AssertionError
a_NK = torch.randn(3, 4)
b_KM = torch.randn(5, 6)  # Wrong! K dimensions don't match
result = matrix_multiply(a_NK, b_KM)  # AssertionError!

Shape Annotation Format

Each character in the dimensions suffix represents one dimension:

  • tensor_NK: 2D tensor with dimensions N × K
  • data_BCHW: 4D tensor with dimensions B × C × H × W

Dimension annotations can contain:

  • Variable dimensions (uppercase letters): N, K, M - stored in variables of the same name
  • Constant dimensions (single digits): 3, 4, 2 - checked for exact size

What Gets Checked

The decorator automatically adds shape validation for:

  • Function arguments with underscores in their names
  • Variable assignments to names containing underscores

Dimension Scope

The dimensions are scoped to the function they are defined in. For example, if you define a function foo with a parameter x_NK, the dimension N is only valid within the scope of foo. If you define another function bar with a parameter y_NL, this dimension N can differ from the one in foo, but it is only valid within the scope of bar.

Julia version

If you're looking for the Julia version of this library, check out SizeCheck.jl.

Metadata

Release files for sizecheck 0.3.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 sizecheck 0.3.0
File Size Uploaded Provenance
sizecheck-0.3.0.tar.gz 6.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 13.5 kB

Release files / sizecheck-0.3.0.tar.gz

Download URL sizecheck-0.3.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9a01082795062e6600a15f595b72d8332ca1bf14c0213d314f04b0b0e223b09d
BLAKE2b-256 checksum
How to use checksums
99f10c48516d2952a61c2dbc4b5059b5407ce0c916b66fb6a0a4f11887b27754
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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 / sizecheck-0.3.0-py3-none-any.whl

Download URL sizecheck-0.3.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29b728abd5e0dcfe3d2ea4fa4a9d828955b60c05be94506e32db736201f11bb0
BLAKE2b-256 checksum
How to use checksums
bf83f86ae8d0485cf5e2cbe6082bb0bfeae16ba1fcd0326f0dec5aa551b66ca9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}

Security for sizecheck 0.3.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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