1
1
import os
2
- import re
3
2
4
3
import pytest
5
4
import numpy as np
@@ -44,9 +43,9 @@ def test_value_string_union():
44
43
def test_value_string_range ():
45
44
assert_array_equal (_to_ticks ('0..115' ), np .asarray (range (116 )))
46
45
assert_array_equal (_to_ticks ('..115' ), np .asarray (range (116 )))
47
- with pytest . raises (ValueError ):
46
+ with must_raise (ValueError ):
48
47
_to_ticks ('10..' )
49
- with pytest . raises (ValueError ):
48
+ with must_raise (ValueError ):
50
49
_to_ticks ('..' )
51
50
52
51
@@ -211,7 +210,7 @@ def test_ndtest():
211
210
def test_getattr (array ):
212
211
assert type (array .geo ) == Axis
213
212
assert array .geo is geo
214
- with pytest . raises (AttributeError ):
213
+ with must_raise (AttributeError ):
215
214
_ = array .geom
216
215
217
216
@@ -231,7 +230,7 @@ def test_bool():
231
230
a = ones ([2 ])
232
231
# ValueError: The truth value of an array with more than one element
233
232
# is ambiguous. Use a.any() or a.all()
234
- with pytest . raises (ValueError ):
233
+ with must_raise (ValueError ):
235
234
bool (a )
236
235
237
236
a = ones ([1 ])
@@ -525,12 +524,12 @@ def test_getitem(array):
525
524
assert_array_equal (arr [pgroup ], arr ['a1' ])
526
525
527
526
# key with duplicate axes
528
- with pytest . raises (ValueError ):
527
+ with must_raise (ValueError ):
529
528
_ = array [age [1 , 2 ], age [3 , 4 ]]
530
529
531
530
# key with lgroup from another axis leading to duplicate axis
532
531
bad = Axis (3 , 'bad' )
533
- with pytest . raises (ValueError ):
532
+ with must_raise (ValueError ):
534
533
_ = array [bad [1 , 2 ], age [3 , 4 ]]
535
534
536
535
@@ -565,11 +564,11 @@ def test_getitem_abstract_axes(array):
565
564
assert_array_equal (array [..., lipro159 ], raw [..., [0 , 4 , 8 ]])
566
565
567
566
# key with duplicate axes
568
- with pytest . raises (ValueError ):
567
+ with must_raise (ValueError ):
569
568
_ = array [X .age [1 , 2 ], X .age [3 ]]
570
569
571
570
# key with invalid axis
572
- with pytest . raises (ValueError ):
571
+ with must_raise (ValueError ):
573
572
_ = array [X .bad [1 , 2 ], X .age [3 , 4 ]]
574
573
575
574
@@ -703,7 +702,7 @@ def test_getitem_positional_group(array):
703
702
assert_array_equal (array [..., lipro159 ], raw [..., [0 , 4 , 8 ]])
704
703
705
704
# 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\n key: (age.i[1, 2], age.i[3, 4]) " ):
707
706
_ = array [age .i [1 , 2 ], age .i [3 , 4 ]]
708
707
709
708
@@ -747,7 +746,7 @@ def test_getitem_abstract_positional(array):
747
746
assert_array_equal (array [..., lipro159 ], raw [..., [0 , 4 , 8 ]])
748
747
749
748
# 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\n key: (X.age.i[2, 3], X.age.i[1, 5]) " ):
751
750
_ = array [X .age .i [2 , 3 ], X .age .i [1 , 5 ]]
752
751
753
752
@@ -797,8 +796,7 @@ def test_getitem_bool_larray_key_arr_wh_bool_axis():
797
796
798
797
# this test checks that the current behavior does not change unintentionally...
799
798
# ... 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})" ):
802
800
_ = arr [key ]
803
801
804
802
@@ -844,7 +842,7 @@ def test_getitem_bool_ndarray_key_arr_wh_bool_axis():
844
842
# ... but I am unsure the current behavior is what we actually want
845
843
# L? is to account for Python2 where shape can be 'long' integers
846
844
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 ):
848
846
_ = arr [key ]
849
847
850
848
@@ -875,12 +873,12 @@ def test_getitem_integer_string_axes():
875
873
876
874
assert_array_equal (arr ['0[a0, a2]' ], arr [a ['a0' , 'a2' ]])
877
875
assert_array_equal (arr ['0[a0:a2]' ], arr [a ['a0:a2' ]])
878
- with pytest . raises (ValueError ):
876
+ with must_raise (ValueError ):
879
877
_ = arr ['1[a0, a2]' ]
880
878
881
879
assert_array_equal (arr ['0.i[0, 2]' ], arr [a .i [0 , 2 ]])
882
880
assert_array_equal (arr ['0.i[0:2]' ], arr [a .i [0 :2 ]])
883
- with pytest . raises (ValueError ):
881
+ with must_raise (ValueError ):
884
882
_ = arr ['3.i[0, 2]' ]
885
883
886
884
@@ -1165,7 +1163,7 @@ def test_positional_indexer_getitem(array):
1165
1163
for key in [0 , (0 , 5 , 1 , 2 ), (slice (None ), 5 , 1 ), (0 , 5 ), [1 , 0 ], ([1 , 0 ], 5 )]:
1166
1164
assert_array_equal (array .i [key ], raw [key ])
1167
1165
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 ):
1169
1167
_ = array .i [0 , 0 , 0 , 0 , 0 ]
1170
1168
1171
1169
@@ -1208,7 +1206,7 @@ def test_points_indexer_getitem():
1208
1206
assert_array_equal (arr .points [label_key ], raw [index_key ])
1209
1207
1210
1208
# XXX: we might want to raise KeyError or IndexError instead?
1211
- with pytest . raises (ValueError ):
1209
+ with must_raise (ValueError ):
1212
1210
_ = arr .points ['a0' , 'b1' , 'c2' , 'd0' ]
1213
1211
1214
1212
@@ -1393,19 +1391,22 @@ def test_setitem_larray(array, small_array):
1393
1391
value = small_array .copy ()
1394
1392
expected_msg = f"Value { value .axes - subset_axes !s} axis is not present in target subset { subset_axes !s} . " \
1395
1393
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 ):
1397
1395
arr ['P01' ] = value
1398
1396
1399
1397
value = arr .rename ('sex' , 'gender' )['P01' ]
1400
1398
expected_msg = f"Value { value .axes - subset_axes !s} axis is not present in target subset { subset_axes !s} . " \
1401
1399
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 ):
1403
1401
arr ['P01' ] = value
1404
1402
1405
1403
# 7) incompatible labels
1406
1404
sex2 = Axis ('sex=F,M' )
1407
1405
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')""" ):
1409
1410
arr [:] = la2
1410
1411
1411
1412
# key has multiple Arrays (this is used within .points indexing)
@@ -1540,7 +1541,7 @@ def test_setitem_bool_array_key(array):
1540
1541
key = (arr < 5 ).expand ([Axis (2 , 'extra' )])
1541
1542
assert key .ndim == 5
1542
1543
# TODO: make this work
1543
- with pytest . raises (ValueError ):
1544
+ with must_raise (ValueError ):
1544
1545
arr [key ] = 0
1545
1546
1546
1547
@@ -2802,7 +2803,7 @@ def test_broadcasting_no_name():
2802
2803
b = ndtest (Axis (3 ))
2803
2804
c = ndtest (Axis (2 ))
2804
2805
2805
- with pytest . raises (ValueError ):
2806
+ with must_raise (ValueError ):
2806
2807
# ValueError: incompatible axes:
2807
2808
# Axis(None, [0, 1, 2])
2808
2809
# vs
@@ -2823,7 +2824,7 @@ def test_broadcasting_no_name():
2823
2824
assert np .array_equal (d , [[0 , 1 , 4 ], # noqa: E241
2824
2825
[0 , 4 , 10 ]])
2825
2826
2826
- with pytest . raises (ValueError ):
2827
+ with must_raise (ValueError ):
2827
2828
# ValueError: operands could not be broadcast together with shapes (2,3) (2,)
2828
2829
np .asarray (a ) * np .asarray (c )
2829
2830
@@ -3518,8 +3519,8 @@ def test_read_excel_xlwings():
3518
3519
# invalid keyword argument #
3519
3520
##############################
3520
3521
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" ):
3523
3524
read_excel (inputpath ('test.xlsx' ), engine = 'xlwings' , dtype = float )
3524
3525
3525
3526
#################
@@ -4389,7 +4390,7 @@ def test_to_excel_xlwings(tmp_path):
4389
4390
# sheet name of 31 characters (= maximum authorized length)
4390
4391
a3 .to_excel (fpath , "sheetname_of_exactly_31_chars__" , engine = 'xlwings' )
4391
4392
# 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" ):
4393
4394
a3 .to_excel (fpath , "sheetname_longer_than_31_characters" , engine = 'xlwings' )
4394
4395
4395
4396
@@ -4418,7 +4419,7 @@ def test_open_excel(tmp_path):
4418
4419
# ==================
4419
4420
fpath = inputpath ('should_not_exist.xlsx' )
4420
4421
# overwrite_file must be set to True to create a new file
4421
- with pytest . raises (ValueError ):
4422
+ with must_raise (ValueError ):
4422
4423
open_excel (fpath )
4423
4424
4424
4425
# 2) with headers
@@ -5370,7 +5371,7 @@ def test_0darray_convert():
5370
5371
float_arr = Array (1.0 )
5371
5372
assert int (float_arr ) == 1
5372
5373
assert float (float_arr ) == 1.0
5373
- with pytest . raises (TypeError ) as e_info :
5374
+ with must_raise (TypeError ) as e_info :
5374
5375
float_arr .__index__ ()
5375
5376
5376
5377
msg = e_info .value .args [0 ]
0 commit comments