Interfaces¶
Base Interface metaclass
- class Interface(shape: Tuple[int, ...] | Any, dtype: str | type | Any | generic)[source]¶
Abstract parent class for interfaces to different array formats
- validate(array: Any) T[source]¶
Validate input, returning final array type
Calls the methods, in order:
array =
before_validation()(array)- dtype =
get_dtype()(array) - get the dtype from the array, override if eg. the dtype is not contained in
array.dtype
- dtype =
- valid =
validate_dtype()(dtype) - check that the dtype matches the one in the NDArray specification. Override if special validation logic is needed for a given format
- valid =
raise_for_dtype()(valid, dtype) - after checking dtype validity,raise an exception if it was invalid. Override to implement custom exceptions or error conditions, or make validation errors conditional.
- array =
after_validate_dtype()(array) - hook for additional validation or array modification mid-validation
- array =
- shape =
get_shape()(array) - get the shape from the array, override if eg. the shape is not contained in
array.shape
- shape =
- valid =
validate_shape()(shape) - check that the shape matches the one in the NDArray specification. Override if special validation logic is needed.
- valid =
raise_for_shape()(valid, shape) - after checking shape validity,raise an exception if it was invalid. You know the deal bc it’s the same as raise for dtype.
after_validation()- hook after validation for modifying the arraythat is set as the model field value
Follow the method signatures and return types to override.
Implementing an interface subclass largely consists of overriding these methods as needed.
- Raises:
If validation fails, rather than eg. returning False, exceptions will –
be raised (to halt the rest of the pydantic validation process). –
When using interfaces outside of pydantic, you must catch both –
.DtypeError –
:raises of
InterfaceError):
- before_validation(array: Any) NDArrayType[source]¶
Optional step pre-validation that coerces the input into a type that can be validated for shape and dtype
Default method is a no-op
- get_dtype(array: NDArrayType) str | type | Any | generic[source]¶
Get the dtype from the input array
- validate_dtype(dtype: str | type | Any | generic) bool[source]¶
Validate the dtype of the given array, returning
Trueif valid,Falseif not.
- raise_for_dtype(valid: bool, dtype: str | type | Any | generic) None[source]¶
After validating, raise an exception if invalid :raises ~numpydantic.exceptions.DtypeError:
- after_validate_dtype(array: NDArrayType) NDArrayType[source]¶
Hook to modify array after validating dtype. Default is a no-op.
- get_shape(array: NDArrayType) Tuple[int, ...][source]¶
Get the shape from the array as a tuple of integers
- validate_shape(shape: Tuple[int, ...]) bool[source]¶
Validate the shape of the given array against the shape specifier, returning
Trueif valid,Falseif not.
- raise_for_shape(valid: bool, shape: Tuple[int, ...]) None[source]¶
Raise a ShapeError if the shape is invalid.
- Raises:
ShapeError –
- after_validation(array: NDArrayType) T[source]¶
Optional step post-validation that coerces the intermediate array type into the return type
Default method is a no-op
- abstract classmethod check(array: Any) bool[source]¶
Method to check whether a given input applies to this interface
- abstract classmethod enabled() bool[source]¶
Check whether this array interface can be used (eg. its dependent packages are installed, etc.)
- classmethod to_json(array: Type[T], info: SerializationInfo | None = None) list | dict[source]¶
Convert an array of
return_typeto a JSON-compatible format using base python types
- classmethod interfaces(with_disabled: bool = False, sort: bool = True) Tuple[Type[Interface], ...][source]¶
Enabled interface subclasses
- classmethod return_types() Tuple[NDArrayType, ...][source]¶
Return types for all enabled interfaces
- classmethod match(array: Any, fast: bool = False) Type[Interface][source]¶
Find the interface that should be used for this array based on its input type
First runs the
checkmethod for all interfaces returned byInterface.interfaces()except forNumpyInterface, and if no match is found then try the numpy interface. This is becauseNumpyInterface.check()can be expensive, as we could potentially try to- Parameters:
fast (bool) – if
False, check all interfaces and raise exceptions for having multiple matching interfaces (default). IfTrue, check each interface (as ordered by itspriority, decreasing), and return on the first match.