Skip to content

Conversation

@apdavison
Copy link
Contributor

The reason for the broken in-place behaviour reported in #254 and #258 is that with NumPy 1.x, super().__imul__(other) calls self.__array_prepare__(self, context), which calculates the correct units, but _array_prepare__ was removed from NumPy in NumPy 2.0.

This is an inelegant quick fix, calling self.__array_prepare__(self, context) explicitly. It's inelegant because we have to reconstruct context, and we have similar code in multiple places. This will hopefully be enough to get a release out quickly, then we can revisit this when we have more time and fix it properly, probably by implementing __array_ufunc__.

See:

…on and division with NumPy 2.x

The reason for the failure is that with NumPy 1.25, `super().__imul__(other)` calls `self.__array_prepare__(self, context)`,
but `_array_prepare__` was removed in NumPy 2.0.
Long-term, we should probably implement `__array_ufunc__`.
This inelegant fix will hopefully be enough to get a release out quickly, then we can revisit this when we have more time and fix it properly
@bjodah
Copy link
Contributor

bjodah commented Jan 16, 2026

I think this looks good, thank you for taking the time to fix this!

Possibly a minor nit:

cself = self.copy()
cother = other.copy()

I guess those calls to copy could be made in the else branch only, no?

Anyhow, +1 to merge from me.

@apdavison
Copy link
Contributor Author

@bjodah I had the copy() calls outside the if..else to avoid repeating super().__imul__(other) (which changes self), but on reflection, it's better to repeat the code than to make unnecessary copies, so I've changed it as you suggest.

@apdavison apdavison merged commit 276789d into python-quantities:master Jan 16, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants