Skip to content

Commit e865ec7

Browse files
TST/CLN: tighten some xfails (pandas-dev#41469)
1 parent 6d59ae9 commit e865ec7

File tree

10 files changed

+66
-57
lines changed

10 files changed

+66
-57
lines changed

doc/source/development/code_style.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ xfail during the testing phase. To do so, use the ``request`` fixture:
5757
import pytest
5858
5959
def test_xfail(request):
60-
request.node.add_marker(pytest.mark.xfail(reason="Indicate why here"))
60+
mark = pytest.mark.xfail(raises=TypeError, reason="Indicate why here")
61+
request.node.add_marker(mark)
6162
6263
xfail is not to be used for tests involving failure due to invalid user arguments.
6364
For these tests, we need to verify the correct exception type and error message

pandas/tests/apply/test_frame_apply.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
164164
if len(args) > 1 and how == "agg":
165165
request.node.add_marker(
166166
pytest.mark.xfail(
167+
raises=TypeError,
167168
reason="agg/apply signature mismatch - agg passes 2nd "
168-
"argument to func"
169+
"argument to func",
169170
)
170171
)
171172
result = getattr(float_frame, how)(func, *args, **kwds)

pandas/tests/apply/test_frame_transform.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def test_transform_bad_dtype(op, frame_or_series, request):
173173
# GH 35964
174174
if op == "rank":
175175
request.node.add_marker(
176-
pytest.mark.xfail(reason="GH 40418: rank does not raise a TypeError")
176+
pytest.mark.xfail(
177+
raises=ValueError, reason="GH 40418: rank does not raise a TypeError"
178+
)
177179
)
178180

179181
obj = DataFrame({"A": 3 * [object]}) # DataFrame that will fail on most transforms

pandas/tests/apply/test_series_apply.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ def test_transform_partial_failure(op, request):
262262
# GH 35964
263263
if op in ("ffill", "bfill", "pad", "backfill", "shift"):
264264
request.node.add_marker(
265-
pytest.mark.xfail(reason=f"{op} is successful on any dtype")
265+
pytest.mark.xfail(
266+
raises=AssertionError, reason=f"{op} is successful on any dtype"
267+
)
266268
)
267269
if op in ("rank", "fillna"):
268270
pytest.skip(f"{op} doesn't raise TypeError on object")

pandas/tests/arithmetic/test_interval.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ def test_compare_scalar_na(self, op, interval_array, nulls_fixture, request):
135135

136136
if nulls_fixture is pd.NA and interval_array.dtype.subtype != "int64":
137137
mark = pytest.mark.xfail(
138-
reason="broken for non-integer IntervalArray; see GH 31882"
138+
raises=AssertionError,
139+
reason="broken for non-integer IntervalArray; see GH 31882",
139140
)
140141
request.node.add_marker(mark)
141142

@@ -220,7 +221,7 @@ def test_compare_list_like_nan(self, op, interval_array, nulls_fixture, request)
220221

221222
if nulls_fixture is pd.NA and interval_array.dtype.subtype != "i8":
222223
reason = "broken for non-integer IntervalArray; see GH 31882"
223-
mark = pytest.mark.xfail(reason=reason)
224+
mark = pytest.mark.xfail(raises=AssertionError, reason=reason)
224225
request.node.add_marker(mark)
225226

226227
tm.assert_numpy_array_equal(result, expected)

pandas/tests/arrays/masked/test_arithmetic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ def test_unary_op_does_not_propagate_mask(data, op, request):
170170
data, _ = data
171171
if data.dtype in ["Float32", "Float64"] and op == "__invert__":
172172
request.node.add_marker(
173-
pytest.mark.xfail(reason="invert is not implemented for float ea dtypes")
173+
pytest.mark.xfail(
174+
raises=TypeError, reason="invert is not implemented for float ea dtypes"
175+
)
174176
)
175177
s = pd.Series(data)
176178
result = getattr(s, op)()

pandas/tests/arrays/masked/test_arrow_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_from_arrow_type_error(request, data):
173173
# TODO numeric dtypes cast any incoming array to the correct dtype
174174
# instead of erroring
175175
request.node.add_marker(
176-
pytest.mark.xfail(reason="numeric dtypes don't error but cast")
176+
pytest.mark.xfail(raises=None, reason="numeric dtypes don't error but cast")
177177
)
178178

179179
arr = pa.array(data).cast("string")

pandas/tests/arrays/sparse/test_arithmetics.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_float_scalar(
132132
elif op is ops.rfloordiv and scalar == 0:
133133
pass
134134
else:
135-
mark = pytest.mark.xfail(reason="GH#38172")
135+
mark = pytest.mark.xfail(raises=AssertionError, reason="GH#38172")
136136
request.node.add_marker(mark)
137137

138138
values = self._base([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
@@ -177,11 +177,13 @@ def test_float_same_index_with_nans(
177177
# when sp_index are the same
178178
op = all_arithmetic_functions
179179

180-
if not np_version_under1p20:
181-
if op is ops.rfloordiv:
182-
if not (mix and kind == "block"):
183-
mark = pytest.mark.xfail(reason="GH#38172")
184-
request.node.add_marker(mark)
180+
if (
181+
not np_version_under1p20
182+
and op is ops.rfloordiv
183+
and not (mix and kind == "block")
184+
):
185+
mark = pytest.mark.xfail(raises=AssertionError, reason="GH#38172")
186+
request.node.add_marker(mark)
185187

186188
values = self._base([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
187189
rvalues = self._base([np.nan, 2, 3, 4, np.nan, 0, 1, 3, 2, np.nan])
@@ -358,10 +360,13 @@ def test_bool_array_logical(self, kind, fill_value):
358360
def test_mixed_array_float_int(self, kind, mix, all_arithmetic_functions, request):
359361
op = all_arithmetic_functions
360362

361-
if not np_version_under1p20:
362-
if op in [operator.floordiv, ops.rfloordiv] and mix:
363-
mark = pytest.mark.xfail(reason="GH#38172")
364-
request.node.add_marker(mark)
363+
if (
364+
not np_version_under1p20
365+
and op in [operator.floordiv, ops.rfloordiv]
366+
and mix
367+
):
368+
mark = pytest.mark.xfail(raises=AssertionError, reason="GH#38172")
369+
request.node.add_marker(mark)
365370

366371
rdtype = "int64"
367372

pandas/tests/arrays/string_/test_string.py

+28-37
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ def test_astype_roundtrip(dtype, request):
115115
def test_add(dtype, request):
116116
if dtype == "arrow_string":
117117
reason = (
118-
"TypeError: unsupported operand type(s) for +: 'ArrowStringArray' and "
118+
"unsupported operand type(s) for +: 'ArrowStringArray' and "
119119
"'ArrowStringArray'"
120120
)
121-
mark = pytest.mark.xfail(reason=reason)
121+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
122122
request.node.add_marker(mark)
123123

124124
a = pd.Series(["a", "b", "c", None, None], dtype=dtype)
@@ -143,7 +143,7 @@ def test_add(dtype, request):
143143
def test_add_2d(dtype, request):
144144
if dtype == "arrow_string":
145145
reason = "Failed: DID NOT RAISE <class 'ValueError'>"
146-
mark = pytest.mark.xfail(reason=reason)
146+
mark = pytest.mark.xfail(raises=None, reason=reason)
147147
request.node.add_marker(mark)
148148

149149
a = pd.array(["a", "b", "c"], dtype=dtype)
@@ -158,11 +158,8 @@ def test_add_2d(dtype, request):
158158

159159
def test_add_sequence(dtype, request):
160160
if dtype == "arrow_string":
161-
reason = (
162-
"TypeError: unsupported operand type(s) for +: 'ArrowStringArray' "
163-
"and 'list'"
164-
)
165-
mark = pytest.mark.xfail(reason=reason)
161+
reason = "unsupported operand type(s) for +: 'ArrowStringArray' and 'list'"
162+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
166163
request.node.add_marker(mark)
167164

168165
a = pd.array(["a", "b", None, None], dtype=dtype)
@@ -179,10 +176,8 @@ def test_add_sequence(dtype, request):
179176

180177
def test_mul(dtype, request):
181178
if dtype == "arrow_string":
182-
reason = (
183-
"TypeError: unsupported operand type(s) for *: 'ArrowStringArray' and 'int'"
184-
)
185-
mark = pytest.mark.xfail(reason=reason)
179+
reason = "unsupported operand type(s) for *: 'ArrowStringArray' and 'int'"
180+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
186181
request.node.add_marker(mark)
187182

188183
a = pd.array(["a", "b", None], dtype=dtype)
@@ -246,7 +241,7 @@ def test_comparison_methods_scalar_pd_na(all_compare_operators, dtype):
246241
def test_comparison_methods_scalar_not_string(all_compare_operators, dtype, request):
247242
if all_compare_operators not in ["__eq__", "__ne__"]:
248243
reason = "comparison op not supported between instances of 'str' and 'int'"
249-
mark = pytest.mark.xfail(reason=reason)
244+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
250245
request.node.add_marker(mark)
251246

252247
op_name = all_compare_operators
@@ -262,11 +257,9 @@ def test_comparison_methods_scalar_not_string(all_compare_operators, dtype, requ
262257

263258
def test_comparison_methods_array(all_compare_operators, dtype, request):
264259
if dtype == "arrow_string":
265-
if all_compare_operators in ["__eq__", "__ne__"]:
266-
reason = "NotImplementedError: Neither scalar nor ArrowStringArray"
267-
else:
268-
reason = "AssertionError: left is not an ExtensionArray"
269-
mark = pytest.mark.xfail(reason=reason)
260+
mark = pytest.mark.xfail(
261+
raises=AssertionError, reason="left is not an ExtensionArray"
262+
)
270263
request.node.add_marker(mark)
271264

272265
op_name = all_compare_operators
@@ -309,8 +302,9 @@ def test_constructor_raises(cls):
309302
@pytest.mark.parametrize("copy", [True, False])
310303
def test_from_sequence_no_mutate(copy, cls, request):
311304
if cls is ArrowStringArray and copy is False:
312-
reason = "AssertionError: numpy array are different"
313-
mark = pytest.mark.xfail(reason=reason)
305+
mark = pytest.mark.xfail(
306+
raises=AssertionError, reason="numpy array are different"
307+
)
314308
request.node.add_marker(mark)
315309

316310
nan_arr = np.array(["a", np.nan], dtype=object)
@@ -333,8 +327,8 @@ def test_from_sequence_no_mutate(copy, cls, request):
333327

334328
def test_astype_int(dtype, request):
335329
if dtype == "arrow_string":
336-
reason = "TypeError: Cannot interpret 'Int64Dtype()' as a data type"
337-
mark = pytest.mark.xfail(reason=reason)
330+
reason = "Cannot interpret 'Int64Dtype()' as a data type"
331+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
338332
request.node.add_marker(mark)
339333

340334
arr = pd.array(["1", pd.NA, "3"], dtype=dtype)
@@ -349,12 +343,10 @@ def test_astype_float(dtype, any_float_allowed_nullable_dtype, request):
349343

350344
if dtype == "arrow_string":
351345
if any_float_allowed_nullable_dtype in {"Float32", "Float64"}:
352-
reason = "TypeError: Cannot interpret 'Float32Dtype()' as a data type"
346+
reason = "Cannot interpret 'Float32Dtype()' as a data type"
353347
else:
354-
reason = (
355-
"TypeError: float() argument must be a string or a number, not 'NAType'"
356-
)
357-
mark = pytest.mark.xfail(reason=reason)
348+
reason = "float() argument must be a string or a number, not 'NAType'"
349+
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
358350
request.node.add_marker(mark)
359351

360352
ser = pd.Series(["1.1", pd.NA, "3.3"], dtype=dtype)
@@ -376,8 +368,8 @@ def test_reduce(skipna, dtype):
376368
@pytest.mark.parametrize("skipna", [True, False])
377369
def test_min_max(method, skipna, dtype, request):
378370
if dtype == "arrow_string":
379-
reason = "AttributeError: 'ArrowStringArray' object has no attribute 'max'"
380-
mark = pytest.mark.xfail(reason=reason)
371+
reason = "'ArrowStringArray' object has no attribute 'max'"
372+
mark = pytest.mark.xfail(raises=AttributeError, reason=reason)
381373
request.node.add_marker(mark)
382374

383375
arr = pd.Series(["a", "b", "c", None], dtype=dtype)
@@ -394,13 +386,12 @@ def test_min_max(method, skipna, dtype, request):
394386
def test_min_max_numpy(method, box, dtype, request):
395387
if dtype == "arrow_string":
396388
if box is pd.array:
397-
reason = (
398-
"TypeError: '<=' not supported between instances of 'str' and "
399-
"'NoneType'"
400-
)
389+
raises = TypeError
390+
reason = "'<=' not supported between instances of 'str' and 'NoneType'"
401391
else:
402-
reason = "AttributeError: 'ArrowStringArray' object has no attribute 'max'"
403-
mark = pytest.mark.xfail(reason=reason)
392+
raises = AttributeError
393+
reason = "'ArrowStringArray' object has no attribute 'max'"
394+
mark = pytest.mark.xfail(raises=raises, reason=reason)
404395
request.node.add_marker(mark)
405396

406397
arr = box(["a", "b", "c", None], dtype=dtype)
@@ -425,10 +416,10 @@ def test_fillna_args(dtype, request):
425416

426417
if dtype == "arrow_string":
427418
reason = (
428-
"AssertionError: Regex pattern \"Cannot set non-string value '1' into "
419+
"Regex pattern \"Cannot set non-string value '1' into "
429420
"a StringArray.\" does not match 'Scalar must be NA or str'"
430421
)
431-
mark = pytest.mark.xfail(reason=reason)
422+
mark = pytest.mark.xfail(raises=AssertionError, reason=reason)
432423
request.node.add_marker(mark)
433424

434425
arr = pd.array(["a", pd.NA], dtype=dtype)

pandas/tests/arrays/test_datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ def test_searchsorted_castable_strings(self, arr1d, box, request):
306306
# If we have e.g. tzutc(), when we cast to string and parse
307307
# back we get pytz.UTC, and then consider them different timezones
308308
# so incorrectly raise.
309-
mark = pytest.mark.xfail(reason="timezone comparisons inconsistent")
309+
mark = pytest.mark.xfail(
310+
raises=TypeError, reason="timezone comparisons inconsistent"
311+
)
310312
request.node.add_marker(mark)
311313

312314
arr = arr1d
@@ -471,7 +473,9 @@ def test_setitem_strs(self, arr1d, request):
471473
# If we have e.g. tzutc(), when we cast to string and parse
472474
# back we get pytz.UTC, and then consider them different timezones
473475
# so incorrectly raise.
474-
mark = pytest.mark.xfail(reason="timezone comparisons inconsistent")
476+
mark = pytest.mark.xfail(
477+
raises=TypeError, reason="timezone comparisons inconsistent"
478+
)
475479
request.node.add_marker(mark)
476480

477481
# Setting list-like of strs

0 commit comments

Comments
 (0)