Skip to content

Commit

Permalink
feat: error check on array constructor (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman authored Feb 26, 2024
1 parent ac07d02 commit 9b06bc4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/vector/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ def constructor(
Quantity['length'](Array([1., 4.], dtype=float32), unit='m')
"""
return cls(**{f.name: obj[..., i] for i, f in enumerate(fields(cls))})
_ = eqx.error_if(
obj,
obj.shape[-1] != len(fields(cls)),
f"Cannot construct {cls} from array with shape {obj.shape}.",
)
comps = {f.name: obj[..., i] for i, f in enumerate(fields(cls))}
return cls(**comps)

# ===============================================================
# Array API
Expand Down

0 comments on commit 9b06bc4

Please sign in to comment.