From f03994d4abdb3b71adbf1debfc8ae57e14f9a6a6 Mon Sep 17 00:00:00 2001 From: jorenham Date: Sun, 22 Dec 2024 18:22:59 +0100 Subject: [PATCH] TYP: fix ``void`` arrays not accepting ``str`` keys in ``__setitem__`` --- numpy/__init__.pyi | 2 ++ numpy/typing/tests/data/pass/ndarray_misc.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index b242ee8c4d2d..02d3d5d1cc7a 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -2129,6 +2129,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DType_co]): value: _ConvertibleToDT64 | _NestedSequence[_ConvertibleToDT64], /, ) -> None: ... + @overload # void + def __setitem__(self: NDArray[void], key: str | list[str], value: object, /) -> None: ... @overload # catch-all def __setitem__(self, key: _ToIndices, value: ArrayLike, /) -> None: ... diff --git a/numpy/typing/tests/data/pass/ndarray_misc.py b/numpy/typing/tests/data/pass/ndarray_misc.py index b9f2a7fe32b6..aa8118c576ef 100644 --- a/numpy/typing/tests/data/pass/ndarray_misc.py +++ b/numpy/typing/tests/data/pass/ndarray_misc.py @@ -175,3 +175,10 @@ class SubClass(npt.NDArray[np.float64]): ... complex(np.array(1.0, dtype=np.float64)) operator.index(np.array(1, dtype=np.int64)) + +# this fails on numpy 2.2.1 +# https://github.com/scipy/scipy/blob/a755ee77ec47a64849abe42c349936475a6c2f24/scipy/io/arff/tests/test_arffread.py#L41-L44 +A_float = np.array([[1, 5], [2, 4], [np.nan, np.nan]]) +A_void: npt.NDArray[np.void] = np.empty(3, [("yop", float), ("yap", float)]) +A_void["yop"] = A_float[:, 0] +A_void["yap"] = A_float[:, 1]