From 63be085194ddf9d2d8fc32a0ccbe30936c78d870 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Tue, 2 Mar 2021 16:10:01 -0700 Subject: Only allow __bool__, __int__, and __float__ on arrays with shape () --- numpy/_array_api/_array_object.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy/_array_api/_array_object.py') diff --git a/numpy/_array_api/_array_object.py b/numpy/_array_api/_array_object.py index 23f8ab333..32a7bc9a8 100644 --- a/numpy/_array_api/_array_object.py +++ b/numpy/_array_api/_array_object.py @@ -83,6 +83,9 @@ class ndarray: """ Performs the operation __bool__. """ + # Note: This is an error here. + if x._array.shape != (): + raise TypeError("bool is only allowed on arrays with shape ()") res = x._array.__bool__() return res @@ -111,6 +114,9 @@ class ndarray: """ Performs the operation __float__. """ + # Note: This is an error here. + if x._array.shape != (): + raise TypeError("bool is only allowed on arrays with shape ()") res = x._array.__float__() return res @@ -146,6 +152,9 @@ class ndarray: """ Performs the operation __int__. """ + # Note: This is an error here. + if x._array.shape != (): + raise TypeError("bool is only allowed on arrays with shape ()") res = x._array.__int__() return res -- cgit v1.2.1