Skip to main content

Elegant runtime shape and dtype checking for NumPy, JAX, PyTorch, and CuPy arrays — powered by beartype.

Project description

Bearshape

Python 3.10-3.14 Coverage 91% Docs

Runtime shape and dtype checking for NumPy, JAX, PyTorch, CuPy, and tree-structured containers, powered by beartype.

from beartype import beartype
from bearshape import N, C
from bearshape.numpy import F32


@beartype
def normalize(x: F32[N, C]) -> F32[N, C]:
  return x / x.sum(axis=1, keepdims=True)

Bearshape turns annotations such as F32[N, C], F32Like[~B, C], F32[Value("size")], and Tree[F32[N], T] into runtime-validated contracts. Named dimensions are shared within a function call, so mismatched shapes fail at the boundary instead of later in array code.

Install

pip install bearshape

Bearshape keeps the root import lightweight. Install the array backend packages you use explicitly:

pip install bearshape numpy
pip install bearshape numpy torch
pip install bearshape numpy jax
pip install bearshape numpy cupy
pip install bearshape numpy optree

What It Checks

  • strict array type, dtype, and shape contracts
  • backend-aware Like[...] conversion checks
  • scalar-like values and constrained runtime Value(...) dimensions
  • tree leaf and structure annotations through JAX or OpTree
  • annotation syntax exercised by pyright, mypy, and ty fixtures

Public Surface

Import dimensions, Value, Scalar, DtypeSpec, check, and check_context from bearshape.

Import backend aliases from backend modules:

from bearshape.numpy import F32, F32Like
from bearshape.jax import Tree
from bearshape.torch import I64

The root package does not import NumPy or any backend. Backend modules require their own runtime dependencies.

Development

uv sync
uv run prek run -a
uv run pytest -n auto tests/
uv run pytest -n auto tests/test_typecheck.py
uv run pyright src tests/typing
uv run mypy src tests/typing
uv run ty check src tests/typing

CuPy runtime tests require a CUDA-capable environment and are deferred on CPU-only machines.

See the documentation site for the full user guide and API reference.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bearshape-0.0.1.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bearshape-0.0.1-py3-none-any.whl (48.4 kB view details)

Uploaded Python 3

File details

Details for the file bearshape-0.0.1.tar.gz.

File metadata

  • Download URL: bearshape-0.0.1.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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}

File hashes

Hashes for bearshape-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f9ead28b049001e1134c0d9b79aa50579c8430b253651ce0691d446efb3e9055
MD5 4548d15d7176c190f34a3ae9e9701575
BLAKE2b-256 69f4128557b40f1c77fc45f1ddfe13a0c479be0a6b2b93e2206f08beaa2d84c7

See more details on using hashes here.

File details

Details for the file bearshape-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: bearshape-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 48.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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}

File hashes

Hashes for bearshape-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3424e6a1b0ab1ed15ad2f6638852128328cd88b4aaa27aac649f0a114b815597
MD5 50888ebacfae85cd63260c39b39c3b79
BLAKE2b-256 ac7894c79d3b5e1713f8291704bf011abff89ac39d483a0e0aa23d68227caee8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page