Skip to content

Commit

Permalink
Backport PR astropy#16426: BUG: fix compatibility with numpy 2 in dif…
Browse files Browse the repository at this point in the history
…f reports (io.fits, utils)
  • Loading branch information
mhvk authored and meeseeksmachine committed May 13, 2024
1 parent 4bd9c6d commit 0bfe006
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
3 changes: 2 additions & 1 deletion astropy/io/fits/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ def _report(self):
return

for index, values in self.diff_pixels:
index = [x + 1 for x in reversed(index)]
# Convert to int to avoid np.int64 in list repr.
index = [int(x + 1) for x in reversed(index)]
self._writeln(f" Data differs at {index}:")
report_diff_values(
values[0],
Expand Down
1 change: 1 addition & 0 deletions astropy/io/fits/tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ def test_partially_identical_files1(self):
assert "a: 2\n b: 3" in report
assert "No differences found between common HDUs" in report

@pytest.mark.usefixtures("without_legacy_printoptions")
def test_partially_identical_files2(self):
"""
Test files that have some identical HDUs but one different HDU.
Expand Down
3 changes: 3 additions & 0 deletions astropy/io/fits/tests/test_fitsdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_onediff(self):
numdiff = fitsdiff.main([tmp_a, tmp_b])
assert numdiff == 1

@pytest.mark.usefixtures("without_legacy_printoptions")
def test_manydiff(self, capsys):
a = np.arange(100).reshape(10, 10)
hdu_a = PrimaryHDU(data=a)
Expand Down Expand Up @@ -94,6 +95,7 @@ def test_manydiff(self, capsys):
" 100 different pixels found (100.00% different).",
]

@pytest.mark.usefixtures("without_legacy_printoptions")
def test_outputfile(self):
a = np.arange(100).reshape(10, 10)
hdu_a = PrimaryHDU(data=a)
Expand Down Expand Up @@ -146,6 +148,7 @@ def test_rtol(self):
numdiff = fitsdiff.main(["-r", "1e-1", tmp_a, tmp_b])
assert numdiff == 0

@pytest.mark.usefixtures("without_legacy_printoptions")
def test_rtol_diff(self, capsys):
a = np.arange(100, dtype=float).reshape(10, 10)
hdu_a = PrimaryHDU(data=a)
Expand Down
27 changes: 4 additions & 23 deletions astropy/utils/diff.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import difflib
import functools
import numbers
import sys
from textwrap import indent

Expand Down Expand Up @@ -126,12 +125,8 @@ def report_diff_values(a, b, fileobj=sys.stdout, indent_width=0, rtol=0.0, atol=
lnpad = " "
sign_a = "a>"
sign_b = "b>"
if isinstance(a, numbers.Number):
a = repr(a)
b = repr(b)
else:
a = str(a)
b = str(b)
a = str(a)
b = str(b)
else:
padding = max(len(typea.__name__), len(typeb.__name__)) + 3
lnpad = (padding + 1) * " "
Expand All @@ -140,22 +135,8 @@ def report_diff_values(a, b, fileobj=sys.stdout, indent_width=0, rtol=0.0, atol=

is_a_str = isinstance(a, str)
is_b_str = isinstance(b, str)
a = (
repr(a)
if (
(is_a_str and not is_b_str)
or (not is_a_str and isinstance(a, numbers.Number))
)
else str(a)
)
b = (
repr(b)
if (
(is_b_str and not is_a_str)
or (not is_b_str and isinstance(b, numbers.Number))
)
else str(b)
)
a = repr(a) if is_a_str and not is_b_str else str(a)
b = repr(b) if is_b_str and not is_a_str else str(b)

identical = True

Expand Down
1 change: 1 addition & 0 deletions docs/changes/io.fits/16426.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix display of diff reports with numpy 2.

0 comments on commit 0bfe006

Please sign in to comment.