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.0. 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.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 tenspec 0.2.0
File Size Uploaded Provenance
tenspec-0.2.0.tar.gz 57.3 kB With provenance Details

Built distribution (wheel)

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

Total release size: 98.7 kB

Release files / tenspec-0.2.0.tar.gz

Download URL tenspec-0.2.0.tar.gz
Size 57.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9bb5857ab21b1ff0240af4beef3b03cdfdebb21204d8669d63001a46e99a362b
BLAKE2b-256 checksum
How to use checksums
3a85ad3141810668abec7cbb6505b6c0d7abe86634d25ea7df5dce22956930c8
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 9, 2026.

Transparency log

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

Download URL tenspec-0.2.0-py3-none-any.whl
Size 41.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e239836893b7a7bdf2c3218cb8a9ab7cb46465c928564a3434957fc0e9d0d733
BLAKE2b-256 checksum
How to use checksums
a02a09dff9d2495a1b571a1b6fc626e486ce53cc966aaaed1be2184fb8e5fbe7
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 9, 2026.

Transparency log

Security for tenspec 0.2.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.

Full provenance

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

Select from 2 files

Attestation for tenspec-0.2.0.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 9, 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