Skip to content

BUG: unnecessary copy on assignment #28070

@DIsrael-LANL

Description

@DIsrael-LANL

Describe the issue:

For assignment using __setitem__, numpy is calling the __array__ method on the value with a copy=True. This can be seen by running the code example.

Reproduce the code example:

import numpy

class MyThing(object):
    def __array__(self, dtype=None, copy=None):
        print(f"MyThing.__array__(dtype={dtype}, copy={copy})")
        return numpy.ones((5, 5))

u = numpy.zeros((5, 5))
v = MyThing()

u[...] = v

Error message:

No response

Python and NumPy Versions:

2.2.1
3.13.1 | packaged by conda-forge | (main, Dec 5 2024, 21:09:18) [Clang 18.1.8 ]

Runtime Environment:

No response

Context for the issue:

This additional copy on a common operation will be extremely inefficient.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions