Numpy

Interface to numpy arrays

class NumpyJsonDict(*, type: Literal['numpy'], dtype: str, value: list, shape: tuple[int, ...] | None = None)[source]

JSON-able roundtrip representation of numpy array

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

to_array_input() ndarray[source]

Construct a numpy array

model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class NumpyTyping[source]

Static-typing companion for NumpyInterface.

constructors: ClassVar[tuple[ConstructorSpec, ...]] = (ConstructorSpec(fullname='numpy.ones', shape_arg=0, dtype_arg='dtype', mode='function'), ConstructorSpec(fullname='numpy.zeros', shape_arg=0, dtype_arg='dtype', mode='function'), ConstructorSpec(fullname='numpy.empty', shape_arg=0, dtype_arg='dtype', mode='function'), ConstructorSpec(fullname='numpy.full', shape_arg=0, dtype_arg='dtype', mode='function'), ConstructorSpec(fullname='numpy._core.multiarray._ConstructorEmpty.__call__', shape_arg=0, dtype_arg='dtype', mode='method'))

Constructor calls whose return type the mypy plugin should refine.

classmethod emit_imports() list[str][source]

Just importing numpy over here!

classmethod emit_constructor_source(shape: tuple[int, ...], dtype: str) str | None[source]

Constructor using numpy.zeros()

class NumpyInterface(shape: tuple[int, ...] | Any = typing.Any, dtype: str | type | Any | generic = typing.Any)[source]

Numpy ndarray s!

return_type

alias of ndarray

json_model

alias of NumpyJsonDict

priority = -999

The numpy interface is usually the interface of last resort. We want to use any more specific interface that we might have, because the numpy interface checks for anything that could be coerced to a numpy array (see NumpyInterface.check() )

typing

alias of NumpyTyping

classmethod check(array: Any) bool[source]

Check that this is in fact a numpy ndarray or something that can be coerced to one

before_validation(array: Any) ndarray[source]

Coerce to an ndarray. We have already checked if coercion is possible in check()

classmethod enabled() bool[source]

Check that numpy is present in the environment

classmethod to_json(array: ndarray, info: SerializationInfo = None) list | JsonDict[source]

Convert an array of return_type to a JSON-compatible format using base python types