Skip to content

BUG: Inplace assignment using -= on tuples throws exception yet changes values #28471

@YogevHend

Description

@YogevHend

Describe the issue:

I have encountered an odd behavior when dealing with tuples.
In place substitution of numpy arrays in tuples using the -= syntax throws an exception on one hand but changes the values of the array on the other hand. At the very least this behavior is not what I'd expect as a developer.

Reproduce the code example:

import numpy as np

arr = np.where(np.array([0,1,1,0,0,1]) == 0)

print(arr[0])

for i in arr[0]:
    arr[0] -= 100 # This throws an exception
    print(i)

print(arr[0]) # We'll see that even though an exception was thrown, the values of the array changed.

for i in arr[0]:
    arr[0] = arr[0] - 100 # This does not throw an exception
    print(i)

Error message:

Python and NumPy Versions:

2.2.2
3.12.8 (main, Jan 14 2025, 22:49:36) [MSC v.1942 64 bit (AMD64)]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions