Array Types¶
Array aliases such as F32[N, C] or DT64[...] are the core "strict array"
side of bearshape. They enforce both:
- the concrete backend array class
- the allowed dtype family
- the shape specification in the subscript
At runtime these aliases become bearshape runtime hint classes rather than
Annotated[...] wrappers. Standard @beartype checks them through
__instancecheck__(), and bearshape supplies readable failure text through
__instancecheck_str__().
Built-in backends¶
Concrete dtypes:
Bool:boolI8,I16,I32,I64:int8–int64U8,U16,U32,U64:uint8–uint64F16,F32,F64,F128:float16–float128/longdoubleC64,C128,C256:complex64–complex256/clongdouble
Additional dtypes:
V:voidStr:stringBytes:bytesObj:objectDT64:datetime64TD64:timedelta64
Category dtypes:
Int: any signed integer dtypeUInt: any unsigned integer dtypeInteger: any integer dtypeFloat: any floating dtypeReal: any integer or floating dtypeComplex: any complex dtypeInexact: any float or complex dtypeNum: any numeric dtypeShaped: any dtype at runtime, with shape-only checking
Notes:
DT64andTD64accept unit-qualified NumPy dtypes such asdatetime64[ns],datetime64[D],timedelta64[ms], andtimedelta64[s].Shapedchecks shape only at runtime. Its static alias is necessarily an approximation, not a true "any dtype ndarray" model.
bearshape.jax uses jax.Array as the base array class.
It exports:
- most NumPy numeric aliases
BF16for JAX bfloat16 arrays- JAX
Likealiases TreeandStructure
It does not expose NumPy-only aliases such as F128, C256, V, Str,
Bytes, Obj, DT64, or TD64.
bearshape.torch uses torch.Tensor as the base array class.
It exports:
- most NumPy numeric aliases
BF16- Torch
Likealiases - NumPy-defined
ScalarLikere-exports
It does not expose NumPy-only aliases such as F128, C256, V, Str,
Bytes, Obj, DT64, or TD64.
Structured dtypes¶
Use Structured() for NumPy structured (record) dtypes:
import numpy as np
from beartype import beartype
from bearshape import N
from bearshape.numpy import Structured
Point = Structured([("x", np.float32), ("y", np.float32)])
@beartype
def process_points(pts: Point[N]) -> Point[N]:
return pts
pts = np.zeros(10, dtype=[("x", np.float32), ("y", np.float32)])
process_points(pts) # OK
wrong = np.zeros(10, dtype=[("a", np.float32), ("b", np.float32)])
process_points(wrong) # Raises
Endianness variants¶
For multi-byte dtypes, create endianness-constrained types programmatically
using make_array_type with endianness DtypeSpec constants:
import numpy as np
from beartype import beartype
from bearshape import N, C, make_array_type
from bearshape._dtypes import FLOAT32_LE
F32LE = make_array_type(np.ndarray, FLOAT32_LE)
@beartype
def process_le(x: F32LE[N, C]) -> F32LE[N, C]:
return x
process_le(np.ones((4, 3), dtype="<f4")) # OK
process_le(np.ones((4, 3), dtype=">f4")) # Raises
Available suffixes: _LE (little-endian), _BE (big-endian), _N (native).
Custom array types¶
Use make_array_type and make_array_like_type to create types for custom
array classes or dtype combinations:
import numpy as np
from bearshape import DtypeSpec, make_array_like_type, make_array_type
MIXED = DtypeSpec("MixedPrecision", frozenset({"float16", "float32"}))
MixedArray = make_array_type(np.ndarray, MIXED)
MixedLike = make_array_like_type(MIXED, name="MixedLike")
MixedExact = make_array_like_type(MIXED, casting="no", name="MixedExact")
make_array_type is the strict array factory. make_array_like_type is the
broader input-contract factory and is covered in more detail on
Like Types.
DtypeSpec¶
DtypeSpec is the root abstraction behind these aliases:
allowedis the set of canonical dtype namesbyteordercan restrict acceptance to"little","big", or"native"DtypeSpec.structured(...)matches one exact NumPy structured dtype layout
That makes it the right tool when the built-in alias set is close but not quite what you need.
When to use array types vs Like types¶
- Use array types such as
F32[N, C]when the function truly requires a real backend array object. - Use
Liketypes such asF32Like[N, C]when callers may pass scalars, lists, tuples, or convertible foreign arrays that you immediately normalize yourself.