Skip to content

Commit b8a399a

Browse files
mroeschkepmhatre1
authored andcommitted
STY: Enable ruff pytest checks (pandas-dev#56671)
* Enable PT001 * Add PT002 * Add PT003 * ignore 4 and 5 * Ignore 6, 7, 8 * Ignore PT009 * Enable PT010 * Enable PT011 * Ignore 12, enable 13 * Enable PT014 * Enforce PT015 * Enable 16 * Disable 17 * Ignore 18 * Ignore 19 * Add 20 * add 21 * Enable 22 * Add 23 * Add 24 * Add 25 * add 26 * Add 27 * use PT * fix some tests * Deduplcate test * Add tests * Added nested tuple case
1 parent b72f90b commit b8a399a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+78
-315
lines changed

.pre-commit-config.yaml

-7
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,6 @@ repos:
272272
language: python
273273
types: [rst]
274274
files: ^doc/source/(development|reference)/
275-
- id: unwanted-patterns-bare-pytest-raises
276-
name: Check for use of bare pytest raises
277-
language: python
278-
entry: python scripts/validate_unwanted_patterns.py --validation-type="bare_pytest_raises"
279-
types: [python]
280-
files: ^pandas/tests/
281-
exclude: ^pandas/tests/extension/
282275
- id: unwanted-patterns-private-function-across-module
283276
name: Check for use of private functions across modules
284277
language: python

pandas/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,6 @@ def warsaw(request) -> str:
19711971
return request.param
19721972

19731973

1974-
@pytest.fixture()
1974+
@pytest.fixture
19751975
def arrow_string_storage():
19761976
return ("pyarrow", "pyarrow_numpy")

pandas/tests/arithmetic/test_datetime64.py

-1
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,6 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array, unit):
13851385
"SemiMonthBegin",
13861386
"Week",
13871387
("Week", {"weekday": 3}),
1388-
"Week",
13891388
("Week", {"weekday": 6}),
13901389
"BusinessDay",
13911390
"BDay",

pandas/tests/arrays/categorical/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,12 @@ def test_set_categories(self):
291291
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
292292
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
293293
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
294-
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
294+
(["b", "a", "c"], ["a", "b"], ["b", "a"]),
295295
# Introduce NaNs
296296
(["a", "b", "c"], ["a", "b"], ["a"]),
297297
(["a", "b", "c"], ["a", "b"], ["b"]),
298298
(["b", "a", "c"], ["a", "b"], ["a"]),
299-
(["b", "a", "c"], ["a", "b"], ["a"]),
299+
(["b", "a", "c"], ["a", "b"], ["b"]),
300300
# No overlap
301301
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
302302
],

pandas/tests/arrays/categorical/test_dtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ def test_set_dtype_new_categories(self):
7373
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
7474
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
7575
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
76-
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
76+
(["b", "a", "c"], ["a", "b"], ["b", "a"]),
7777
# Introduce NaNs
7878
(["a", "b", "c"], ["a", "b"], ["a"]),
7979
(["a", "b", "c"], ["a", "b"], ["b"]),
8080
(["b", "a", "c"], ["a", "b"], ["a"]),
81-
(["b", "a", "c"], ["a", "b"], ["a"]),
81+
(["b", "a", "c"], ["a", "b"], ["b"]),
8282
# No overlap
8383
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
8484
],

pandas/tests/arrays/masked/test_function.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def data(request):
2121
return request.param
2222

2323

24-
@pytest.fixture()
24+
@pytest.fixture
2525
def numpy_dtype(data):
2626
"""
2727
Fixture returning numpy dtype from 'data' input array.

pandas/tests/arrays/sparse/test_arithmetics.py

-3
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,6 @@ def test_ufuncs(ufunc, arr):
433433
[
434434
(SparseArray([0, 0, 0]), np.array([0, 1, 2])),
435435
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
436-
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
437-
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
438-
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
439436
],
440437
)
441438
@pytest.mark.parametrize("ufunc", [np.add, np.greater])

pandas/tests/arrays/string_/test_string_arrow.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,22 @@ def test_setitem_invalid_indexer_raises():
220220

221221
arr = ArrowStringArray(pa.array(list("abcde")))
222222

223-
with pytest.raises(IndexError, match=None):
223+
with tm.external_error_raised(IndexError):
224224
arr[5] = "foo"
225225

226-
with pytest.raises(IndexError, match=None):
226+
with tm.external_error_raised(IndexError):
227227
arr[-6] = "foo"
228228

229-
with pytest.raises(IndexError, match=None):
229+
with tm.external_error_raised(IndexError):
230230
arr[[0, 5]] = "foo"
231231

232-
with pytest.raises(IndexError, match=None):
232+
with tm.external_error_raised(IndexError):
233233
arr[[0, -6]] = "foo"
234234

235-
with pytest.raises(IndexError, match=None):
235+
with tm.external_error_raised(IndexError):
236236
arr[[True, True, False]] = "foo"
237237

238-
with pytest.raises(ValueError, match=None):
238+
with tm.external_error_raised(ValueError):
239239
arr[[0, 1]] = ["foo", "bar", "baz"]
240240

241241

pandas/tests/arrays/test_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
)
3030

3131

32-
@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]", "M8[m]"])
32+
@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]"])
3333
def test_dt64_array(dtype_unit):
3434
# PR 53817
3535
dtype_var = np.dtype(dtype_unit)

pandas/tests/copy_view/test_internals.py

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def test_switch_options():
8383
([0, 1, 2], np.array([[-1, -2, -3], [-4, -5, -6], [-4, -5, -6]]).T),
8484
([1, 2], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
8585
([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
86-
([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
8786
],
8887
)
8988
def test_iset_splits_blocks_inplace(using_copy_on_write, locs, arr, dtype):

pandas/tests/dtypes/test_missing.py

-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ def test_array_equivalent_different_dtype_but_equal():
531531
(fix_now, fix_utcnow),
532532
(fix_now.to_datetime64(), fix_utcnow),
533533
(fix_now.to_pydatetime(), fix_utcnow),
534-
(fix_now, fix_utcnow),
535534
(fix_now.to_datetime64(), fix_utcnow.to_pydatetime()),
536535
(fix_now.to_pydatetime(), fix_utcnow.to_pydatetime()),
537536
],

pandas/tests/extension/base/dim2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def test_reshape(self, data):
9090
assert arr2d.shape == (data.size, 1)
9191
assert len(arr2d) == len(data)
9292

93-
with pytest.raises(ValueError):
93+
with tm.external_error_raised(ValueError):
9494
data.reshape((data.size, 2))
95-
with pytest.raises(ValueError):
95+
with tm.external_error_raised(ValueError):
9696
data.reshape(data.size, 2)
9797

9898
def test_getitem_2d(self, data):

pandas/tests/extension/base/getitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_take_non_na_fill_value(self, data_missing):
394394
tm.assert_extension_array_equal(result, expected)
395395

396396
def test_take_pandas_style_negative_raises(self, data, na_value):
397-
with pytest.raises(ValueError, match=""):
397+
with tm.external_error_raised(ValueError):
398398
data.take([0, -2], fill_value=na_value, allow_fill=True)
399399

400400
@pytest.mark.parametrize("allow_fill", [True, False])

pandas/tests/extension/base/setitem.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ def test_setitem_integer_array(self, data, idx, box_in_series):
209209
[0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948")
210210
),
211211
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
212-
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
212+
# TODO: change False to True?
213+
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False), # noqa: PT014
213214
],
214215
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
215216
)
@@ -332,7 +333,7 @@ def test_setitem_loc_iloc_slice(self, data):
332333

333334
def test_setitem_slice_mismatch_length_raises(self, data):
334335
arr = data[:5]
335-
with pytest.raises(ValueError):
336+
with tm.external_error_raised(ValueError):
336337
arr[:1] = arr[:2]
337338

338339
def test_setitem_slice_array(self, data):
@@ -342,7 +343,7 @@ def test_setitem_slice_array(self, data):
342343

343344
def test_setitem_scalar_key_sequence_raise(self, data):
344345
arr = data[:5].copy()
345-
with pytest.raises(ValueError):
346+
with tm.external_error_raised(ValueError):
346347
arr[0] = arr[[0, 1]]
347348

348349
def test_setitem_preserves_views(self, data):

pandas/tests/extension/json/test_json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_setitem_integer_array(self, data, idx, box_in_series, request):
355355
[0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948")
356356
),
357357
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
358-
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
358+
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), True),
359359
],
360360
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
361361
)

pandas/tests/extension/test_numpy.py

-13
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,6 @@ def test_setitem_mask(self, data, mask, box_in_series):
373373
def test_setitem_integer_array(self, data, idx, box_in_series):
374374
super().test_setitem_integer_array(data, idx, box_in_series)
375375

376-
@pytest.mark.parametrize(
377-
"idx, box_in_series",
378-
[
379-
([0, 1, 2, pd.NA], False),
380-
pytest.param([0, 1, 2, pd.NA], True, marks=pytest.mark.xfail),
381-
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
382-
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
383-
],
384-
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
385-
)
386-
def test_setitem_integer_with_missing_raises(self, data, idx, box_in_series):
387-
super().test_setitem_integer_with_missing_raises(data, idx, box_in_series)
388-
389376
@skip_nested
390377
def test_setitem_slice(self, data, box_in_series):
391378
super().test_setitem_slice(data, box_in_series)

pandas/tests/extension/test_sparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def gen(count):
7070
for _ in range(count):
7171
yield SparseArray(make_data(request.param), fill_value=request.param)
7272

73-
yield gen
73+
return gen
7474

7575

7676
@pytest.fixture(params=[0, np.nan])

pandas/tests/frame/methods/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ class IntegerArrayNoCopy(pd.core.arrays.IntegerArray):
867867
# GH 42501
868868

869869
def copy(self):
870-
assert False
870+
raise NotImplementedError
871871

872872

873873
class Int16DtypeNoCopy(pd.Int16Dtype):

pandas/tests/frame/methods/test_explode.py

-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ def test_error():
3838
[],
3939
"column must be nonempty",
4040
),
41-
(
42-
list("AC"),
43-
"columns must have matching element counts",
44-
),
4541
],
4642
)
4743
def test_error_multi_columns(input_subset, error_message):

pandas/tests/frame/methods/test_filter.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ def test_filter_regex_search(self, float_frame):
100100
@pytest.mark.parametrize(
101101
"name,expected",
102102
[
103-
("a", DataFrame({"a": [1, 2]})),
104103
("a", DataFrame({"a": [1, 2]})),
105104
("あ", DataFrame({"あ": [3, 4]})),
106105
],
@@ -112,9 +111,9 @@ def test_filter_unicode(self, name, expected):
112111
tm.assert_frame_equal(df.filter(like=name), expected)
113112
tm.assert_frame_equal(df.filter(regex=name), expected)
114113

115-
@pytest.mark.parametrize("name", ["a", "a"])
116-
def test_filter_bytestring(self, name):
114+
def test_filter_bytestring(self):
117115
# GH13101
116+
name = "a"
118117
df = DataFrame({b"a": [1, 2], b"b": [3, 4]})
119118
expected = DataFrame({b"a": [1, 2]})
120119

pandas/tests/frame/methods/test_reindex.py

-3
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,16 @@ def f(val):
452452
("mid",),
453453
("mid", "btm"),
454454
("mid", "btm", "top"),
455-
("mid",),
456455
("mid", "top"),
457456
("mid", "top", "btm"),
458457
("btm",),
459458
("btm", "mid"),
460459
("btm", "mid", "top"),
461-
("btm",),
462460
("btm", "top"),
463461
("btm", "top", "mid"),
464462
("top",),
465463
("top", "mid"),
466464
("top", "mid", "btm"),
467-
("top",),
468465
("top", "btm"),
469466
("top", "btm", "mid"),
470467
],

pandas/tests/frame/methods/test_reset_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import pandas._testing as tm
2828

2929

30-
@pytest.fixture()
30+
@pytest.fixture
3131
def multiindex_df():
3232
levels = [["A", ""], ["B", "b"]]
3333
return DataFrame([[0, 2], [1, 3]], columns=MultiIndex.from_tuples(levels))

pandas/tests/frame/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ def test_constructor_mixed_type_rows(self):
15551555
"tuples,lists",
15561556
[
15571557
((), []),
1558-
((()), []),
1558+
(((),), [[]]),
15591559
(((), ()), [(), ()]),
15601560
(((), ()), [[], []]),
15611561
(([], []), [[], []]),

pandas/tests/frame/test_query_eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ def df(self):
11871187
by backticks. The last two columns cannot be escaped by backticks
11881188
and should raise a ValueError.
11891189
"""
1190-
yield DataFrame(
1190+
return DataFrame(
11911191
{
11921192
"A": [1, 2, 3],
11931193
"B B": [3, 2, 1],

pandas/tests/frame/test_reductions.py

-1
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,6 @@ def test_df_empty_min_count_1(self, opname, dtype, exp_dtype):
18661866
[
18671867
("sum", "Int8", 0, ("Int32" if is_windows_np2_or_is32 else "Int64")),
18681868
("prod", "Int8", 1, ("Int32" if is_windows_np2_or_is32 else "Int64")),
1869-
("prod", "Int8", 1, ("Int32" if is_windows_np2_or_is32 else "Int64")),
18701869
("sum", "Int64", 0, "Int64"),
18711870
("prod", "Int64", 1, "Int64"),
18721871
("sum", "UInt8", 0, ("UInt32" if is_windows_np2_or_is32 else "UInt64")),

pandas/tests/groupby/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def three_group():
9292
)
9393

9494

95-
@pytest.fixture()
95+
@pytest.fixture
9696
def slice_test_df():
9797
data = [
9898
[0, "a", "a0_at_0"],
@@ -108,7 +108,7 @@ def slice_test_df():
108108
return df.set_index("Index")
109109

110110

111-
@pytest.fixture()
111+
@pytest.fixture
112112
def slice_test_grouped(slice_test_df):
113113
return slice_test_df.groupby("Group", as_index=False)
114114

pandas/tests/groupby/test_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def test_dataframe_categorical_ordered_observed_sort(ordered, observed, sort):
647647
f"for (ordered={ordered}, observed={observed}, sort={sort})\n"
648648
f"Result:\n{result}"
649649
)
650-
assert False, msg
650+
pytest.fail(msg)
651651

652652

653653
def test_datetime():

pandas/tests/groupby/test_groupby_dropna.py

-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ def test_groupby_drop_nan_with_multi_index():
410410
"UInt64",
411411
"Int64",
412412
"Float32",
413-
"Int64",
414413
"Float64",
415414
"category",
416415
"string",

pandas/tests/indexes/datetimelike_/test_sort_values.py

-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,6 @@ def test_sort_values_without_freq_timedeltaindex(self):
185185
@pytest.mark.parametrize(
186186
"index_dates,expected_dates",
187187
[
188-
(
189-
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
190-
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
191-
),
192188
(
193189
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
194190
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],

pandas/tests/indexes/multi/test_constructors.py

-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ def test_from_arrays_empty():
304304
(1, 2),
305305
([1], 2),
306306
(1, [2]),
307-
"a",
308307
("a",),
309308
("a", "b"),
310309
(["a"], "b"),

pandas/tests/internals/test_internals.py

-1
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,6 @@ def test_array_to_slice_conversion(self, arr, slc):
11581158
[-1],
11591159
[-1, -2, -3],
11601160
[-10],
1161-
[-1],
11621161
[-1, 0, 1, 2],
11631162
[-2, 0, 2, 4],
11641163
[1, 0, -1],

pandas/tests/io/formats/test_css.py

-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ def test_css_none_absent(style, equiv):
243243
("02.54cm", "72pt"),
244244
("25.4mm", "72pt"),
245245
("101.6q", "72pt"),
246-
("101.6q", "72pt"),
247246
],
248247
)
249248
@pytest.mark.parametrize("relative_to", [None, "16pt"]) # invariant to inherited size

0 commit comments

Comments
 (0)