From 554739ee490013cf2757c1ef1420d120d607836b Mon Sep 17 00:00:00 2001 From: Simon Altrogge <8720147+simonaltrogge@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:12:41 +0100 Subject: [PATCH 1/2] TYP: allow `None` in operand sequence of nditer Prevent type-hint errors when using `nditer` in an intended way (see https://numpy.org/doc/stable/reference/arrays.nditer.html#iterator-allocated-output-arrays). Fix #28038 --- numpy/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index a81e060f6233..4972f3bf209c 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -4746,7 +4746,7 @@ class iinfo(Generic[_IntegerT_co]): class nditer: def __new__( cls, - op: ArrayLike | Sequence[ArrayLike], + op: ArrayLike | Sequence[ArrayLike | None], flags: None | Sequence[_NDIterFlagsKind] = ..., op_flags: None | Sequence[Sequence[_NDIterFlagsOp]] = ..., op_dtypes: DTypeLike | Sequence[DTypeLike] = ..., From 723605bcaf472514fbb34947e5b61daa135c0769 Mon Sep 17 00:00:00 2001 From: Simon Altrogge <8720147+simonaltrogge@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:46:40 +0100 Subject: [PATCH 2/2] TST: Add test for allowing `None` in operand sequence passed to `nditer` --- numpy/typing/tests/data/pass/nditer.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 numpy/typing/tests/data/pass/nditer.py diff --git a/numpy/typing/tests/data/pass/nditer.py b/numpy/typing/tests/data/pass/nditer.py new file mode 100644 index 000000000000..25a5b44d7aec --- /dev/null +++ b/numpy/typing/tests/data/pass/nditer.py @@ -0,0 +1,4 @@ +import numpy as np + +arr = np.array([1]) +np.nditer([arr, None])