Skip to content

Commit 51c9d14

Browse files
committed
TEST: use must_raise instead of pytest.raises so that we use an exact match by default
1 parent c0eb58b commit 51c9d14

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

larray/tests/test_array.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import re
32

43
import pytest
54
import numpy as np
@@ -44,9 +43,9 @@ def test_value_string_union():
4443
def test_value_string_range():
4544
assert_array_equal(_to_ticks('0..115'), np.asarray(range(116)))
4645
assert_array_equal(_to_ticks('..115'), np.asarray(range(116)))
47-
with pytest.raises(ValueError):
46+
with must_raise(ValueError):
4847
_to_ticks('10..')
49-
with pytest.raises(ValueError):
48+
with must_raise(ValueError):
5049
_to_ticks('..')
5150

5251

@@ -211,7 +210,7 @@ def test_ndtest():
211210
def test_getattr(array):
212211
assert type(array.geo) == Axis
213212
assert array.geo is geo
214-
with pytest.raises(AttributeError):
213+
with must_raise(AttributeError):
215214
_ = array.geom
216215

217216

@@ -231,7 +230,7 @@ def test_bool():
231230
a = ones([2])
232231
# ValueError: The truth value of an array with more than one element
233232
# is ambiguous. Use a.any() or a.all()
234-
with pytest.raises(ValueError):
233+
with must_raise(ValueError):
235234
bool(a)
236235

237236
a = ones([1])
@@ -525,12 +524,12 @@ def test_getitem(array):
525524
assert_array_equal(arr[pgroup], arr['a1'])
526525

527526
# key with duplicate axes
528-
with pytest.raises(ValueError):
527+
with must_raise(ValueError):
529528
_ = array[age[1, 2], age[3, 4]]
530529

531530
# key with lgroup from another axis leading to duplicate axis
532531
bad = Axis(3, 'bad')
533-
with pytest.raises(ValueError):
532+
with must_raise(ValueError):
534533
_ = array[bad[1, 2], age[3, 4]]
535534

536535

@@ -565,11 +564,11 @@ def test_getitem_abstract_axes(array):
565564
assert_array_equal(array[..., lipro159], raw[..., [0, 4, 8]])
566565

567566
# key with duplicate axes
568-
with pytest.raises(ValueError):
567+
with must_raise(ValueError):
569568
_ = array[X.age[1, 2], X.age[3]]
570569

571570
# key with invalid axis
572-
with pytest.raises(ValueError):
571+
with must_raise(ValueError):
573572
_ = array[X.bad[1, 2], X.age[3, 4]]
574573

575574

@@ -703,7 +702,7 @@ def test_getitem_positional_group(array):
703702
assert_array_equal(array[..., lipro159], raw[..., [0, 4, 8]])
704703

705704
# key with duplicate axes
706-
with pytest.raises(ValueError, match="key has several values for axis: age"):
705+
with must_raise(ValueError, "key has several values for axis: age\nkey: (age.i[1, 2], age.i[3, 4])"):
707706
_ = array[age.i[1, 2], age.i[3, 4]]
708707

709708

@@ -747,7 +746,7 @@ def test_getitem_abstract_positional(array):
747746
assert_array_equal(array[..., lipro159], raw[..., [0, 4, 8]])
748747

749748
# key with duplicate axes
750-
with pytest.raises(ValueError, match="key has several values for axis: age"):
749+
with must_raise(ValueError, "key has several values for axis: age\nkey: (X.age.i[2, 3], X.age.i[1, 5])"):
751750
_ = array[X.age.i[2, 3], X.age.i[1, 5]]
752751

753752

@@ -797,8 +796,7 @@ def test_getitem_bool_larray_key_arr_wh_bool_axis():
797796

798797
# this test checks that the current behavior does not change unintentionally...
799798
# ... but I am unsure the current behavior is what we actually want
800-
msg = re.escape("boolean subset key contains more axes ({id}) than array ({gender})")
801-
with pytest.raises(ValueError, match=msg):
799+
with must_raise(ValueError, "boolean subset key contains more axes ({id}) than array ({gender})"):
802800
_ = arr[key]
803801

804802

@@ -844,7 +842,7 @@ def test_getitem_bool_ndarray_key_arr_wh_bool_axis():
844842
# ... but I am unsure the current behavior is what we actually want
845843
# L? is to account for Python2 where shape can be 'long' integers
846844
msg = r"boolean key with a different shape \(\(4L?,\)\) than array \(\(2,\)\)"
847-
with pytest.raises(ValueError, match=msg):
845+
with must_raise(ValueError, match=msg):
848846
_ = arr[key]
849847

850848

@@ -875,12 +873,12 @@ def test_getitem_integer_string_axes():
875873

876874
assert_array_equal(arr['0[a0, a2]'], arr[a['a0', 'a2']])
877875
assert_array_equal(arr['0[a0:a2]'], arr[a['a0:a2']])
878-
with pytest.raises(ValueError):
876+
with must_raise(ValueError):
879877
_ = arr['1[a0, a2]']
880878

881879
assert_array_equal(arr['0.i[0, 2]'], arr[a.i[0, 2]])
882880
assert_array_equal(arr['0.i[0:2]'], arr[a.i[0:2]])
883-
with pytest.raises(ValueError):
881+
with must_raise(ValueError):
884882
_ = arr['3.i[0, 2]']
885883

886884

@@ -1165,7 +1163,7 @@ def test_positional_indexer_getitem(array):
11651163
for key in [0, (0, 5, 1, 2), (slice(None), 5, 1), (0, 5), [1, 0], ([1, 0], 5)]:
11661164
assert_array_equal(array.i[key], raw[key])
11671165
assert_array_equal(array.i[[1, 0], [5, 4]], raw[np.ix_([1, 0], [5, 4])])
1168-
with pytest.raises(IndexError):
1166+
with must_raise(IndexError):
11691167
_ = array.i[0, 0, 0, 0, 0]
11701168

11711169

@@ -1208,7 +1206,7 @@ def test_points_indexer_getitem():
12081206
assert_array_equal(arr.points[label_key], raw[index_key])
12091207

12101208
# XXX: we might want to raise KeyError or IndexError instead?
1211-
with pytest.raises(ValueError):
1209+
with must_raise(ValueError):
12121210
_ = arr.points['a0', 'b1', 'c2', 'd0']
12131211

12141212

@@ -1393,19 +1391,22 @@ def test_setitem_larray(array, small_array):
13931391
value = small_array.copy()
13941392
expected_msg = f"Value {value.axes - subset_axes!s} axis is not present in target subset {subset_axes!s}. " \
13951393
f"A value can only have the same axes or fewer axes than the subset being targeted"
1396-
with pytest.raises(ValueError, match=expected_msg):
1394+
with must_raise(ValueError, expected_msg):
13971395
arr['P01'] = value
13981396

13991397
value = arr.rename('sex', 'gender')['P01']
14001398
expected_msg = f"Value {value.axes - subset_axes!s} axis is not present in target subset {subset_axes!s}. " \
14011399
f"A value can only have the same axes or fewer axes than the subset being targeted"
1402-
with pytest.raises(ValueError, match=expected_msg):
1400+
with must_raise(ValueError, expected_msg):
14031401
arr['P01'] = value
14041402

14051403
# 7) incompatible labels
14061404
sex2 = Axis('sex=F,M')
14071405
la2 = Array(small_array.data, axes=(sex2, lipro))
1408-
with pytest.raises(ValueError, match="incompatible axes:"):
1406+
with must_raise(ValueError, """incompatible axes:
1407+
Axis(['M', 'F'], 'c')
1408+
vs
1409+
Axis(['F', 'M'], 'c')"""):
14091410
arr[:] = la2
14101411

14111412
# key has multiple Arrays (this is used within .points indexing)
@@ -1540,7 +1541,7 @@ def test_setitem_bool_array_key(array):
15401541
key = (arr < 5).expand([Axis(2, 'extra')])
15411542
assert key.ndim == 5
15421543
# TODO: make this work
1543-
with pytest.raises(ValueError):
1544+
with must_raise(ValueError):
15441545
arr[key] = 0
15451546

15461547

@@ -2802,7 +2803,7 @@ def test_broadcasting_no_name():
28022803
b = ndtest(Axis(3))
28032804
c = ndtest(Axis(2))
28042805

2805-
with pytest.raises(ValueError):
2806+
with must_raise(ValueError):
28062807
# ValueError: incompatible axes:
28072808
# Axis(None, [0, 1, 2])
28082809
# vs
@@ -2823,7 +2824,7 @@ def test_broadcasting_no_name():
28232824
assert np.array_equal(d, [[0, 1, 4], # noqa: E241
28242825
[0, 4, 10]])
28252826

2826-
with pytest.raises(ValueError):
2827+
with must_raise(ValueError):
28272828
# ValueError: operands could not be broadcast together with shapes (2,3) (2,)
28282829
np.asarray(a) * np.asarray(c)
28292830

@@ -3518,8 +3519,8 @@ def test_read_excel_xlwings():
35183519
# invalid keyword argument #
35193520
##############################
35203521

3521-
with pytest.raises(TypeError, match="'dtype' is an invalid keyword argument for this function "
3522-
"when using the xlwings backend"):
3522+
with must_raise(TypeError, "'dtype' is an invalid keyword argument for this function "
3523+
"when using the xlwings backend"):
35233524
read_excel(inputpath('test.xlsx'), engine='xlwings', dtype=float)
35243525

35253526
#################
@@ -4389,7 +4390,7 @@ def test_to_excel_xlwings(tmp_path):
43894390
# sheet name of 31 characters (= maximum authorized length)
43904391
a3.to_excel(fpath, "sheetname_of_exactly_31_chars__", engine='xlwings')
43914392
# sheet name longer than 31 characters
4392-
with pytest.raises(ValueError, match="Sheet names cannot exceed 31 characters"):
4393+
with must_raise(ValueError, "Sheet names cannot exceed 31 characters"):
43934394
a3.to_excel(fpath, "sheetname_longer_than_31_characters", engine='xlwings')
43944395

43954396

@@ -4418,7 +4419,7 @@ def test_open_excel(tmp_path):
44184419
# ==================
44194420
fpath = inputpath('should_not_exist.xlsx')
44204421
# overwrite_file must be set to True to create a new file
4421-
with pytest.raises(ValueError):
4422+
with must_raise(ValueError):
44224423
open_excel(fpath)
44234424

44244425
# 2) with headers
@@ -5370,7 +5371,7 @@ def test_0darray_convert():
53705371
float_arr = Array(1.0)
53715372
assert int(float_arr) == 1
53725373
assert float(float_arr) == 1.0
5373-
with pytest.raises(TypeError) as e_info:
5374+
with must_raise(TypeError) as e_info:
53745375
float_arr.__index__()
53755376

53765377
msg = e_info.value.args[0]

0 commit comments

Comments
 (0)