From 519ce63b8ef9e3af0688936b3b822a8cbcc123f7 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 20 Sep 2022 12:29:38 +0200 Subject: ENH: Allow creating structured void scalars by passing dtype Adds an optional `dtype=` kwarg to `np.void`. If given (and not None), this kwarg effectively turns it into: res = np.array(data, dtype=dtype)[()] Thanks for Marten's review and Bas' help with the typing. Reviewed-by: Marten van Kerkwijk Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com> --- numpy/typing/tests/data/fail/scalars.pyi | 5 +++-- numpy/typing/tests/data/pass/scalars.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/scalars.pyi b/numpy/typing/tests/data/fail/scalars.pyi index 964470538..74a86bbb3 100644 --- a/numpy/typing/tests/data/fail/scalars.pyi +++ b/numpy/typing/tests/data/fail/scalars.pyi @@ -47,7 +47,8 @@ np.uint16(A()) # E: incompatible type np.uint32(A()) # E: incompatible type np.uint64(A()) # E: incompatible type -np.void("test") # E: incompatible type +np.void("test") # E: No overload variant +np.void("test", dtype=None) # E: No overload variant np.generic(1) # E: Cannot instantiate abstract class np.number(1) # E: Cannot instantiate abstract class @@ -62,7 +63,7 @@ np.uint64(value=0) # E: Unexpected keyword argument np.complex128(value=0.0j) # E: Unexpected keyword argument np.str_(value='bob') # E: No overload variant np.bytes_(value=b'test') # E: No overload variant -np.void(value=b'test') # E: Unexpected keyword argument +np.void(value=b'test') # E: No overload variant np.bool_(value=True) # E: Unexpected keyword argument np.datetime64(value="2019") # E: No overload variant np.timedelta64(value=0) # E: Unexpected keyword argument diff --git a/numpy/typing/tests/data/pass/scalars.py b/numpy/typing/tests/data/pass/scalars.py index 684d41fad..aef9c3584 100644 --- a/numpy/typing/tests/data/pass/scalars.py +++ b/numpy/typing/tests/data/pass/scalars.py @@ -113,6 +113,8 @@ np.void(True) np.void(np.bool_(True)) np.void(b"test") np.void(np.bytes_("test")) +np.void(object(), [("a", "O"), ("b", "O")]) +np.void(object(), dtype=[("a", "O"), ("b", "O")]) # Protocols i8 = np.int64() -- cgit v1.2.1