ndarray

Extension of nptyping NDArray for pydantic that allows for JSON-Schema serialization

Note

This module should only have the NDArray class in it, because the type stub ndarray.pyi is only created for NDArray . Otherwise, type checkers will complain about using any helper functions elsewhere - those all belong in numpydantic.schema .

Keeping with nptyping’s style, NDArrayMeta is in this module even if it’s excluded from the type stub.

class NDArrayMeta(name: str, *args: Any, **kwargs: Any)[source]

Hooking into nptyping’s array metaclass to override methods pending completion of the transition away from nptyping

Prevent subclasses, return from internal dict instead

__call__(val: NDArrayType) NDArrayType[source]

Call ndarray as a validator function

__instancecheck__(instance: Any)[source]

Extended type checking that determines whether

  1. the type of the given instance is one of those in

    Interface.input_types()

but also

  1. it satisfies the constraints set on the NDArray annotation

Parameters:

instance (typing.Any) – Thing to check!

Returns:

True if matches constraints, False otherwise.

Return type:

bool

class NDArray(val: NDArrayType)[source]

Constrained array type allowing npytyping syntax for dtype and shape validation and serialization.

This class is not intended to be instantiable, and support for static type checking is limited, it implements the __get_pydantic_core_schema__ method to invoke the relevant interface for validation and serialization.

It is callable, however, which validates and attempts to coerce input to a supported array type. There is no such thing as an “NDArray instance,” but one can think of it as a validating passthrough callable.

References