Skip to content

Regression on inplace arithmetic operators for numpy>=2 #258

@bjodah

Description

@bjodah

I noticed a bunch of failing tests in a library I'm maintaining when I moved my CI to numpy 2+.
I was relying on python-quantities, and I was making use of __imul__, __iadd__, etc.

I guess I (we) didn't quite get the semantics right for array wrapping for numpy>=2.

Here's a reproducer:

import quantities as pq
velocity = 3 * pq.m/pq.s
time = 2*pq.s
distance = velocity.copy()
distance *= time  # <--- this is the problematic part, if we write distance = distance * time, things work as expected even using numpy>=2
n_centimeters = distance.rescale(pq.centimeter)
print(n_centimeters)
n_cm_ul = n_centimeters.magnitude.item()
assert isinstance(n_cm_ul, float)
err = n_cm_ul - 300*2
print(f"{err=}")
assert abs(err) < 1e-12

Executing it with numpy 1.26.4 and numpy 2+:

(cpython-v3.13-apt-deb) 13:38 root@argus:/home/bjorn/vc/chempy# python regression_quantities2.py 
600.0 cm
err=0.0
(cpython-v3.13-apt-deb) 13:38 root@argus:/home/bjorn/vc/chempy# pip install 'numpy>2'
Collecting numpy>2
  Downloading numpy-2.3.1-cp313-cp313-manylinux_2_28_x86_64.whl.metadata (62 kB)
Downloading numpy-2.3.1-cp313-cp313-manylinux_2_28_x86_64.whl (16.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.6/16.6 MB 48.0 MB/s eta 0:00:00
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 1.26.4
    Uninstalling numpy-1.26.4:
      Successfully uninstalled numpy-1.26.4
Successfully installed numpy-2.3.1
(cpython-v3.13-apt-deb) 13:38 root@argus:/home/bjorn/vc/chempy# python regression_quantities2.py 
Traceback (most recent call last):
  File "/opt-3/cpython-v3.13-apt-deb/lib/python3.13/site-packages/quantities/quantity.py", line 216, in rescale
    cf = get_conversion_factor(from_u, to_u)
  File "/opt-3/cpython-v3.13-apt-deb/lib/python3.13/site-packages/quantities/quantity.py", line 52, in get_conversion_factor
    assert from_u.dimensionality == to_u.dimensionality
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bjorn/vc/chempy/regression_quantities2.py", line 6, in <module>
    n_centimeters = distance.rescale(pq.centimeter)
  File "/opt-3/cpython-v3.13-apt-deb/lib/python3.13/site-packages/quantities/quantity.py", line 218, in rescale
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Unable to convert between units of "m/s" and "cm"

As a work around, I have changed all my call sites from expressions of the form:

my_dict[key] *= other

to:

my_dict[key] = my_dict[key] * other   # <--- works with current released quantities & numpy>=2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions