Skip to content

Commit 5ce1939

Browse files
Jammy2211claude
authored andcommitted
fix(__array__): coerce to numpy + accept copy kwarg (numpy 2.x strict mode)
NumPy >=1.26 raises "object __array__ method not producing an array" when __array__ returns a non-np.ndarray. Our impl returned self._array directly, but _array can be a jax.Array on the JAX path (e.g. set by `_array = jnp.where(...)` in __setitem__). Coerce to numpy via np.asarray to satisfy the protocol contract. Also added the `copy` kwarg introduced in numpy 2.0; without it, future numpy may pass copy positionally and break the call. Surfaced by python_matrix.yml smoke runs on autogalaxy_workspace, which hit `np.array(vis.slim)` in autoarray/plot/utils.py:plot_visibilities_1d on every Python version. Issue #74 in PyAutoBuild Category A. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 25a988d commit 5ce1939

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

autoarray/abstract_ndarray.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,20 @@ def __repr__(self):
418418
self.__class__.__name__,
419419
)
420420

421-
def __array__(self, dtype=None):
422-
if dtype:
423-
return self._array.astype(dtype)
424-
return self._array
421+
def __array__(self, dtype=None, copy=None):
422+
# numpy >=1.26 requires __array__ to return an actual np.ndarray
423+
# ("object __array__ method not producing an array" otherwise),
424+
# and numpy 2.0 added the `copy` kwarg. self._array can be a
425+
# jax.Array on the JAX path (set in `_array = jnp.where(...)` etc.)
426+
# — coerce to numpy so the protocol contract holds.
427+
arr = self._array
428+
if not isinstance(arr, np.ndarray):
429+
arr = np.asarray(arr)
430+
if dtype is not None:
431+
arr = arr.astype(dtype)
432+
if copy:
433+
arr = arr.copy()
434+
return arr
425435

426436
def __len__(self):
427437
return len(self._array)

0 commit comments

Comments
 (0)