Skip to content

In-place operators in 1.9 "respected" __array_priority__; in current master they don't #6020

Closed
@njsmith

Description

@njsmith

In 1.9.2:

In [1]: class Foo(object):
   ...:     def __radd__(self, other):
   ...:         print("__radd__ called!")
   ...:     __array_priority__ = 1e9
   ...:     

In [2]: a = np.arange(3)

In [3]: a + Foo()
__radd__ called!

In [4]: a += Foo()
__radd__ called!

In [5]: a

But in current master:

In [3]: a + Foo()
__radd__ called!

In [4]: a += Foo()
TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''

This is because:

#1.9.2
In [3]: np.add(a, Foo(), out=a)
Out[3]: NotImplemented

# master
In [5]: np.add(a, Foo(), out=a)
TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''

I'm pretty sure this is due to #5964 / #5864, i.e., my fault, doh. Though I'm not sure how, exactly; e.g. @pv caught that there was potentially an issue here but then decided it actually only affected object arrays, so right now I'm not even 100% sure that this is the responsible change or what exactly happened. Some careful analysis is needed.

I think the behaviour in master is probably correct, but it could break people's code. So options:

  • Leave it alone unless someone complains
  • Fix it to issue a DeprecationWarning in 1.10, error in 1.11.

And if we go with the latter option, then we have to figure out exactly which cases should return NotImplemented and issue the warning.

This doesn't need to block 1.10 alpha, but it does need some sort of decision before 1.10 final, so I'll tag it with the milestone.

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