Skip to main content

tenspec

Declare what an array must be, beside the value it describes, and check it at one boundary.

Tenspec moves the shape and dtype checks from the top of a function into its signature, where a reader and a type checker both see them. It accepts or refuses the array the caller passed. It converts nothing, moves nothing, and copies nothing, unless the declaration names a transform that copies.

This is an early release, at version 0.2.2. The interface can change before version 1.0.

Install

Install it with pip, or with uv. Either one is enough, so run the line for the tool you use:

pip install "tenspec[numpy]"   # or: uv add "tenspec[numpy]"

Name the array library you need as an extra: numpy or torch. import tenspec loads neither one. It needs Python 3.13 or newer.

What it gives you

  • Ordinary types. A declaration is an ordinary annotation carrying your backend's array type, so a type checker reads it and untouched code still runs. The checkers, the settings they need and their measured limits are on Type checkers and Ruff.
  • Pydantic boundaries, natively. TensorContracts relates the fields of your own model inside one model validation.
  • Runtime checks are opt in. An annotation alone performs no runtime work.
  • Transforms are explicit. A value changes only when a declaration names a transform, and the runtime verifies the result kept its class, shape, native dtype and device.

The three entry points

Entry point Use it for
@checked a function or a method, with its arguments and its return
TensorContracts a Pydantic model, whose related fields share one boundary
validate(value, annotation) one value, or one tuple of related values
from typing import Literal as Shape

import numpy as np

from tenspec import checked
from tenspec.numpy import Float


@checked
def weigh_columns(
    values: Float[Shape["rows features"]], weights: Float[Shape["features"]]
) -> Float[Shape["rows features"]]:
    return values * weights


print(weigh_columns(np.ones((3, 4)), np.array([1.0, 2.0, 3.0, 4.0])).shape)

weights must cover as many features as values has columns. A disagreement is a run-time refusal naming the axis, both extents, and the bindings the boundary held.

No type checker compares two shapes. A shape that disagrees is a run-time refusal, never a static error.

Documentation

The documentation site holds the guide, the API reference and two executed tutorials. The repository holds the source.

Attribution

The shape notation follows jaxtyping, which is MIT licensed. jaxtyping is not a dependency of this package.

License

MIT. See LICENSE.

Metadata

Release files for tenspec 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tenspec 0.2.2
File Size Uploaded Provenance
tenspec-0.2.2.tar.gz 59.8 kB With provenance Details

Built distribution (wheel)

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

Total release size: 101.9 kB

Release files / tenspec-0.2.2.tar.gz

Download URL tenspec-0.2.2.tar.gz
Size 59.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2ad7edb4d51ef56c6ed3d015662a4492a0d008d5226ec2f9c02350f2c5f24eeb
BLAKE2b-256 checksum
How to use checksums
dd32eb1f7084dcc64e6c0eb99bed83cb7b35f19610c8a86fa3779e3015c63953
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 Oct 20, 2026.

Transparency log

Release files / tenspec-0.2.2-py3-none-any.whl

Download URL tenspec-0.2.2-py3-none-any.whl
Size 42.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75b4d24d5daad056d5999b9398fbdb8d5a4ec40b8f9ab2c961c5212b036ba7e5
BLAKE2b-256 checksum
How to use checksums
6b8d4b10aebe1031a65cc7e9bc308abb70e5c027d41efc8ca9e6826f106d2796
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 Oct 20, 2026.

Transparency log

Security for tenspec 0.2.2

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tenspec-0.2.2.tar.gz

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 Oct 20, 2026.

Transparency log

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