nptyping

2.5.0 · active · verified Sun Apr 12

nptyping provides type hints for NumPy arrays and structured arrays, enabling static type checking for data science code that uses NumPy. It also includes experimental support for Pandas DataFrames. As of version 2.5.0, it's actively maintained with a moderate release cadence, focusing on compatibility, new features like structure expressions, and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `nptyping.NDArray` with `Shape` and a specific integer type (`Int[64]`) to provide type hints for a NumPy array in a function signature. It also shows a basic runtime `isinstance` check using `nptyping` types.

import numpy as np
from nptyping import NDArray, Shape, Int

def process_integer_array(arr: NDArray[Shape["*, *"], Int[64]]) -> NDArray[Shape["*, *"], Int[64]]:
    """Type-hinted function for processing a 2D integer array."""
    print(f"Processing array with shape {arr.shape} and dtype {arr.dtype}")
    return arr * 2

# Create a NumPy array
my_array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)

# Call the type-hinted function
result_array = process_integer_array(my_array)
print(f"Resulting array:\n{result_array}")

# Example of runtime type checking (works with numpy arrays)
if isinstance(my_array, NDArray[Shape["*, *"], Int]):
    print("my_array is a 2D integer NDArray.")

view raw JSON →