You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: np.<ufunc>(np.ndarray) should raise TypeError #8877
In [3]: np.add(int, int)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-979cc31d12eb> in <module>()
----> 1 np.add(int, int)
TypeError: unsupported operand type(s) for +: 'type' and 'type'
In [4]: np.add(ndarray, ndarray)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-6dcac5b0c00d> in <module>()
----> 1 np.add(ndarray, ndarray)
ValueError: invalid __array_struct__
Should really both be TypeError.
Similarly, the following is arguably less than useful:
In [2]: np.sin(int)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-0b1d09c17e53> in <module>()
----> 1 np.sin(int)
AttributeError: type object 'int' has no attribute 'sin'
The fix for the last case at least is the thing where we need to stop auto-coercing unrecognized objects into dtype=object arrays, as discussed multiple times.
This happens because __array_struct__ is supposed to be <capsule object ...>, but for the ndarray type itself it's unavoidably <attribute '__array_struct__' of 'numpy.ndarray' objects>
This would be fixed by having np.array(x) use type(x).__array_struct__ instead of x.__array_struct__, which is how python does its magic method lookup normally.
Activity
njsmith commentedon Mar 31, 2017
The fix for the last case at least is the thing where we need to stop auto-coercing unrecognized objects into
dtype=objectarrays, as discussed multiple times.eric-wieser commentedon Jan 13, 2019
Second half of this issue is fixed in #12700.
First half isn't really a ufunc problem at all, and comes from:
This happens because
__array_struct__is supposed to be<capsule object ...>, but for the ndarray type itself it's unavoidably<attribute '__array_struct__' of 'numpy.ndarray' objects>This would be fixed by having
np.array(x)usetype(x).__array_struct__instead ofx.__array_struct__, which is how python does its magic method lookup normally.Do we think we can get away with changing that?
mhvk commentedon Jan 13, 2019
Seems sensible to be consistent with how python does it.
BUG: Allow array-like types to be coerced as object array elements
mhvk commentedon Jul 28, 2020
Nice to see this resolved, thanks all!