Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: (NEP 19) fix array repr compatibility with Numpy 2.0 #434

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -959,14 +959,14 @@ bit floating point array.

>>> data = np.array([1, 2, 3], dtype='int32')*km
>>> data.in_units('mile')
unyt_array([0.62137121, 1.24274242, 1.86411357], dtype=float32, units='mile')
unyt_array([0.6213712, 1.2427424, 1.8641136], dtype=float32, units='mile')

In-place operations will also mutate the dtype from float to integer in these
cases, again in a way that will preserve the byte size of the data.

>>> data.convert_to_units('mile')
>>> data
unyt_array([0.62137121, 1.24274242, 1.86411357], dtype=float32, units='mile')
unyt_array([0.6213712, 1.2427424, 1.8641136], dtype=float32, units='mile')

It is possible that arrays containing large integers (16777217 for 32 bit and
9007199254740993 for 64 bit) will lose precision when converting data to a
Expand Down
11 changes: 11 additions & 0 deletions unyt/_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,14 @@ def interp(x, xp, fp, *args, **kwargs):
np.interp(np.asarray(x), np.asarray(xp), np.asarray(fp), *args, **kwargs)
* ret_units
)


@implements(np.array_repr)
def array_repr(arr, *args, **kwargs):
rep = np.array_repr._implementation(arr.view(np.ndarray), *args, **kwargs)
rep = rep.replace("array", arr.__class__.__name__)
units_repr = arr.units.__repr__()
if "=" in rep:
return rep[:-1] + ", units='" + units_repr + "')"
else:
return rep[:-1] + ", '" + units_repr + "')"
7 changes: 1 addition & 6 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,7 @@ def __new__(
return obj

def __repr__(self):
rep = super().__repr__()
units_repr = self.units.__repr__()
if "=" in rep:
return rep[:-1] + ", units='" + units_repr + "')"
else:
return rep[:-1] + ", '" + units_repr + "')"
return np.array_repr(self)

def __str__(self):
return str(self.view(np.ndarray)) + " " + str(self.units)
Expand Down
3 changes: 1 addition & 2 deletions unyt/tests/test_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
np.argpartition, # returns pure numbers
np.argsort, # returns pure numbers
np.argwhere, # returns pure numbers
np.array_repr, # hooks into __repr__
np.array_str, # hooks into __str__
np.atleast_1d, # works out of the box (tested)
np.atleast_2d, # works out of the box (tested)
Expand Down Expand Up @@ -256,7 +255,7 @@ def test_wrapping_completeness():

def test_array_repr():
arr = [1, 2, 3] * cm
assert np.array_repr(arr) == "unyt_array([1, 2, 3], units='cm')"
assert re.fullmatch(r"unyt_array\(\[1, 2, 3\], (units=)?'cm'\)", np.array_repr(arr))


def test_dot_vectors():
Expand Down