Skip to content

Commit 0fafdd4

Browse files
committed
TEST: handle pytest 8.3.4 change in bool tests with approx
Fix sherpa#2202 In pytest 8.3.3 and earlier >>> import numpy as np; import pytest >>> pytest.__version__ '8.3.3' >>> np.ones(2, dtype=bool) == pytest.approx([True, True]) True However, pytest 8.3.4 now causes this to fail >>> import numpy as np; import pytest >>> pytest.__version__ '8.3.4' >>> np.ones(2, dtype=bool) == pytest.approx([True, True]) False This is because of "pytest.approx considers boolean numeric types" pytest-dev/pytest#9353 The solution is to make the "expected" value be a ndarray, and so >>> np.ones(2, dtype=bool) == pytest.approx(np.asarray([True, True])) True holds with both pytest 8.3.3 and 8.3.4. So this commit basically goes through and updates the tests so that we use a ndarray for boolean arrays. An alternative would be to change from assert got == pytest.approx(expected) to something like assert np.all(got == expected) However, the error message when the array lengths are different or an element is different are a **lot less** useful, and the change would be even-more invasive than this change.
1 parent c034afa commit 0fafdd4

13 files changed

+146
-96
lines changed

sherpa/astro/io/tests/test_io_pha.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ def test_write_pha_with_bad_quality(tmp_path):
15041504
counts = chans * 2
15051505
group = [1, -1, -1, 1, -1, 1, 1, 1, -1]
15061506
quality = [0, 5, 0, 0, 0, 0, 0, 2, 2]
1507-
qfilt = [True, False] + [True] * 5 + [False] * 2
1507+
qfilt = np.asarray([True, False] + [True] * 5 + [False] * 2)
15081508

15091509
pha0 = DataPHA("qual", chans, counts, grouping=group,
15101510
quality=quality)

sherpa/astro/tests/test_astro_data.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -2367,7 +2367,7 @@ def test_pha_check_limit(ignore, lo, hi, evals):
23672367
pha.units = 'energy'
23682368

23692369
assert pha.mask is True
2370-
assert pha.get_mask() == pytest.approx([True] * 10)
2370+
assert pha.get_mask() == pytest.approx(np.ones(10, dtype=bool))
23712371

23722372
func = pha.ignore if ignore else pha.notice
23732373
func(lo, hi)
@@ -2380,7 +2380,7 @@ def test_pha_check_limit(ignore, lo, hi, evals):
23802380
vin = True
23812381

23822382
c1, c2, c3 = evals
2383-
expected = [vout] * c1 + [vin] * c2 + [vout] * c3
2383+
expected = np.asarray([vout] * c1 + [vin] * c2 + [vout] * c3)
23842384
assert pha.mask == pytest.approx(pha.get_mask())
23852385
assert pha.mask == pytest.approx(expected)
23862386

@@ -2449,7 +2449,7 @@ def test_pha_check_limit_channel(ignore, lo, hi, evals):
24492449
pha.units = 'channel'
24502450

24512451
assert pha.mask is True
2452-
assert pha.get_mask() == pytest.approx([True] * 10)
2452+
assert pha.get_mask() == pytest.approx(np.ones(10, dtype=bool))
24532453

24542454
func = pha.ignore if ignore else pha.notice
24552455
func(lo, hi)
@@ -2462,7 +2462,7 @@ def test_pha_check_limit_channel(ignore, lo, hi, evals):
24622462
vin = True
24632463

24642464
c1, c2, c3 = evals
2465-
expected = [vout] * c1 + [vin] * c2 + [vout] * c3
2465+
expected = np.asarray([vout] * c1 + [vin] * c2 + [vout] * c3)
24662466
assert pha.mask == pytest.approx(pha.get_mask())
24672467
assert pha.mask == pytest.approx(expected)
24682468

@@ -2672,7 +2672,9 @@ def test_is_mask_reset_pha(caplog):
26722672
# Pick a value somewhere within the independent axis
26732673
assert data.mask is True
26742674
data.ignore(None, 2)
2675-
assert data.mask == pytest.approx([False, False, True])
2675+
2676+
mask = np.asarray([False, False, True])
2677+
assert data.mask == pytest.approx(mask)
26762678

26772679
# Change the independent axis, but to something of the same
26782680
# length.
@@ -2683,7 +2685,7 @@ def test_is_mask_reset_pha(caplog):
26832685
assert len(caplog.records) == 0
26842686

26852687
# The mask has *not* been cleared
2686-
assert data.mask == pytest.approx([False, False, True])
2688+
assert data.mask == pytest.approx(mask)
26872689

26882690

26892691
def test_is_mask_reset_pha_channel(caplog):
@@ -2703,7 +2705,8 @@ def test_is_mask_reset_pha_channel(caplog):
27032705
assert len(caplog.records) == 0
27042706

27052707
# The mask has not been cleared
2706-
assert data.mask == pytest.approx([False, False, True])
2708+
mask = np.asarray([False, False, True])
2709+
assert data.mask == pytest.approx(mask)
27072710

27082711

27092712
@requires_region
@@ -3376,9 +3379,11 @@ def test_pha_notice_bkg_id_none():
33763379

33773380
pha.notice(lo=2, bkg_id=None) # the default
33783381

3379-
assert pha.mask == pytest.approx([False, True])
3380-
assert b1.mask == pytest.approx([False, True])
3381-
assert bup.mask == pytest.approx([False, True])
3382+
bfilt = np.asarray([False, True])
3383+
3384+
assert pha.mask == pytest.approx(bfilt)
3385+
assert b1.mask == pytest.approx(bfilt)
3386+
assert bup.mask == pytest.approx(bfilt)
33823387

33833388

33843389
@pytest.mark.parametrize("bkg_id", [1, "up"])
@@ -3394,13 +3399,15 @@ def test_pha_notice_bkg_id_scalar(bkg_id):
33943399

33953400
pha.notice(lo=2, bkg_id=bkg_id)
33963401

3402+
bfilt = np.asarray([False, True])
3403+
33973404
assert pha.mask is True
33983405
if bkg_id == 1:
3399-
assert b1.mask == pytest.approx([False, True])
3406+
assert b1.mask == pytest.approx(bfilt)
34003407
assert bup.mask is True
34013408
else:
34023409
assert b1.mask is True
3403-
assert bup.mask == pytest.approx([False, True])
3410+
assert bup.mask == pytest.approx(bfilt)
34043411

34053412

34063413
def test_pha_notice_bkg_id_array_all():
@@ -3415,9 +3422,11 @@ def test_pha_notice_bkg_id_array_all():
34153422

34163423
pha.notice(lo=2, bkg_id=["up", 1])
34173424

3425+
bfilt = np.asarray([False, True])
3426+
34183427
assert pha.mask is True
3419-
assert b1.mask == pytest.approx([False, True])
3420-
assert bup.mask == pytest.approx([False, True])
3428+
assert b1.mask == pytest.approx(bfilt)
3429+
assert bup.mask == pytest.approx(bfilt)
34213430

34223431

34233432
@pytest.mark.parametrize("bkg_id", [1, "up"])
@@ -3433,13 +3442,15 @@ def test_pha_notice_bkg_id_array_subset(bkg_id):
34333442

34343443
pha.notice(lo=2, bkg_id=[bkg_id])
34353444

3445+
bfilt = np.asarray([False, True])
3446+
34363447
assert pha.mask is True
34373448
if bkg_id == 1:
3438-
assert b1.mask == pytest.approx([False, True])
3449+
assert b1.mask == pytest.approx(bfilt)
34393450
assert bup.mask is True
34403451
else:
34413452
assert b1.mask is True
3442-
assert bup.mask == pytest.approx([False, True])
3453+
assert bup.mask == pytest.approx(bfilt)
34433454

34443455

34453456
def get_img_spatial_mask():

0 commit comments

Comments
 (0)