- Notifications
You must be signed in to change notification settings - Fork75
Open
Description
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 fornumpy>=2
.
Here's a reproducer:
importquantitiesaspqvelocity=3*pq.m/pq.stime=2*pq.sdistance=velocity.copy()distance*=time# <--- this is the problematic part, if we write distance = distance * time, things work as expected even using numpy>=2n_centimeters=distance.rescale(pq.centimeter)print(n_centimeters)n_cm_ul=n_centimeters.magnitude.item()assertisinstance(n_cm_ul,float)err=n_cm_ul-300*2print(f"{err=}")assertabs(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 cmerr=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:00Installing collected packages: numpy Attempting uninstall: numpy Found existing installation: numpy 1.26.4 Uninstalling numpy-1.26.4: Successfully uninstalled numpy-1.26.4Successfully 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionErrorDuring 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
Labels
No labels