Skip to content

AshrithSagar/typingkit

Repository files navigation

typingkit

Ruff

Python strong typing suite, along with Typed NumPy: Static shape typing and runtime shape validation.

Warning

Experimental & WIP. See USAGE.md for more details.

Installation

Install uv (optional, recommended)

Install uv, if not already. Check here for installation instructions.

It is recommended to use uv, as it will automatically install the dependencies in a virtual environment. If you don't want to use uv, skip to the next step.

TL;DR: Just run

curl -LsSf https://astral.sh/uv/install.sh | sh
Install the package

The dependencies are listed in the pyproject.toml file. At present, the only required dependency is numpy.

Install the package from the PyPI release:

# Using uv
uv add typingkit

# Or with pip
pip3 install typingkit

To install from the latest commit:

uv add git+https://github.com/AshrithSagar/typingkit.git@main

Usage

from typing import Literal, TypeAlias, TypeVar

import numpy as np
from typingkit.numpy import TypedNDArray as NDArray

# Shape variables are just regular TypeVars
N = TypeVar("N", bound=int, default=int)
M = TypeVar("M", bound=int, default=int)

# Create aliases such as these, or use TypedNDArray directly
Vector: TypeAlias = NDArray[tuple[N], np.dtype[Any]]
Matrix: TypeAlias = NDArray[tuple[M, N], np.dtype[Any]]

v1 = Vector([1, 2, 3])  # Passes
v2 = Vector([4, 5, 6, 7])  # Also passes

v3 = NDArray[tuple[int]]([[8, 9]])
# Fails, since expected 1D array but passed in a 2D array

# Literal types also work
v4 = Vector[Literal[3]]([1, 2, 3])  # Passes
v5 = Vector[Literal[3]]([4, 5, 6, 7])  # Fails

v6 = Vector[Literal[3, 4]]([4, 5, 6, 7])  # Passes
# Allowed shapes are either (3,) or (4,)

See USAGE.md for more details.

License

This project falls under the MIT License.

About

Python strong typing suite, along with Typed NumPy: Static shape typing and runtime shape validation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages