NumPy Typing Compatibility Layer

20251206.2.4 · active · verified Sat Apr 11

numpy-typing-compat provides a static typing compatibility layer for older versions of NumPy. Its current version is `20251206.2.4`, and it sees frequent releases, often coinciding with new NumPy pre-releases or critical bug fixes to ensure type hint accuracy across various NumPy versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `CanArray` and `DTypeLike` for type hinting NumPy arrays. The library primarily provides these compatibility types for static analysis on projects using older NumPy versions, where native type hints might be less mature.

import numpy as np
from numpy_typing_compat import CanArray, DTypeLike

def process_numeric_array(data: CanArray[DTypeLike]) -> CanArray[DTypeLike]:
    """
    Processes a NumPy array, using CanArray for type hinting
    compatibility, especially with older NumPy versions where
    type stubs might be less complete.
    """
    print(f"Input array type: {type(data)}, dtype: {data.dtype}")
    # Perform some array operation
    return data * 2

if __name__ == "__main__":
    # Example with a float array
    my_float_array = np.array([1.0, 2.5, 3.0], dtype=np.float64)
    result_float_array = process_numeric_array(my_float_array)
    print(f"Result float array: {result_float_array}, dtype: {result_float_array.dtype}\n")

    # Example with an integer array
    my_int_array = np.array([10, 20, 30], dtype=np.int32)
    result_int_array = process_numeric_array(my_int_array)
    print(f"Result int array: {result_int_array}, dtype: {result_int_array.dtype}")

view raw JSON →