Skip to content

Commit b72f90b

Browse files
mroeschkepmhatre1
authored andcommitted
TST: Scope pytest.raises closer to failing line (pandas-dev#56746)
1 parent 77b9c29 commit b72f90b

22 files changed

+65
-69
lines changed

pandas/tests/computation/test_eval.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class TestEval:
141141
def test_complex_cmp_ops(self, cmp1, cmp2, binop, lhs, rhs, engine, parser):
142142
if parser == "python" and binop in ["and", "or"]:
143143
msg = "'BoolOp' nodes are not implemented"
144+
ex = f"(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)"
144145
with pytest.raises(NotImplementedError, match=msg):
145-
ex = f"(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)"
146146
pd.eval(ex, engine=engine, parser=parser)
147147
return
148148

@@ -161,9 +161,8 @@ def test_simple_cmp_ops(self, cmp_op, lhs, rhs, engine, parser):
161161

162162
if parser == "python" and cmp_op in ["in", "not in"]:
163163
msg = "'(In|NotIn)' nodes are not implemented"
164-
164+
ex = f"lhs {cmp_op} rhs"
165165
with pytest.raises(NotImplementedError, match=msg):
166-
ex = f"lhs {cmp_op} rhs"
167166
pd.eval(ex, engine=engine, parser=parser)
168167
return
169168

@@ -193,8 +192,8 @@ def test_simple_cmp_ops(self, cmp_op, lhs, rhs, engine, parser):
193192
def test_compound_invert_op(self, op, lhs, rhs, request, engine, parser):
194193
if parser == "python" and op in ["in", "not in"]:
195194
msg = "'(In|NotIn)' nodes are not implemented"
195+
ex = f"~(lhs {op} rhs)"
196196
with pytest.raises(NotImplementedError, match=msg):
197-
ex = f"~(lhs {op} rhs)"
198197
pd.eval(ex, engine=engine, parser=parser)
199198
return
200199

pandas/tests/frame/methods/test_compare.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,25 @@ def test_compare_multi_index(align_axis):
168168
tm.assert_frame_equal(result, expected)
169169

170170

171-
def test_compare_unaligned_objects():
172-
# test DataFrames with different indices
171+
def test_compare_different_indices():
173172
msg = (
174173
r"Can only compare identically-labeled \(both index and columns\) DataFrame "
175174
"objects"
176175
)
176+
df1 = pd.DataFrame([1, 2, 3], index=["a", "b", "c"])
177+
df2 = pd.DataFrame([1, 2, 3], index=["a", "b", "d"])
177178
with pytest.raises(ValueError, match=msg):
178-
df1 = pd.DataFrame([1, 2, 3], index=["a", "b", "c"])
179-
df2 = pd.DataFrame([1, 2, 3], index=["a", "b", "d"])
180179
df1.compare(df2)
181180

182-
# test DataFrames with different shapes
181+
182+
def test_compare_different_shapes():
183183
msg = (
184184
r"Can only compare identically-labeled \(both index and columns\) DataFrame "
185185
"objects"
186186
)
187+
df1 = pd.DataFrame(np.ones((3, 3)))
188+
df2 = pd.DataFrame(np.zeros((2, 1)))
187189
with pytest.raises(ValueError, match=msg):
188-
df1 = pd.DataFrame(np.ones((3, 3)))
189-
df2 = pd.DataFrame(np.zeros((2, 1)))
190190
df1.compare(df2)
191191

192192

pandas/tests/frame/methods/test_sample.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,10 @@ def test_sample_invalid_weight_lengths(self, obj):
111111
obj.sample(n=3, weights=[0, 1])
112112

113113
with pytest.raises(ValueError, match=msg):
114-
bad_weights = [0.5] * 11
115-
obj.sample(n=3, weights=bad_weights)
114+
obj.sample(n=3, weights=[0.5] * 11)
116115

117116
with pytest.raises(ValueError, match="Fewer non-zero entries in p than size"):
118-
bad_weight_series = Series([0, 0, 0.2])
119-
obj.sample(n=4, weights=bad_weight_series)
117+
obj.sample(n=4, weights=Series([0, 0, 0.2]))
120118

121119
def test_sample_negative_weights(self, obj):
122120
# Check won't accept negative weights

pandas/tests/frame/methods/test_tz_convert.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,23 @@ def test_tz_convert_and_localize(self, fn):
9898
tm.assert_index_equal(df3.index.levels[1], l1_expected)
9999
assert not df3.index.levels[1].equals(l1)
100100

101-
# Bad Inputs
102-
101+
@pytest.mark.parametrize("fn", ["tz_localize", "tz_convert"])
102+
def test_tz_convert_and_localize_bad_input(self, fn):
103+
int_idx = Index(range(5))
104+
l0 = date_range("20140701", periods=5, freq="D")
103105
# Not DatetimeIndex / PeriodIndex
106+
df = DataFrame(index=int_idx)
104107
with pytest.raises(TypeError, match="DatetimeIndex"):
105-
df = DataFrame(index=int_idx)
106108
getattr(df, fn)("US/Pacific")
107109

108110
# Not DatetimeIndex / PeriodIndex
111+
df = DataFrame(np.ones(5), MultiIndex.from_arrays([int_idx, l0]))
109112
with pytest.raises(TypeError, match="DatetimeIndex"):
110-
df = DataFrame(np.ones(5), MultiIndex.from_arrays([int_idx, l0]))
111113
getattr(df, fn)("US/Pacific", level=0)
112114

113115
# Invalid level
116+
df = DataFrame(index=l0)
114117
with pytest.raises(ValueError, match="not valid"):
115-
df = DataFrame(index=l0)
116118
getattr(df, fn)("US/Pacific", level=1)
117119

118120
@pytest.mark.parametrize("copy", [True, False])

pandas/tests/groupby/aggregate/test_aggregate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ def numpystd(x):
466466

467467
# this uses column selection & renaming
468468
msg = r"nested renamer is not supported"
469+
d = {"C": "mean", "D": {"foo": "mean", "bar": "std"}}
469470
with pytest.raises(SpecificationError, match=msg):
470-
d = {"C": "mean", "D": {"foo": "mean", "bar": "std"}}
471471
grouped.aggregate(d)
472472

473473
# But without renaming, these functions are OK

pandas/tests/indexes/datetimes/test_partial_slicing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ def test_getitem_with_datestring_with_UTC_offset(self, start, end):
450450
with pytest.raises(ValueError, match="Both dates must"):
451451
df[start : end[:-4] + "1:00"]
452452

453+
df = df.tz_localize(None)
453454
with pytest.raises(ValueError, match="The index must be timezone"):
454-
df = df.tz_localize(None)
455455
df[start:end]
456456

457457
def test_slice_reduce_to_series(self):

pandas/tests/indexing/test_na_indexing.py

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_series_mask_boolean(values, dtype, mask, indexer_class, frame):
5454
msg = "iLocation based boolean indexing cannot use an indexable as a mask"
5555
with pytest.raises(ValueError, match=msg):
5656
result = obj.iloc[mask]
57-
tm.assert_equal(result, expected)
5857
else:
5958
result = obj.iloc[mask]
6059
tm.assert_equal(result, expected)

pandas/tests/indexing/test_scalar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def test_iat_set_ints(self, dtype, frame_or_series):
5353
def test_iat_set_other(self, index, frame_or_series):
5454
f = frame_or_series(range(len(index)), index=index)
5555
msg = "iAt based indexing can only have integer indexers"
56+
idx = next(generate_indices(f, False))
5657
with pytest.raises(ValueError, match=msg):
57-
idx = next(generate_indices(f, False))
5858
f.iat[idx] = 1
5959

6060
@pytest.mark.parametrize(

pandas/tests/internals/test_internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ def test_duplicate_ref_loc_failure(self):
381381

382382
msg = "Gaps in blk ref_locs"
383383

384+
mgr = BlockManager(blocks, axes)
384385
with pytest.raises(AssertionError, match=msg):
385-
mgr = BlockManager(blocks, axes)
386386
mgr._rebuild_blknos_and_blklocs()
387387

388388
blocks[0].mgr_locs = BlockPlacement(np.array([0]))

pandas/tests/io/excel/test_openpyxl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ def test_if_sheet_exists_raises(ext, if_sheet_exists, msg):
269269
# GH 40230
270270
df = DataFrame({"fruit": ["pear"]})
271271
with tm.ensure_clean(ext) as f:
272+
df.to_excel(f, sheet_name="foo", engine="openpyxl")
272273
with pytest.raises(ValueError, match=re.escape(msg)):
273-
df.to_excel(f, sheet_name="foo", engine="openpyxl")
274274
with ExcelWriter(
275275
f, engine="openpyxl", mode="a", if_sheet_exists=if_sheet_exists
276276
) as writer:

pandas/tests/io/parser/dtypes/test_empty.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def test_empty_with_dup_column_pass_dtype_by_indexes_raises(all_parsers):
125125
expected.index = expected.index.astype(object)
126126

127127
with pytest.raises(ValueError, match="Duplicate names"):
128-
data = ""
129-
parser.read_csv(StringIO(data), names=["one", "one"], dtype={0: "u1", 1: "f"})
128+
parser.read_csv(StringIO(""), names=["one", "one"], dtype={0: "u1", 1: "f"})
130129

131130

132131
@pytest.mark.parametrize(

pandas/tests/io/parser/test_header.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def test_read_with_bad_header(all_parsers):
3232
msg = r"but only \d+ lines in file"
3333

3434
with pytest.raises(ValueError, match=msg):
35-
s = StringIO(",,")
36-
parser.read_csv(s, header=[10])
35+
parser.read_csv(StringIO(",,"), header=[10])
3736

3837

3938
def test_negative_header(all_parsers):

pandas/tests/io/test_pickle.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ def test_write_explicit(self, compression, get_random_path):
309309

310310
@pytest.mark.parametrize("compression", ["", "None", "bad", "7z"])
311311
def test_write_explicit_bad(self, compression, get_random_path):
312-
with pytest.raises(ValueError, match="Unrecognized compression type"):
313-
with tm.ensure_clean(get_random_path) as path:
314-
df = DataFrame(
315-
1.1 * np.arange(120).reshape((30, 4)),
316-
columns=Index(list("ABCD"), dtype=object),
317-
index=Index([f"i-{i}" for i in range(30)], dtype=object),
318-
)
312+
df = DataFrame(
313+
1.1 * np.arange(120).reshape((30, 4)),
314+
columns=Index(list("ABCD"), dtype=object),
315+
index=Index([f"i-{i}" for i in range(30)], dtype=object),
316+
)
317+
with tm.ensure_clean(get_random_path) as path:
318+
with pytest.raises(ValueError, match="Unrecognized compression type"):
319319
df.to_pickle(path, compression=compression)
320320

321321
def test_write_infer(self, compression_ext, get_random_path):

pandas/tests/io/test_stata.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -957,20 +957,18 @@ def test_drop_column(self, datapath):
957957

958958
msg = "columns contains duplicate entries"
959959
with pytest.raises(ValueError, match=msg):
960-
columns = ["byte_", "byte_"]
961960
read_stata(
962961
datapath("io", "data", "stata", "stata6_117.dta"),
963962
convert_dates=True,
964-
columns=columns,
963+
columns=["byte_", "byte_"],
965964
)
966965

967966
msg = "The following columns were not found in the Stata data set: not_found"
968967
with pytest.raises(ValueError, match=msg):
969-
columns = ["byte_", "int_", "long_", "not_found"]
970968
read_stata(
971969
datapath("io", "data", "stata", "stata6_117.dta"),
972970
convert_dates=True,
973-
columns=columns,
971+
columns=["byte_", "int_", "long_", "not_found"],
974972
)
975973

976974
@pytest.mark.parametrize("version", [114, 117, 118, 119, None])
@@ -2196,16 +2194,16 @@ def test_non_categorical_value_labels():
21962194
assert reader_value_labels == expected
21972195

21982196
msg = "Can't create value labels for notY, it wasn't found in the dataset."
2197+
value_labels = {"notY": {7: "label1", 8: "label2"}}
21992198
with pytest.raises(KeyError, match=msg):
2200-
value_labels = {"notY": {7: "label1", 8: "label2"}}
22012199
StataWriter(path, data, value_labels=value_labels)
22022200

22032201
msg = (
22042202
"Can't create value labels for Z, value labels "
22052203
"can only be applied to numeric columns."
22062204
)
2205+
value_labels = {"Z": {1: "a", 2: "k", 3: "j", 4: "i"}}
22072206
with pytest.raises(ValueError, match=msg):
2208-
value_labels = {"Z": {1: "a", 2: "k", 3: "j", 4: "i"}}
22092207
StataWriter(path, data, value_labels=value_labels)
22102208

22112209

pandas/tests/io/xml/test_xml.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,22 @@ def test_empty_string_lxml(val):
471471
r"None \(line 0\)",
472472
]
473473
)
474+
if isinstance(val, str):
475+
data = StringIO(val)
476+
else:
477+
data = BytesIO(val)
474478
with pytest.raises(lxml_etree.XMLSyntaxError, match=msg):
475-
if isinstance(val, str):
476-
read_xml(StringIO(val), parser="lxml")
477-
else:
478-
read_xml(BytesIO(val), parser="lxml")
479+
read_xml(data, parser="lxml")
479480

480481

481482
@pytest.mark.parametrize("val", ["", b""])
482483
def test_empty_string_etree(val):
484+
if isinstance(val, str):
485+
data = StringIO(val)
486+
else:
487+
data = BytesIO(val)
483488
with pytest.raises(ParseError, match="no element found"):
484-
if isinstance(val, str):
485-
read_xml(StringIO(val), parser="etree")
486-
else:
487-
read_xml(BytesIO(val), parser="etree")
489+
read_xml(data, parser="etree")
488490

489491

490492
def test_wrong_file_path(parser):

pandas/tests/plotting/test_boxplot_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,8 @@ def test_grouped_box_multiple_axes_ax_error(self, hist_df):
663663
# GH 6970, GH 7069
664664
df = hist_df
665665
msg = "The number of passed axes must be 3, the same as the output plot"
666+
_, axes = mpl.pyplot.subplots(2, 3)
666667
with pytest.raises(ValueError, match=msg):
667-
fig, axes = mpl.pyplot.subplots(2, 3)
668668
# pass different number of axes from required
669669
with tm.assert_produces_warning(UserWarning):
670670
axes = df.groupby("classroom").boxplot(ax=axes)

pandas/tests/plotting/test_hist_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ def test_hist_with_nans_and_weights(self):
655655
idxerror_weights = np.array([[0.3, 0.25], [0.45, 0.45]])
656656

657657
msg = "weights must have the same shape as data, or be a single column"
658+
_, ax2 = mpl.pyplot.subplots()
658659
with pytest.raises(ValueError, match=msg):
659-
_, ax2 = mpl.pyplot.subplots()
660660
no_nan_df.plot.hist(ax=ax2, weights=idxerror_weights)
661661

662662

pandas/tests/reshape/test_util.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def test_exceed_product_space(self):
7272
# GH31355: raise useful error when produce space is too large
7373
msg = "Product space too large to allocate arrays!"
7474

75+
dims = [np.arange(0, 22, dtype=np.int16) for i in range(12)] + [
76+
(np.arange(15128, dtype=np.int16)),
77+
]
7578
with pytest.raises(ValueError, match=msg):
76-
dims = [np.arange(0, 22, dtype=np.int16) for i in range(12)] + [
77-
(np.arange(15128, dtype=np.int16)),
78-
]
7979
cartesian_product(X=dims)

pandas/tests/series/methods/test_between.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ def test_between_error_args(self, inclusive):
7070
"'left', 'right', or 'neither'."
7171
)
7272

73+
series = Series(date_range("1/1/2000", periods=10))
7374
with pytest.raises(ValueError, match=value_error_msg):
74-
series = Series(date_range("1/1/2000", periods=10))
7575
series.between(left, right, inclusive=inclusive)

pandas/tests/series/methods/test_compare.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ def test_compare_multi_index():
9999
tm.assert_series_equal(result, expected)
100100

101101

102-
def test_compare_unaligned_objects():
103-
# test Series with different indices
102+
def test_compare_different_indices():
104103
msg = "Can only compare identically-labeled Series objects"
104+
ser1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
105+
ser2 = pd.Series([1, 2, 3], index=["a", "b", "d"])
105106
with pytest.raises(ValueError, match=msg):
106-
ser1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
107-
ser2 = pd.Series([1, 2, 3], index=["a", "b", "d"])
108107
ser1.compare(ser2)
109108

110-
# test Series with different lengths
109+
110+
def test_compare_different_lengths():
111111
msg = "Can only compare identically-labeled Series objects"
112+
ser1 = pd.Series([1, 2, 3])
113+
ser2 = pd.Series([1, 2, 3, 4])
112114
with pytest.raises(ValueError, match=msg):
113-
ser1 = pd.Series([1, 2, 3])
114-
ser2 = pd.Series([1, 2, 3, 4])
115115
ser1.compare(ser2)
116116

117117

pandas/tests/tools/test_to_datetime.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2254,12 +2254,12 @@ def test_dataframe_coerce(self, cache):
22542254
expected = Series([Timestamp("20150204 00:00:00"), NaT])
22552255
tm.assert_series_equal(result, expected)
22562256

2257-
def test_dataframe_extra_keys_raisesm(self, df, cache):
2257+
def test_dataframe_extra_keys_raises(self, df, cache):
22582258
# extra columns
22592259
msg = r"extra keys have been passed to the datetime assemblage: \[foo\]"
2260+
df2 = df.copy()
2261+
df2["foo"] = 1
22602262
with pytest.raises(ValueError, match=msg):
2261-
df2 = df.copy()
2262-
df2["foo"] = 1
22632263
to_datetime(df2, cache=cache)
22642264

22652265
@pytest.mark.parametrize(

pandas/tests/window/test_base_indexer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ def test_rolling_forward_window(
157157
indexer = FixedForwardWindowIndexer(window_size=3)
158158

159159
match = "Forward-looking windows can't have center=True"
160+
rolling = frame_or_series(values).rolling(window=indexer, center=True)
160161
with pytest.raises(ValueError, match=match):
161-
rolling = frame_or_series(values).rolling(window=indexer, center=True)
162162
getattr(rolling, func)()
163163

164164
match = "Forward-looking windows don't support setting the closed argument"
165+
rolling = frame_or_series(values).rolling(window=indexer, closed="right")
165166
with pytest.raises(ValueError, match=match):
166-
rolling = frame_or_series(values).rolling(window=indexer, closed="right")
167167
getattr(rolling, func)()
168168

169169
rolling = frame_or_series(values).rolling(window=indexer, min_periods=2, step=step)

0 commit comments

Comments
 (0)