Skip to content

STY: Enable ruff pytest checks #56671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e672f7b
Enable PT001
mroeschke Dec 28, 2023
6ce08db
Add PT002
mroeschke Dec 28, 2023
fc2e0d2
Add PT003
mroeschke Dec 28, 2023
d225564
ignore 4 and 5
mroeschke Dec 28, 2023
8fe6476
Ignore 6, 7, 8
mroeschke Dec 28, 2023
d99f392
Ignore PT009
mroeschke Dec 28, 2023
8d27e34
Enable PT010
mroeschke Dec 28, 2023
d7379bd
Enable PT011
mroeschke Dec 28, 2023
0cdc13a
Ignore 12, enable 13
mroeschke Dec 28, 2023
78692da
Enable PT014
mroeschke Dec 28, 2023
893cf5b
Enforce PT015
mroeschke Dec 28, 2023
5702122
Enable 16
mroeschke Dec 28, 2023
47ce700
Disable 17
mroeschke Dec 28, 2023
a2ebb71
Ignore 18
mroeschke Dec 28, 2023
3ea2e08
Ignore 19
mroeschke Dec 28, 2023
022135b
Add 20
mroeschke Dec 28, 2023
ecf0592
add 21
mroeschke Dec 28, 2023
dd879a6
Enable 22
mroeschke Dec 28, 2023
cfaa72a
Add 23
mroeschke Dec 28, 2023
01adb5a
Add 24
mroeschke Dec 28, 2023
477d2d2
Add 25
mroeschke Dec 28, 2023
7458b8b
add 26
mroeschke Dec 28, 2023
d520b13
Add 27
mroeschke Dec 28, 2023
31c2ce3
use PT
mroeschke Dec 28, 2023
54f6bce
Merge remote-tracking branch 'upstream/main' into sty/ruff/pytest
mroeschke Dec 29, 2023
0511ebe
fix some tests
mroeschke Dec 29, 2023
01472dc
Deduplcate test
mroeschke Dec 29, 2023
2dc60a9
Merge remote-tracking branch 'upstream/main' into sty/ruff/pytest
mroeschke Jan 2, 2024
f3be5ab
Add tests
mroeschke Jan 2, 2024
9c6d650
Merge remote-tracking branch 'upstream/main' into sty/ruff/pytest
mroeschke Jan 3, 2024
44e4729
Added nested tuple case
mroeschke Jan 4, 2024
547ece5
Merge remote-tracking branch 'upstream/main' into sty/ruff/pytest
mroeschke Jan 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,6 @@ repos:
language: python
types: [rst]
files: ^doc/source/(development|reference)/
- id: unwanted-patterns-bare-pytest-raises
name: Check for use of bare pytest raises
language: python
entry: python scripts/validate_unwanted_patterns.py --validation-type="bare_pytest_raises"
types: [python]
files: ^pandas/tests/
exclude: ^pandas/tests/extension/
- id: unwanted-patterns-private-function-across-module
name: Check for use of private functions across modules
language: python
Expand Down
2 changes: 1 addition & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,6 @@ def warsaw(request) -> str:
return request.param


@pytest.fixture()
@pytest.fixture
def arrow_string_storage():
return ("pyarrow", "pyarrow_numpy")
1 change: 0 additions & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,6 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array, unit):
"SemiMonthBegin",
"Week",
("Week", {"weekday": 3}),
"Week",
("Week", {"weekday": 6}),
"BusinessDay",
"BDay",
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ def test_set_categories(self):
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
(["b", "a", "c"], ["a", "b"], ["b", "a"]),
# Introduce NaNs
(["a", "b", "c"], ["a", "b"], ["a"]),
(["a", "b", "c"], ["a", "b"], ["b"]),
(["b", "a", "c"], ["a", "b"], ["a"]),
(["b", "a", "c"], ["a", "b"], ["a"]),
(["b", "a", "c"], ["a", "b"], ["b"]),
# No overlap
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
],
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def test_set_dtype_new_categories(self):
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this end with ["b", "a"]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed

(["b", "a", "c"], ["a", "b"], ["b", "a"]),
# Introduce NaNs
(["a", "b", "c"], ["a", "b"], ["a"]),
(["a", "b", "c"], ["a", "b"], ["b"]),
(["b", "a", "c"], ["a", "b"], ["a"]),
(["b", "a", "c"], ["a", "b"], ["a"]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "b"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed

(["b", "a", "c"], ["a", "b"], ["b"]),
# No overlap
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
],
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/masked/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def data(request):
return request.param


