Skip to content

BUG: np.<ufunc>(np.ndarray) should raise TypeError #8877

Closed

Description

@mhvk

As noted by @charris in charris#5 (comment)

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'

Activity

  1. njsmith commented on Mar 31, 2017

    @njsmith
    Member

    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.

  2. eric-wieser commented on Jan 13, 2019

    @eric-wieser
    Member

    Second half of this issue is fixed in #12700.

    First half isn't really a ufunc problem at all, and comes from:

    >>> np.array(np.ndarray)
    Traceback (most recent call last):
      File "<pyshell#25>", line 1, in <module>
        np.array(np.ndarray)
    ValueError: invalid __array_struct__

    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.

    Do we think we can get away with changing that?

  3. mhvk commented on Jan 13, 2019

    @mhvk
    ContributorAuthor

    Seems sensible to be consistent with how python does it.

  4. added a commit that references this issue on Jul 24, 2020
    d45b16d
  5. mhvk commented on Jul 28, 2020

    @mhvk
    ContributorAuthor

    Nice to see this resolved, thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions