--- id: torchtyping version: "0.1.5" license: Apache-2.0 license_treatment: permissive maintenance: aging --- # torchtyping — Runtime type annotations for the shape, dtype etc. of PyTorch Tensors. License: permissive · Maintenance: aging · Downloads: 186.2K/mo ## What it is and what it does torchtyping lets you annotate PyTorch tensors with their expected shape, dtype, layout, and dimension names directly in function signatures. Instead of writing comments like `# x has shape (batch, hidden_state)`, you write `x: TensorType["batch", "hidden_state"]`, making tensor contracts explicit and machine-readable. When typeguard is installed and enabled, these annotations are checked at runtime to catch shape mismatches and dtype errors early. The package integrates with typeguard for optional runtime validation and pytest for test-time checking, so you can enforce tensor contracts without runtime overhead in production. It supports flexible shape specifications (exact sizes, named dimensions, variable batch dimensions with `...`), multiple dtype checks, and extensible custom validation rules. However, the author now recommends jaxtyping for new projects, citing better static type checker compatibility. Use it for: - Document tensor shapes in neural network functions to prevent shape mismatch bugs during model development. - Validate batch dimensions and channel sizes at runtime during training to catch data pipeline errors early. - Enforce consistent dimension naming across a codebase to improve code clarity and reduce shape-related debugging. - Test tensor contracts in pytest with automatic typeguard patching to catch shape violations in unit tests. - Combine with typeguard's import hook to automatically check all tensor operations in a module without manual decorators. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Adds type annotations for PyTorch tensor shape, dtype, layout, and dimension names with optional runtime checking via typeguard. Yes, if you are actively developing PyTorch models and want runtime shape validation. The low install friction and permissive license make adoption straightforward. However, be aware that the package is aging and the author recommends jaxtyping for new projects due to better static type checker support. If you need static type checking or are starting a new project, consider jaxtyping instead; if you have an existing codebase using torchtyping or prefer runtime-only validation, it remains functional. ## Install pip install torchtyping uv add torchtyping poetry add torchtyping ## Installing torchtyping Before you install: Low friction installation with only torch and typeguard as runtime dependencies. Maintenance status is aging—last release was in 2024—but the repository remains active and unarchived with 1484 stars. License in practice: Apache-2.0 permissive license allows commercial and private use with minimal restrictions; suitable for most projects. Quickstart: pip install torchtyping typeguard from torch import rand from torchtyping import TensorType, patch_typeguard from typeguard import typechecked patch_typeguard() @typechecked def func(x: TensorType["batch"], y: TensorType["batch"]) -> TensorType["batch"]: return x + y func(rand(3), rand(3)) # works Requires typeguard <3.0.0 if using runtime checking; Python >=3.7 and PyTorch >=1.7.0. Verify before relying: - Whether static type checkers (mypy, pyright) fully recognize TensorType annotations without additional configuration - Current adoption rate and community support level relative to the newer jaxtyping recommendation - Whether the package remains actively maintained beyond the most recent release date ## Package facts - License: Apache-2.0 (permissive) - Python support: supports_current - Install friction: low - Maintenance: aging - Downloads: 186.2K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags pytorch tensor type annotations, shape dtype checking tensors, runtime tensor validation, named tensor type hints, pytorch type safety, tensor dimension checking, pytorch static type checking, pytorch, type-hints, tensor-validation [View on SkillFed](https://skillfed.io/packages/torchtyping) · [View on PyPI](https://pypi.org/project/torchtyping/)