@pytest.fixture()
@pytest.fixture
def numpy_dtype(data):
"""
Fixture returning numpy dtype from 'data' input array.
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/arrays/sparse/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,6 @@ def test_ufuncs(ufunc, arr):
[
(SparseArray([0, 0, 0]), np.array([0, 1, 2])),
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
],
)
@pytest.mark.parametrize("ufunc", [np.add, np.greater])
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/arrays/string_/test_string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,22 @@ def test_setitem_invalid_indexer_raises():

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

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

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

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

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

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

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


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)


@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]", "M8[m]"])
@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]"])
def test_dt64_array(dtype_unit):
# PR 53817
dtype_var = np.dtype(dtype_unit)
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/copy_view/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def test_switch_options():
([0, 1, 2], np.array([[-1, -2, -3], [-4, -5, -6], [-4, -5, -6]]).T),
([1, 2], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
([1, 3], np.array([[-1, -2, -3], [-4, -5, -6]]).T),
],
)
def test_iset_splits_blocks_inplace(using_copy_on_write, locs, arr, dtype):
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ def test_array_equivalent_different_dtype_but_equal():
(fix_now, fix_utcnow),
(fix_now.to_datetime64(), fix_utcnow),
(fix_now.to_pydatetime(), fix_utcnow),
(fix_now, fix_utcnow),
(fix_now.to_datetime64(), fix_utcnow.to_pydatetime()),
(fix_now.to_pydatetime(), fix_utcnow.to_pydatetime()),
],
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def test_reshape(self, data):
assert arr2d.shape == (data.size, 1)
assert len(arr2d) == len(data)

with pytest.raises(ValueError):
with tm.external_error_raised(ValueError):
data.reshape((data.size, 2))
with pytest.raises(ValueError):
with tm.external_error_raised(ValueError):
data.reshape(data.size, 2)

def test_getitem_2d(self, data):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_take_non_na_fill_value(self, data_missing):
tm.assert_extension_array_equal(result, expected)

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

@pytest.mark.parametrize("allow_fill", [True, False])
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def test_setitem_integer_array(self, data, idx, box_in_series):
[0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948")
),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
# TODO: change False to True?
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False), # noqa: PT014
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this is a dupe right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup but changing it to True fails a few tests so adding a noqa for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issue as a followup?

(the noqa makes it look like ruff had a false positive)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, #56727

],
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
)
Expand Down Expand Up @@ -332,7 +333,7 @@ def test_setitem_loc_iloc_slice(self, data):

def test_setitem_slice_mismatch_length_raises(self, data):
arr = data[:5]
with pytest.raises(ValueError):
with tm.external_error_raised(ValueError):
arr[:1] = arr[:2]

def test_setitem_slice_array(self, data):
Expand All @@ -342,7 +343,7 @@ def test_setitem_slice_array(self, data):

def test_setitem_scalar_key_sequence_raise(self, data):
arr = data[:5].copy()
with pytest.raises(ValueError):
with tm.external_error_raised(ValueError):
arr[0] = arr[[0, 1]]

def test_setitem_preserves_views(self, data):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_setitem_integer_array(self, data, idx, box_in_series, request):
[0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948")
),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), True),
],
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
)
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/extension/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,6 @@ def test_setitem_mask(self, data, mask, box_in_series):
def test_setitem_integer_array(self, data, idx, box_in_series):
super().test_setitem_integer_array(data, idx, box_in_series)

@pytest.mark.parametrize(
"idx, box_in_series",
[
([0, 1, 2, pd.NA], False),
pytest.param([0, 1, 2, pd.NA], True, marks=pytest.mark.xfail),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
],
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
)
def test_setitem_integer_with_missing_raises(self, data, idx, box_in_series):
super().test_setitem_integer_with_missing_raises(data, idx, box_in_series)

@skip_nested
def test_setitem_slice(self, data, box_in_series):
super().test_setitem_slice(data, box_in_series)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def gen(count):
for _ in range(count):
yield SparseArray(make_data(request.param), fill_value=request.param)

yield gen
return gen


@pytest.fixture(params=[0, np.nan])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ class IntegerArrayNoCopy(pd.core.arrays.IntegerArray):
# GH 42501

def copy(self):
assert False
raise NotImplementedError


class Int16DtypeNoCopy(pd.Int16Dtype):
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/frame/methods/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ def test_error():
[],
"column must be nonempty",
),
(
list("AC"),
"columns must have matching element counts",
),
],
)
def test_error_multi_columns(input_subset, error_message):
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/methods/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def test_filter_regex_search(self, float_frame):
@pytest.mark.parametrize(
"name,expected",
[
("a", DataFrame({"a": [1, 2]})),
("a", DataFrame({"a": [1, 2]})),
("あ", DataFrame({"あ": [3, 4]})),
],
Expand All @@ -112,9 +111,9 @@ def test_filter_unicode(self, name, expected):
tm.assert_frame_equal(df.filter(like=name), expected)
tm.assert_frame_equal(df.filter(regex=name), expected)

@pytest.mark.parametrize("name", ["a", "a"])
def test_filter_bytestring(self, name):
def test_filter_bytestring(self):
# GH13101
name = "a"
df = DataFrame({b"a": [1, 2], b"b": [3, 4]})
expected = DataFrame({b"a": [1, 2]})

Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,16 @@ def f(val):
("mid",),
("mid", "btm"),
("mid", "btm", "top"),
("mid",),
("mid", "top"),
("mid", "top", "btm"),
("btm",),
("btm", "mid"),
("btm", "mid", "top"),
("btm",),
("btm", "top"),
("btm", "top", "mid"),
("top",),
("top", "mid"),
("top", "mid", "btm"),
("top",),
("top", "btm"),
("top", "btm", "mid"),
],
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import pandas._testing as tm


@pytest.fixture()
@pytest.fixture
def multiindex_df():
levels = [["A", ""], ["B", "b"]]
return DataFrame([[0, 2], [1, 3]], columns=MultiIndex.from_tuples(levels))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def test_constructor_mixed_type_rows(self):
"tuples,lists",
[
((), []),
((()), []),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, was this supposed to contain an inner tuple like
((),)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah possibly yes. That passes here so added it

(((),), [[]]),
(((), ()), [(), ()]),
(((), ()), [[], []]),
(([], []), [[], []]),
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ def df(self):
by backticks. The last two columns cannot be escaped by backticks
and should raise a ValueError.
"""
yield DataFrame(
return DataFrame(
{
"A": [1, 2, 3],
"B B": [3, 2, 1],
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,6 @@ def test_df_empty_min_count_1(self, opname, dtype, exp_dtype):
[
("sum", "Int8", 0, ("Int32" if is_windows_np2_or_is32 else "Int64")),
("prod", "Int8", 1, ("Int32" if is_windows_np2_or_is32 else "Int64")),
("prod", "Int8", 1, ("Int32" if is_windows_np2_or_is32 else "Int64")),
("sum", "Int64", 0, "Int64"),
("prod", "Int64", 1, "Int64"),
("sum", "UInt8", 0, ("UInt32" if is_windows_np2_or_is32 else "UInt64")),
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def three_group():
)


@pytest.fixture()
@pytest.fixture
def slice_test_df():
data = [
[0, "a", "a0_at_0"],
Expand All @@ -108,7 +108,7 @@ def slice_test_df():
return df.set_index("Index")


@pytest.fixture()
@pytest.fixture
def slice_test_grouped(slice_test_df):
return slice_test_df.groupby("Group", as_index=False)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ def test_dataframe_categorical_ordered_observed_sort(ordered, observed, sort):
f"for (ordered={ordered}, observed={observed}, sort={sort})\n"
f"Result:\n{result}"
)
assert False, msg
pytest.fail(msg)


def test_datetime():
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/groupby/test_groupby_dropna.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ def test_groupby_drop_nan_with_multi_index():
"UInt64",
"Int64",
"Float32",
"Int64",
"Float64",
"category",
"string",
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/indexes/datetimelike_/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ def test_sort_values_without_freq_timedeltaindex(self):
@pytest.mark.parametrize(
"index_dates,expected_dates",
[
(
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
),
(
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/indexes/multi/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ def test_from_arrays_empty():
(1, 2),
([1], 2),
(1, [2]),
"a",
("a",),
("a", "b"),
(["a"], "b"),
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,6 @@ def test_array_to_slice_conversion(self, arr, slc):
[-1],
[-1, -2, -3],
[-10],
[-1],
[-1, 0, 1, 2],
[-2, 0, 2, 4],
[1, 0, -1],
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/io/formats/test_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def test_css_none_absent(style, equiv):
("02.54cm", "72pt"),
("25.4mm", "72pt"),
("101.6q", "72pt"),
("101.6q", "72pt"),
],
)
@pytest.mark.parametrize("relative_to", [None, "16pt"]) # invariant to inherited size
Expand Down
Loading