8000 FIX: Fix unit example so that we can unpin numpy>2.1 · matplotlib/matplotlib@2c087c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c087c1

Browse files
committed
FIX: Fix unit example so that we can unpin numpy>2.1
Closes #28780. The underlying problem is that operations on numpy scalars try to eagerly convert the other operand to an array. As a result `scalar = np .float64 (2); scalar * radians` would result in a numpy scalar. But we don't want that. Instead we enforce `radians.__rmul__(scalar)` by giving the unit a higher `__array_priority__`. See also https://github .com/numpy/numpy/issues/17650. I haven't found any specific change notes on this in numpy 2.1. Interestingly, the full story is even more complex. Also for numpy<2.1 `radians.__rmul__(scalar)` is not called, but there seems another mechanism through __array__ and __array_warp__ catching back in so that the result is again a TaggedValue. But I have not fully investigated why it worked previously. In fact, we want the solution here with going through __rmul__, and that works for all numpy versions. `
1 parent 2d5e503 commit 2c087c1

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ commands:
9898
parameters:
9999
numpy_version:
100100
type: string
101-
default: "~=2.0.0"
101+
default: ""
102102
steps:
103103
- run:
104104
name: Install Python dependencies

environment.yml

Lines changed: 1 addition & 1 deletion
8000
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies:
1818
- kiwisolver>=1.3.1
1919
- pybind11>=2.13.2
2020
- meson-python>=0.13.1
21-
- numpy<2.1
21+
- numpy
2222
- pillow>=9
2323
- pkg-config
2424
- pygobject

galleries/examples/units/basic_units.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ def get_unit(self):
193193

194194

195195
class BasicUnit:
196+
# numpy scalars convert eager and np.float64(2) * BasicUnit('cm')
197+
# would thus return a numpy scalar. To avoid this, we increase the
198+
# priority of the BasicUnit.
199+
__array_priority__ = np.float64(0).__array_priority__ + 1
200+
196201
def __init__(self, name, fullname=None):
197202
self.name = name
198203
if fullname is None:

0 commit comments

Comments
 (0)
0