Skip to content

Commit

Permalink
chore(python): Fix flake8-pytest-style errors in tests. (#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls committed Jan 4, 2023
1 parent 1067ec3 commit 32c094d
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 54 deletions.
4 changes: 4 additions & 0 deletions py-polars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ select = [
"Q",
# pyupgrade
"UP",
# flake8-pytest-style
"PT",
]
ignore = []

Expand All @@ -134,6 +136,8 @@ extend-ignore = [
"D402",
"D415",
"D416",
# flake8-pytest-style:
"PT011", # pytest.raises({exception}) is too broad, set the match parameter or use a more specific exception
# TODO: Remove errors below to further improve docstring linting
# Ordered from most common to least common errors.
"D105",
Expand Down
2 changes: 1 addition & 1 deletion py-polars/requirements-lint.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
black==22.8.0
blackdoc==0.3.7
ruff==0.0.208
ruff==0.0.209
2 changes: 1 addition & 1 deletion py-polars/tests/slow/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
)


@pytest.fixture
@pytest.fixture()
def io_test_dir() -> str:
return IO_TEST_DIR
20 changes: 10 additions & 10 deletions py-polars/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@
)


@pytest.fixture
@pytest.fixture()
def io_test_dir() -> str:
return IO_TEST_DIR


@pytest.fixture
@pytest.fixture()
def examples_dir() -> str:
return EXAMPLES_DIR


@pytest.fixture
@pytest.fixture()
def foods_csv() -> str:
return FOODS_CSV


@pytest.fixture
@pytest.fixture()
def foods_csv_glob() -> str:
return FOODS_CSV

Expand All @@ -76,22 +76,22 @@ def foods_csv_glob() -> str:
pl.read_csv(FOODS_CSV).write_json(FOODS_NDJSON, json_lines=True)


@pytest.fixture
@pytest.fixture()
def foods_ipc() -> str:
return FOODS_IPC


@pytest.fixture
@pytest.fixture()
def foods_parquet() -> str:
return FOODS_PARQUET


@pytest.fixture
@pytest.fixture()
def foods_ndjson() -> str:
return FOODS_NDJSON


