Skip to content

Commit 7ef6052

Browse files
Use LSP parameter style for request
Replace `request` parameter with `*args, **kwargs` in many places. This allows us to avoid needlessly passing request parameters just to satisfy method signatures. Also remove whatsnew entry as this enhancement to test cases is not really user-visible. Signed-off-by: Michael Tiemann <72577720+MichaelTiemannOSC@users.noreply.github.com>
1 parent 7535374 commit 7ef6052

File tree

9 files changed

+76
-54
lines changed

9 files changed

+76
-54
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ Styler
351351

352352
Other
353353
^^^^^
354-
- Add complex128 to the types of numerical data we test across the test suite (:issue:`54761`)
355354
- Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`)
356355

357356
.. ***DO NOT USE THIS SECTION***

pandas/tests/extension/base/ops.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BaseOpsUtil:
1919
divmod_exc: type[Exception] | None = TypeError
2020

2121
def _get_expected_exception(
22-
self, op_name: str, obj, other, request
22+
self, op_name: str, obj, other, *args, **kwargs
2323
) -> type[Exception] | None:
2424
# Find the Exception, if any we expect to raise calling
2525
# obj.__op_name__(other)
@@ -54,8 +54,8 @@ def get_op_from_name(self, op_name: str):
5454
# case that still requires overriding _check_op or _combine, please let
5555
# us know at github.com/pandas-dev/pandas/issues
5656
@final
57-
def check_opname(self, ser: pd.Series, op_name: str, other, request):
58-
exc = self._get_expected_exception(op_name, ser, other, request)
57+
def check_opname(self, ser: pd.Series, op_name: str, other, *args, **kwargs):
58+
exc = self._get_expected_exception(op_name, ser, other, *args, **kwargs)
5959
op = self.get_op_from_name(op_name)
6060

6161
self._check_op(ser, op, other, op_name, exc)
@@ -91,12 +91,16 @@ def _check_op(
9191

9292
# see comment on check_opname
9393
@final
94-
def _check_divmod_op(self, ser: pd.Series, op, other, request):
94+
def _check_divmod_op(self, ser: pd.Series, op, other, *args, **kwargs):
9595
# check that divmod behavior matches behavior of floordiv+mod
9696
if op is divmod:
97-
exc = self._get_expected_exception("__divmod__", ser, other, request)
97+
exc = self._get_expected_exception(
98+
"__divmod__", ser, other, *args, **kwargs
99+
)
98100
else:
99-
exc = self._get_expected_exception("__rdivmod__", ser, other, request)
101+
exc = self._get_expected_exception(
102+
"__rdivmod__", ser, other, *args, **kwargs
103+
)
100104
if exc is None:
101105
result_div, result_mod = op(ser, other)
102106
if op is divmod:
@@ -128,53 +132,61 @@ class BaseArithmeticOpsTests(BaseOpsUtil):
128132
series_array_exc: type[Exception] | None = TypeError
129133
divmod_exc: type[Exception] | None = TypeError
130134

131-
def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request):
135+
def test_arith_series_with_scalar(
136+
self, data, all_arithmetic_operators, *args, **kwargs
137+
):
132138
# series & scalar
133139
if all_arithmetic_operators == "__rmod__" and is_string_dtype(data.dtype):
134140
pytest.skip("Skip testing Python string formatting")
135141

136142
op_name = all_arithmetic_operators
137143
ser = pd.Series(data)
138-
self.check_opname(ser, op_name, ser.iloc[0], request)
144+
self.check_opname(ser, op_name, ser.iloc[0], *args, **kwargs)
139145

140-
def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
146+
def test_arith_frame_with_scalar(
147+
self, data, all_arithmetic_operators, *args, **kwargs
148+
):
141149
# frame & scalar
142150
if all_arithmetic_operators == "__rmod__" and is_string_dtype(data.dtype):
143151
pytest.skip("Skip testing Python string formatting")
144152

145153
op_name = all_arithmetic_operators
146154
df = pd.DataFrame({"A": data})
147-
self.check_opname(df, op_name, data[0], request)
155+
self.check_opname(df, op_name, data[0], *args, **kwargs)
148156

149-
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
157+
def test_arith_series_with_array(
158+
self, data, all_arithmetic_operators, *args, **kwargs
159+
):
150160
# ndarray & other series
151161
op_name = all_arithmetic_operators
152162
ser = pd.Series(data)
153-
self.check_opname(ser, op_name, pd.Series([ser.iloc[0]] * len(ser)), request)
163+
self.check_opname(
164+
ser, op_name, pd.Series([ser.iloc[0]] * len(ser)), *args, **kwargs
165+
)
154166

155-
def test_divmod(self, data, request):
167+
def test_divmod(self, data, *args, **kwargs):
156168
ser = pd.Series(data)
157-
self._check_divmod_op(ser, divmod, 1, request)
158-
self._check_divmod_op(1, ops.rdivmod, ser, request)
169+
self._check_divmod_op(ser, divmod, 1, *args, **kwargs)
170+
self._check_divmod_op(1, ops.rdivmod, ser, *args, **kwargs)
159171

160-
def test_divmod_series_array(self, data, data_for_twos, request):
172+
def test_divmod_series_array(self, data, data_for_twos, *args, **kwargs):
161173
ser = pd.Series(data)
162-
self._check_divmod_op(ser, divmod, data, request)
174+
self._check_divmod_op(ser, divmod, data, *args, **kwargs)
163175

164176
other = data_for_twos
165-
self._check_divmod_op(other, ops.rdivmod, ser, request)
177+
self._check_divmod_op(other, ops.rdivmod, ser, *args, **kwargs)
166178

167179
other = pd.Series(other)
168-
self._check_divmod_op(other, ops.rdivmod, ser, request)
180+
self._check_divmod_op(other, ops.rdivmod, ser, *args, **kwargs)
169181

170-
def test_add_series_with_extension_array(self, data, request):
182+
def test_add_series_with_extension_array(self, data, *args, **kwargs):
171183
# Check adding an ExtensionArray to a Series of the same dtype matches
172184
# the behavior of adding the arrays directly and then wrapping in a
173185
# Series.
174186

175187
ser = pd.Series(data)
176188

177-
exc = self._get_expected_exception("__add__", ser, data, request)
189+
exc = self._get_expected_exception("__add__", ser, data, *args, **kwargs)
178190
if exc is not None:
179191
with pytest.raises(exc):
180192
ser + data

pandas/tests/extension/decimal/test_decimal.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def data_for_grouping():
6767

6868
class TestDecimalArray(base.ExtensionTests):
6969
def _get_expected_exception(
70-
self, op_name: str, obj, other, request
70+
self, op_name: str, obj, other, *args, **kwargs
7171
) -> type[Exception] | None:
7272
return None
7373

@@ -108,7 +108,7 @@ def test_compare_array(self, data, comparison_op):
108108
other = pd.Series(data) * [decimal.Decimal(pow(2.0, i)) for i in alter]
109109
self._compare_other(ser, data, comparison_op, other)
110110

111-
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
111+
def test_arith_series_with_array(self, data, all_arithmetic_operators):
112112
op_name = all_arithmetic_operators
113113
ser = pd.Series(data)
114114

@@ -120,13 +120,13 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
120120

121121
# Decimal supports ops with int, but not float
122122
other = pd.Series([int(d * 100) for d in data])
123-
self.check_opname(ser, op_name, other, request)
123+
self.check_opname(ser, op_name, other)
124124

125125
if "mod" not in op_name:
126-
self.check_opname(ser, op_name, ser * 2, request)
126+
self.check_opname(ser, op_name, ser * 2)
127127

128-
self.check_opname(ser, op_name, 0, request)
129-
self.check_opname(ser, op_name, 5, request)
128+
self.check_opname(ser, op_name, 0)
129+
self.check_opname(ser, op_name, 5)
130130
context.traps[decimal.DivisionByZero] = divbyzerotrap
131131
context.traps[decimal.InvalidOperation] = invalidoptrap
132132

@@ -330,11 +330,11 @@ class TestArithmeticOps(base.BaseArithmeticOpsTests):
330330
series_array_exc = None
331331

332332
def _get_expected_exception(
333-
self, op_name: str, obj, other, request
333+
self, op_name: str, obj, other
334334
) -> type[Exception] | None:
335335
return None
336336

337-
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
337+
def test_arith_series_with_array(self, data, all_arithmetic_operators):
338338
op_name = all_arithmetic_operators
339339
s = pd.Series(data)
340340

@@ -346,13 +346,13 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
346346

347347
# Decimal supports ops with int, but not float
348348
other = pd.Series([int(d * 100) for d in data])
349-
self.check_opname(s, op_name, other, request)
349+
self.check_opname(s, op_name, other)
350350

351351
if "mod" not in op_name:
352-
self.check_opname(s, op_name, s * 2, request)
352+
self.check_opname(s, op_name, s * 2)
353353

354-
self.check_opname(s, op_name, 0, request)
355-
self.check_opname(s, op_name, 5, request)
354+
self.check_opname(s, op_name, 0)
355+
self.check_opname(s, op_name, 5)
356356
context.traps[decimal.DivisionByZero] = divbyzerotrap
357357
context.traps[decimal.InvalidOperation] = invalidoptrap
358358

pandas/tests/extension/test_arrow.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def _is_temporal_supported(self, opname, pa_dtype):
938938
)
939939

940940
def _get_expected_exception(
941-
self, op_name: str, obj, other, request
941+
self, op_name: str, obj, other, *args, **kwargs
942942
) -> type[Exception] | None:
943943
if op_name in ("__divmod__", "__rdivmod__"):
944944
return self.divmod_exc
@@ -1048,7 +1048,9 @@ def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request)
10481048
if mark is not None:
10491049
request.node.add_marker(mark)
10501050

1051-
super().test_arith_series_with_scalar(data, all_arithmetic_operators, request)
1051+
super().test_arith_series_with_scalar(
1052+
data, all_arithmetic_operators, request=request
1053+
)
10521054

10531055
def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
10541056
pa_dtype = data.dtype.pyarrow_dtype
@@ -1063,7 +1065,7 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
10631065
request.node.add_marker(mark)
10641066

10651067
super().test_arith_frame_with_scalar(
1066-
data, all_arithmetic_operators, request
1068+
data, all_arithmetic_operators, request=request
10671069
)
10681070

10691071
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
@@ -1098,7 +1100,7 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
10981100
# since ser.iloc[0] is a python scalar
10991101
other = pd.Series(pd.array([ser.iloc[0]] * len(ser), dtype=data.dtype))
11001102

1101-
self.check_opname(ser, op_name, other, request)
1103+
self.check_opname(ser, op_name, other, request=request)
11021104

11031105
def test_add_series_with_extension_array(self, data, request):
11041106
pa_dtype = data.dtype.pyarrow_dtype
@@ -1110,7 +1112,7 @@ def test_add_series_with_extension_array(self, data, request):
11101112
reason=f"raises on overflow for {pa_dtype}",
11111113
)
11121114
)
1113-
super().test_add_series_with_extension_array(data, request)
1115+
super().test_add_series_with_extension_array(data, request=request)
11141116

11151117
def test_invalid_other_comp(self, data, comparison_op):
11161118
# GH 48833

pandas/tests/extension/test_categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
153153
reason="rmod never called when string is first argument"
154154
)
155155
)
156-
super().test_arith_frame_with_scalar(data, op_name, request)
156+
super().test_arith_frame_with_scalar(data, op_name)
157157

158158
def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request):
159159
op_name = all_arithmetic_operators
@@ -163,7 +163,7 @@ def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request)
163163
reason="rmod never called when string is first argument"
164164
)
165165
)
166-
super().test_arith_series_with_scalar(data, op_name, request)
166+
super().test_arith_series_with_scalar(data, op_name)
167167

168168
def _compare_other(self, ser: pd.Series, data, op, other):
169169
op_name = f"__{op.__name__}__"

pandas/tests/extension/test_datetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ def cmp(a, b):
8383

8484
# ----------------------------------------------------------------------------
8585
class TestDatetimeArray(base.ExtensionTests):
86-
def _get_expected_exception(self, op_name, obj, other, request):
86+
def _get_expected_exception(self, op_name, obj, other, *args, **kwargs):
8787
if op_name in ["__sub__", "__rsub__"]:
8888
return None
89-
return super()._get_expected_exception(op_name, obj, other, request)
89+
return super()._get_expected_exception(op_name, obj, other, *args, **kwargs)
9090

9191
def _supports_accumulation(self, ser, op_name: str) -> bool:
9292
return op_name in ["cummin", "cummax"]

pandas/tests/extension/test_masked.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def data_for_grouping(dtype):
162162

163163

164164
class TestMaskedArrays(base.ExtensionTests):
165-
def _get_expected_exception(self, op_name, obj, other, request):
165+
def _get_expected_exception(self, op_name, obj, other, *args, **kwargs):
166166
try:
167167
dtype = tm.get_dtype(obj)
168168
except AttributeError:
@@ -235,7 +235,7 @@ def test_divmod_series_array(self, data, data_for_twos, request):
235235
"non-masked bool dtype."
236236
)
237237
request.node.add_marker(mark)
238-
super().test_divmod_series_array(data, data_for_twos, request)
238+
super().test_divmod_series_array(data, data_for_twos)
239239

240240
def test_combine_le(self, data_repeated):
241241
# TODO: patching self is a bad pattern here

pandas/tests/extension/test_numpy.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class TestArithmetics(BaseNumPyTests, base.BaseArithmeticOpsTests):
283283
series_array_exc = None
284284

285285
def _get_expected_exception(
286-
self, op_name: str, obj, other, request
286+
self, op_name: str, obj, other, *args, **kwargs
287287
) -> type[Exception] | None:
288288
# Find the Exception, if any we expect to raise calling
289289
# obj.__op_name__(other)
@@ -311,34 +311,43 @@ def _get_expected_exception(
311311
marked_reason = f"{dtype.name} dtype"
312312
break
313313
if marked_reason:
314-
request.node.add_marker(
314+
kwargs["request"].node.add_marker(
315315
pytest.mark.xfail(
316316
raises=TypeError,
317317
reason=f"{marked_reason} does not support {op_name}",
318318
strict=False,
319319
)
320320
)
321321
return TypeError
322-
return super()._get_expected_exception(op_name, obj, other, request)
322+
return super()._get_expected_exception(op_name, obj, other)
323323

324324
@skip_nested
325325
def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request):
326-
super().test_arith_series_with_scalar(data, all_arithmetic_operators, request)
326+
super().test_arith_series_with_scalar(
327+
data, all_arithmetic_operators, request=request
328+
)
327329

328330
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
329331
opname = all_arithmetic_operators
330332
if data.dtype.numpy_dtype == object and opname not in ["__add__", "__radd__"]:
331333
mark = pytest.mark.xfail(reason="Fails for object dtype")
332334
request.node.add_marker(mark)
333-
super().test_arith_series_with_array(data, all_arithmetic_operators, request)
335+
super().test_arith_series_with_array(
336+
data, all_arithmetic_operators, request=request
337+
)
334338

335339
@skip_nested
336340
def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
337-
super().test_arith_frame_with_scalar(data, all_arithmetic_operators, request)
341+
super().test_arith_frame_with_scalar(
342+
data, all_arithmetic_operators, request=request
343+
)
338344

339345
@skip_nested
340346
def test_divmod(self, data, request):
341-
super().test_divmod(data, request)
347+
super().test_divmod(data, request=request)
348+
349+
def test_divmod_series_array(self, data, data_for_twos, request):
350+
super().test_divmod_series_array(data, data_for_twos, request=request)
342351

343352

344353
class TestPrinting(BaseNumPyTests, base.BasePrintingTests):

pandas/tests/extension/test_period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def data_for_grouping(dtype):
7272

7373

7474
class TestPeriodArray(base.ExtensionTests):
75-
def _get_expected_exception(self, op_name, obj, other, request):
75+
def _get_expected_exception(self, op_name, obj, other, *args, **kwargs):
7676
if op_name in ("__sub__", "__rsub__"):
7777
return None
78-
return super()._get_expected_exception(op_name, obj, other, request)
78+
return super()._get_expected_exception(op_name, obj, other, *args, **kwargs)
7979

8080
def _supports_accumulation(self, ser, op_name: str) -> bool:
8181
return op_name in ["cummin", "cummax"]

0 commit comments

Comments
 (0)