Check that the `shape` is safe to be used and return it as a tuple.
(shape: Shape | np.integer | int)
| 85 | |
| 86 | |
| 87 | def _normalize_shape(shape: Shape | np.integer | int) -> Shape: |
| 88 | """Check that the `shape` is safe to be used and return it as a tuple.""" |
| 89 | if isinstance(shape, (np.integer, int)): |
| 90 | if shape < 1: |
| 91 | raise ValueError("shape value must be greater than 0: %d" % shape) |
| 92 | shape = (shape,) # N is a shorthand for (N,) |
| 93 | try: |
| 94 | shape = tuple(shape) |
| 95 | except TypeError: |
| 96 | raise TypeError(f"shape must be an integer or sequence: {shape!r}") |
| 97 | |
| 98 | # XXX Get from HDF5 library if possible. |
| 99 | # HDF5 does not support ranks greater than 32 |
| 100 | if len(shape) > 32: |
| 101 | raise ValueError(f"shapes with rank > 32 are not supported: {shape!r}") |
| 102 | |
| 103 | return tuple(SizeType(s) for s in shape) |
| 104 | |
| 105 | |
| 106 | def _normalize_default(value: Any, dtype: DTypeLike) -> np.ndarray: |