@pytest.fixture
@pytest.fixture()
def df() -> pl.DataFrame:
df = pl.DataFrame(
{
Expand Down Expand Up @@ -122,14 +122,14 @@ def df() -> pl.DataFrame:
)


@pytest.fixture
@pytest.fixture()
def df_no_lists(df: pl.DataFrame) -> pl.DataFrame:
return df.select(
pl.all().exclude(["list_str", "list_int", "list_bool", "list_int", "list_flt"])
)


@pytest.fixture
@pytest.fixture()
def fruits_cars() -> pl.DataFrame:
return pl.DataFrame(
{
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/io/test_avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import polars as pl


@pytest.fixture
@pytest.fixture()
def example_df() -> pl.DataFrame:
return pl.DataFrame({"i64": [1, 2], "f64": [0.1, 0.2], "utf8": ["a", "b"]})


@pytest.fixture
@pytest.fixture()
def compressions() -> list[str]:
return ["uncompressed", "snappy", "deflate"]

Expand Down
11 changes: 6 additions & 5 deletions py-polars/tests/unit/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ def test_partial_column_rename() -> None:


@pytest.mark.parametrize(
"col_input, col_out", [([0, 1], ["a", "b"]), ([0, 2], ["a", "c"]), (["b"], ["b"])]
("col_input", "col_out"),
[([0, 1], ["a", "b"]), ([0, 2], ["a", "c"]), (["b"], ["b"])],
)
def test_read_csv_columns_argument(
col_input: list[int] | list[str], col_out: list[str]
Expand Down Expand Up @@ -753,7 +754,7 @@ def test_csv_dtype_overwrite_bool() -> None:


@pytest.mark.parametrize(
"fmt,expected",
("fmt", "expected"),
[
(None, "dt\n2022-01-02T00:00:00.000000\n"),
("%F %T%.3f", "dt\n2022-01-02 00:00:00.000\n"),
Expand All @@ -770,7 +771,7 @@ def test_datetime_format(fmt: str, expected: str) -> None:


@pytest.mark.parametrize(
"tu1,tu2,expected",
("tu1", "tu2", "expected"),
[
(
"ns",
Expand Down Expand Up @@ -810,7 +811,7 @@ def test_datetime_format_inferred_precision(


@pytest.mark.parametrize(
"fmt,expected",
("fmt", "expected"),
[
(None, "dt\n2022-01-02\n"),
("%Y", "dt\n2022\n"),
Expand All @@ -825,7 +826,7 @@ def test_date_format(fmt: str, expected: str) -> None:


@pytest.mark.parametrize(
"fmt,expected",
("fmt", "expected"),
[
(None, "dt\n16:15:30.000000000\n"),
("%R", "dt\n16:15\n"),
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/io/test_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from polars.testing import assert_frame_equal_local_categoricals


@pytest.fixture
@pytest.fixture()
def compressions() -> list[str]:
return ["uncompressed", "lz4", "zstd"]

Expand Down
24 changes: 17 additions & 7 deletions py-polars/tests/unit/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]


@pytest.fixture
@pytest.fixture()
def compressions() -> list[ParquetCompression]:
return COMPRESSIONS

Expand All @@ -36,17 +36,22 @@ def test_to_from_buffer(
) -> None:
for compression in compressions:
if compression == "lzo":
# lzo compression is not supported now
buf = io.BytesIO()
# Writing lzo compressed parquet files is not supported for now.
with pytest.raises(pl.ArrowError):
buf = io.BytesIO()
df.write_parquet(buf, compression=compression)
buf.seek(0)
buf.seek(0)
# Invalid parquet file as writing failed.
with pytest.raises(pl.ArrowError):
_ = pl.read_parquet(buf)

buf = io.BytesIO()
with pytest.raises(OSError):
buf = io.BytesIO()
# Writing lzo compressed parquet files is not supported for now.
df.write_parquet(buf, compression=compression, use_pyarrow=True)
buf.seek(0)
buf.seek(0)
# Invalid parquet file as writing failed.
with pytest.raises(pl.ArrowError):
_ = pl.read_parquet(buf)
else:
buf = io.BytesIO()
Expand All @@ -69,13 +74,18 @@ def test_to_from_file(
f = os.path.join(io_test_dir, "small.parquet")
for compression in compressions:
if compression == "lzo":
# lzo compression is not supported now
# Writing lzo compressed parquet files is not supported for now.
with pytest.raises(pl.ArrowError):
df.write_parquet(f, compression=compression)
# Invalid parquet file as writing failed.
with pytest.raises(pl.ArrowError):
_ = pl.read_parquet(f)

# Writing lzo compressed parquet files is not supported for now.
with pytest.raises(OSError):
df.write_parquet(f, compression=compression, use_pyarrow=True)
# Invalid parquet file as writing failed.
with pytest.raises(FileNotFoundError):
_ = pl.read_parquet(f)
else:
df.write_parquet(f, compression=compression)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@pytest.fixture(autouse=True)
def environ() -> Iterator[None]:
def _environ() -> Iterator[None]:
"""Fixture to restore the environment variables/state after the test."""
with pl.StringCache(), pl.Config():
yield
Expand Down
6 changes: 4 additions & 2 deletions py-polars/tests/unit/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def test_init_dict() -> None:
assert df.schema == {"c": pl.Int8, "d": pl.Int16}

dfe = df.cleared()
assert (df.schema == dfe.schema) and (len(dfe) == 0)
assert df.schema == dfe.schema
assert len(dfe) == 0


def test_init_ndarray(monkeypatch: Any) -> None:
Expand Down Expand Up @@ -435,7 +436,8 @@ def test_init_only_columns() -> None:

dfe = df.cleared()
assert len(dfe) == 0
assert (df.schema == dfe.schema) and (dfe.shape == df.shape)
assert df.schema == dfe.schema
assert dfe.shape == df.shape


def test_from_dicts_list_without_dtype() -> None:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_dtypes_hashable() -> None:


@pytest.mark.parametrize(
["dtype", "representation"],
("dtype", "representation"),
[
(pl.Boolean, "Boolean"),
(pl.Datetime, "Datetime"),
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def test_date_range_lazy() -> None:


@pytest.mark.parametrize(
"one,two",
("one", "two"),
[
(date(2001, 1, 1), date(2001, 1, 2)),
(datetime(2001, 1, 1), datetime(2001, 1, 2)),
Expand Down
26 changes: 15 additions & 11 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def test_groupby_agg_input_types(lazy: bool) -> None:
df_or_lazy: pl.DataFrame | pl.LazyFrame = df.lazy() if lazy else df

for bad_param in bad_agg_parameters():
with pytest.raises(TypeError):
with pytest.raises(TypeError): # noqa: PT012
result = df_or_lazy.groupby("a").agg(bad_param)
if lazy:
result.collect() # type: ignore[union-attr]
Expand All @@ -525,7 +525,7 @@ def test_groupby_rolling_agg_input_types(lazy: bool) -> None:
df_or_lazy: pl.DataFrame | pl.LazyFrame = df.lazy() if lazy else df

for bad_param in bad_agg_parameters():
with pytest.raises(TypeError):
with pytest.raises(TypeError): # noqa: PT012
result = df_or_lazy.groupby_rolling(
index_column="index_column", period="2i"
).agg(bad_param)
Expand All @@ -549,7 +549,7 @@ def test_groupby_dynamic_agg_input_types(lazy: bool) -> None:
df_or_lazy: pl.DataFrame | pl.LazyFrame = df.lazy() if lazy else df

for bad_param in bad_agg_parameters():
with pytest.raises(TypeError):
with pytest.raises(TypeError): # noqa: PT012
result = df_or_lazy.groupby_dynamic(
index_column="index_column", every="2i", closed="right"
).agg(bad_param)
Expand All @@ -568,7 +568,7 @@ def test_groupby_dynamic_agg_input_types(lazy: bool) -> None:


@pytest.mark.parametrize(
"stack,exp_shape,exp_columns",
("stack", "exp_shape", "exp_columns"),
[
([pl.Series("stacked", [-1, -1, -1])], (3, 3), ["a", "b", "stacked"]),
(
Expand Down Expand Up @@ -707,9 +707,11 @@ def test_read_missing_file() -> None:
with pytest.raises(FileNotFoundError, match="fake_csv_file"):
pl.read_csv("fake_csv_file")

with pytest.raises(FileNotFoundError, match="fake_csv_file"):
with open("fake_csv_file") as f:
pl.read_csv(f)
with pytest.raises(FileNotFoundError, match="fake_ipc_file"):
pl.read_ipc("fake_ipc_file")

with pytest.raises(FileNotFoundError, match="fake_avro_file"):
pl.read_ipc("fake_avro_file")


def test_melt() -> None:
Expand Down Expand Up @@ -1987,7 +1989,9 @@ def test_get_item() -> None:
assert df[4, [5]].frame_equal(pl.DataFrame({"fo5": [1024]}))


@pytest.mark.parametrize("as_series,inner_dtype", [(True, pl.Series), (False, list)])
@pytest.mark.parametrize(
("as_series", "inner_dtype"), [(True, pl.Series), (False, list)]
)
def test_to_dict(as_series: bool, inner_dtype: Any) -> None:
df = pl.DataFrame(
{
Expand Down Expand Up @@ -2512,14 +2516,14 @@ def test_item() -> None:
df = pl.DataFrame({"a": [1]})
assert df.item() == 1

df = pl.DataFrame({"a": [1, 2]})
with pytest.raises(ValueError):
df = pl.DataFrame({"a": [1, 2]})
df.item()

df = pl.DataFrame({"a": [1], "b": [2]})
with pytest.raises(ValueError):
df = pl.DataFrame({"a": [1], "b": [2]})
df.item()

df = pl.DataFrame({})
with pytest.raises(ValueError):
df = pl.DataFrame({})
df.item()
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@pytest.mark.parametrize(
"expected, values",
("expected", "values"),
[
pytest.param(
"""shape: (1,)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,8 +1597,8 @@ def test_from_epoch(input_dtype: PolarsDataType) -> None:

assert_frame_equal(ldf_result, expected)

ts_col = pl.col("timestamp_s")
with pytest.raises(ValueError):
ts_col = pl.col("timestamp_s")
_ = ldf.select(pl.from_epoch(ts_col, unit="s2")) # type: ignore[call-overload]


Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_pivot_list() -> None:


@pytest.mark.parametrize(
["agg_fn", "expected_rows"],
("agg_fn", "expected_rows"),
[
("first", [("a", 2, None, None), ("b", None, None, 10)]),
("count", [("a", 2, None, None), ("b", None, 2, 1)]),
Expand All @@ -74,7 +74,7 @@ def test_pivot_aggregate(agg_fn: PivotAgg, expected_rows: list[tuple[Any]]) -> N


@pytest.mark.parametrize(
["agg_fn", "expected_rows"],
("agg_fn", "expected_rows"),
[
("first", [("a", 2, None, None), ("b", None, None, 10)]),
("count", [("a", 2, None, None), ("b", None, 2, 1)]),
Expand Down

0 comments on commit 32c094d

Please sign in to comment.