shape

Declaration and validation functions for array shapes.

Mostly a mildly modified version of nptyping’s npytping.shape_expression.validate_shape() and its internals to allow for extended syntax, including ranges of shapes.

Modifications from nptyping:

  • “…” - In nptyping, '...' means “any number of dimensions with the same shape as the last dimension. ie Shape[2, ...] means “any number of 2-length dimensions. Here '...' always means “any number of any-shape dimensions”

  • Ranges - (inclusive) shape ranges are allowed. eg. to specify an array where the first dimension can be 2, 3, or 4 length:

    Shape[“2-4, …”]

    To specify a range with an unbounded min or max, use wildcards, eg. for an array with the first dimension at least length 2, and the second dimension at most length 5 (both inclusive):

    Shape[“2-*, *-5”]

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

Metaclass that is coupled to nptyping.Shape.

Overridden from nptyping to use local shape validation function

Prevent subclasses, return from internal dict instead

class Shape(*_: Any, **__: Any)[source]

A container for shape expressions that describe the shape of an multi dimensional array.

Simple example:

>>> Shape['2, 2']
Shape['2, 2']

A Shape can be compared to a typing.Literal. You can use Literals in NDArray as well.

>>> from typing import Literal
>>> Shape['2, 2'] == Literal['2, 2']
True
prepared_args = ('*', '...')
validate_shape_expression(shape_expression: str | Any) None[source]

CHANGES FROM NPTYPING: Allow ranges

validate_shape(shape: ~typing.Tuple[int, ...], target: ~numpydantic.validation.shape.Shape[*, ...]) bool[source]

Check whether the given shape corresponds to the given shape_expression. :param shape: the shape in question. :param target: the shape expression to which shape is tested. :return: True if the given shape corresponds to shape_expression.