Skip to content

Commit

Permalink
chore: Replace black by ruff format (#11996)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Oct 26, 2023
1 parent 667bcd4 commit 8adbf8e
Show file tree
Hide file tree
Showing 29 changed files with 95 additions and 106 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ jobs:

- name: Lint Python
run: |
ruff --exit-non-zero-on-fix .
black --check .
blackdoc --check .
ruff check --diff .
ruff format --diff .
blackdoc --diff .
mypy:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ make pre-commit
Note that we do not actually use the [pre-commit](https://pre-commit.com/) tool.
We use the Makefile to conveniently run the following formatting and linting tools:

- [black](https://black.readthedocs.io/) and [blackdoc](https://github.com/keewis/blackdoc)
- [ruff](https://github.com/charliermarsh/ruff)
- [blackdoc](https://github.com/keewis/blackdoc)
- [mypy](http://mypy-lang.org/)
- [rustfmt](https://github.com/rust-lang/rustfmt)
- [clippy](https://doc.rust-lang.org/nightly/clippy/index.html)
Expand Down
2 changes: 1 addition & 1 deletion docs/development/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ make pre-commit
Note that we do not actually use the [pre-commit](https://pre-commit.com/) tool.
We use the Makefile to conveniently run the following formatting and linting tools:

- [black](https://black.readthedocs.io/) and [blackdoc](https://github.com/keewis/blackdoc)
- [ruff](https://github.com/charliermarsh/ruff)
- [blackdoc](https://github.com/keewis/blackdoc)
- [mypy](http://mypy-lang.org/)
- [rustfmt](https://github.com/rust-lang/rustfmt)
- [clippy](https://doc.rust-lang.org/nightly/clippy/index.html)
Expand Down
4 changes: 2 additions & 2 deletions py-polars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ build-release-native: .venv ## Same as build-release, except with native CPU op

.PHONY: fmt
fmt: .venv ## Run autoformatting and linting
$(VENV_BIN)/ruff .
$(VENV_BIN)/black .
$(VENV_BIN)/ruff check .
$(VENV_BIN)/ruff format .
$(VENV_BIN)/blackdoc .
$(VENV_BIN)/typos ..
cargo fmt --all
Expand Down
4 changes: 3 additions & 1 deletion py-polars/polars/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ def save_to_file(cls, file: Path | str) -> None:
@classmethod
@deprecate_nonkeyword_arguments(version="0.19.3")
def state(
cls, if_set: bool = False, env_only: bool = False # noqa: FBT001
cls,
if_set: bool = False, # noqa: FBT001
env_only: bool = False, # noqa: FBT001
) -> dict[str, str | None]:
"""
Show the current state of all Config variables as a dict.
Expand Down
30 changes: 21 additions & 9 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
NoReturn,
Sequence,
TypeVar,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -174,10 +175,10 @@
# MultiColSelector indexes into the horizontal axis
# NOTE: wrapping these as strings is necessary for Python <3.10

MultiRowSelector: TypeAlias = "slice | range | list[int] | Series"
MultiColSelector: TypeAlias = (
"slice | range | list[int] | list[str] | list[bool] | Series"
)
MultiRowSelector: TypeAlias = Union[slice, range, "list[int]", "Series"]
MultiColSelector: TypeAlias = Union[
slice, range, "list[int]", "list[str]", "list[bool]", "Series"
]

T = TypeVar("T")
P = ParamSpec("P")
Expand Down Expand Up @@ -1276,7 +1277,9 @@ def __array__(self, dtype: Any = None) -> np.ndarray[Any, Any]:
return self.to_numpy().__array__()

def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True # noqa: FBT001
self,
nan_as_null: bool = False, # noqa: FBT001
allow_copy: bool = True, # noqa: FBT001
) -> PolarsDataFrame:
"""
Convert to a dataframe object implementing the dataframe interchange protocol.
Expand Down Expand Up @@ -1911,13 +1914,15 @@ def to_dict(self, as_series: Literal[False]) -> dict[str, list[Any]]:

@overload
def to_dict(
self, as_series: bool # noqa: FBT001
self,
as_series: bool, # noqa: FBT001
) -> dict[str, Series] | dict[str, list[Any]]:
...

# TODO: Make `as_series` keyword-only
def to_dict(
self, as_series: bool = True # noqa: FBT001
self,
as_series: bool = True, # noqa: FBT001
) -> dict[str, Series] | dict[str, list[Any]]:
"""
Convert DataFrame to a dictionary mapping column name to values.
Expand Down Expand Up @@ -2029,7 +2034,10 @@ def to_dicts(self) -> list[dict[str, Any]]:

@deprecate_nonkeyword_arguments(version="0.19.3")
def to_numpy(
self, structured: bool = False, *, order: IndexOrder = "fortran" # noqa: FBT001
self,
structured: bool = False, # noqa: FBT001
*,
order: IndexOrder = "fortran",
) -> np.ndarray[Any, Any]:
"""
Convert DataFrame to a 2D NumPy array.
Expand Down Expand Up @@ -3094,7 +3102,11 @@ def write_excel(
options = {"hidden": True}
if column in column_widths: # type: ignore[operator]
ws.set_column_pixels(
col_idx, col_idx, column_widths[column], None, options # type: ignore[index]
col_idx,
col_idx,
column_widths[column], # type: ignore[index]
None,
options,
)
elif options:
ws.set_column(col_idx, col_idx, None, None, options)
Expand Down
4 changes: 1 addition & 3 deletions py-polars/polars/datatypes/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ def _map_py_type_to_dtype(
if len(nested) == 1:
nested = nested[0]
return (
dtype
if nested is None
else dtype(_map_py_type_to_dtype(nested)) # type: ignore[operator]
dtype if nested is None else dtype(_map_py_type_to_dtype(nested)) # type: ignore[operator]
)

raise TypeError("invalid type")
Expand Down
4 changes: 3 additions & 1 deletion py-polars/polars/interchange/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def __init__(self, df: DataFrame, *, allow_copy: bool = True):
self._allow_copy = allow_copy

def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True # noqa: FBT001
self,
nan_as_null: bool = False, # noqa: FBT001
allow_copy: bool = True, # noqa: FBT001
) -> PolarsDataFrame:
"""
Construct a new dataframe object, potentially changing the parameters.
Expand Down
8 changes: 6 additions & 2 deletions py-polars/polars/interchange/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ def version(self) -> int:
"""Version of the protocol."""

def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True # noqa: FBT001
self,
nan_as_null: bool = False, # noqa: FBT001
allow_copy: bool = True, # noqa: FBT001
) -> DataFrame:
"""Convert to a dataframe object implementing the dataframe interchange protocol.""" # noqa: W505

Expand Down Expand Up @@ -236,7 +238,9 @@ class SupportsInterchange(Protocol):
"""Dataframe that supports conversion into an interchange dataframe object."""

def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True # noqa: FBT001
self,
nan_as_null: bool = False, # noqa: FBT001
allow_copy: bool = True, # noqa: FBT001
) -> SupportsInterchange:
"""Convert to a dataframe object implementing the dataframe interchange protocol.""" # noqa: W505

Expand Down
4 changes: 1 addition & 3 deletions py-polars/polars/testing/parametric/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,7 @@ def draw_frames(draw: DrawFn) -> DataFrame | LazyFrame:
schema={repr(schema).replace("', ","', pl.")},
orient={orient!r},
)
""".replace(
"datetime.", ""
)
""".replace("datetime.", "")
)
# note: this avoids printing the repro twice
if failed_frame_init not in _failed_frame_init_msgs_:
Expand Down
4 changes: 3 additions & 1 deletion py-polars/polars/utils/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def __init__(self) -> None:
self._watcher.start(self._watcher_callback)

def get(
self, block: bool = True, timeout: float | int | None = None # noqa: FBT001
self,
block: bool = True, # noqa: FBT001
timeout: float | int | None = None,
) -> T:
return self.result.get(block=block, timeout=timeout)

Expand Down
7 changes: 2 additions & 5 deletions py-polars/polars/utils/_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ def _construct_series_with_fallbacks(
if "'float'" in str_exc and (
# we do not accept float values as int/temporal, as it causes silent
# information loss; the caller should explicitly cast in this case.
target_dtype
not in (INTEGER_DTYPES | TEMPORAL_DTYPES)
target_dtype not in (INTEGER_DTYPES | TEMPORAL_DTYPES)
):
constructor = py_type_to_constructor(float)

Expand Down Expand Up @@ -741,9 +740,7 @@ def _unpack_schema(
else None
)
column_dtypes: dict[str, PolarsDataType] = {
lookup.get((name := col[0]), name)
if lookup
else col[0]: dtype # type: ignore[misc]
lookup.get((name := col[0]), name) if lookup else col[0]: dtype # type: ignore[misc]
if is_polars_dtype(dtype, include_unknown=True)
else py_type_to_dtype(dtype)
for col in schema
Expand Down
7 changes: 3 additions & 4 deletions py-polars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ select = [
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"Q", # flake8-quotes
"UP", # pyupgrade
"PT", # flake8-pytest-style
"RUF", # Ruff-specific rules
Expand Down Expand Up @@ -172,14 +171,14 @@ ignore = [
"TD003", # Missing issue link on the line following this TODO
# tryceratops
"TRY003", # Avoid specifying long messages outside the exception class
# Lints below are turned off because of conflicts with the ruff formatter
"D206",
"W191",
]

[tool.ruff.pycodestyle]
max-doc-length = 88

[tool.ruff.isort]
split-on-trailing-comma = false

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"

Expand Down
3 changes: 1 addition & 2 deletions py-polars/requirements-lint.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
black==23.10.0
blackdoc==0.3.8
mypy==1.6.0
ruff==0.1.0
ruff==0.1.2
typos==1.16.20
22 changes: 11 additions & 11 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -2641,9 +2641,7 @@ def test_fill_null_limits() -> None:
pl.all().fill_null(strategy="forward", limit=2),
pl.all().fill_null(strategy="backward", limit=2).name.suffix("_backward"),
]
).to_dict(
False
) == {
).to_dict(False) == {
"a": [1, 1, 1, None, 5, 6, 6, 6, None, 10],
"b": ["a", "a", "a", None, "b", "c", "c", "c", None, "d"],
"c": [True, True, True, None, False, True, True, True, None, False],
Expand Down Expand Up @@ -2774,15 +2772,17 @@ def test_selection_regex_and_multicol() -> None:
]


def test_unique_on_sorted() -> None:
@pytest.mark.parametrize("subset", ["a", cs.starts_with("x", "a")])
def test_unique_on_sorted(subset: Any) -> None:
df = pl.DataFrame(data={"a": [1, 1, 3], "b": [1, 2, 3]})
for subset in ("a", cs.starts_with("x", "a")):
assert df.with_columns([pl.col("a").set_sorted()]).unique(
subset=subset, keep="last" # type: ignore[arg-type]
).to_dict(False) == {
"a": [1, 3],
"b": [2, 3],
}

result = df.with_columns([pl.col("a").set_sorted()]).unique(
subset=subset,
keep="last",
)

expected = pl.DataFrame({"a": [1, 3], "b": [2, 3]})
assert_frame_equal(result, expected)


def test_len_compute(df: pl.DataFrame) -> None:
Expand Down
10 changes: 2 additions & 8 deletions py-polars/tests/unit/datatypes/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,9 @@ def test_nested_categorical_aggregation_7848() -> None:
}
).with_columns([pl.col("letter").cast(pl.Categorical)]).group_by(
maintain_order=True, by=["group"]
).all().with_columns(
[pl.col("letter").list.len().alias("c_group")]
).group_by(
).all().with_columns(pl.col("letter").list.len().alias("c_group")).group_by(
by=["c_group"], maintain_order=True
).agg(
pl.col("letter")
).to_dict(
False
) == {
).agg(pl.col("letter")).to_dict(False) == {
"c_group": [2, 3],
"letter": [[["a", "b"], ["f", "g"]], [["c", "d", "e"]]],
}
Expand Down
8 changes: 5 additions & 3 deletions py-polars/tests/unit/datatypes/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def test_decimal_aggregations() -> None:
sum=pl.sum("a"),
min=pl.min("a"),
max=pl.max("a"),
).to_dict(
False
) == {"sum": [D("9110.33")], "min": [D("0.10")], "max": [D("9000.12")]}
).to_dict(False) == {
"sum": [D("9110.33")],
"min": [D("0.10")],
"max": [D("9000.12")],
}
4 changes: 1 addition & 3 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ def test_list_count_matches_deprecated() -> None:
{"listcol": [[], [1], [1, 2, 3, 2], [1, 2, 1], [4, 4]]}
).select(pl.col("listcol").list.count_match(2).alias("number_of_twos")).to_dict(
False
) == {
"number_of_twos": [0, 0, 2, 1, 0]
}
) == {"number_of_twos": [0, 0, 2, 1, 0]}


def test_list_count_matches() -> None:
Expand Down
8 changes: 2 additions & 6 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ def test_struct_list_head_tail() -> None:
pl.col("list_of_struct").list.head(1).alias("head"),
pl.col("list_of_struct").list.tail(1).alias("tail"),
]
).to_dict(
False
) == {
).to_dict(False) == {
"list_of_struct": [
[{"a": 1, "b": 4}, {"a": 3, "b": 6}],
[{"a": 10, "b": 40}, {"a": 20, "b": 50}, {"a": 30, "b": 60}],
Expand Down Expand Up @@ -401,9 +399,7 @@ def test_struct_concat_list() -> None:
}
).with_columns(
[pl.col("list_struct1").list.concat("list_struct2").alias("result")]
)[
"result"
].to_list() == [
)["result"].to_list() == [
[{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 6, "b": 7}, {"a": 8, "b": 9}],
[{"a": 1, "b": 2}, {"a": 6, "b": 7}],
]
Expand Down
14 changes: 3 additions & 11 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,11 +1263,7 @@ def test_unique_counts_on_dates() -> None:
pl.col("dt_ns").dt.cast_time_unit("ms").alias("dt_ms"),
pl.col("dt_ns").cast(pl.Date).alias("date"),
]
).select(
pl.all().unique_counts().sum()
).to_dict(
False
) == {
).select(pl.all().unique_counts().sum()).to_dict(False) == {
"dt_ns": [3],
"dt_us": [3],
"dt_ms": [3],
Expand Down Expand Up @@ -1304,9 +1300,7 @@ def test_rolling_by_ordering() -> None:
[
pl.col("val").sum().alias("sum val"),
]
).to_dict(
False
) == {
).to_dict(False) == {
"key": ["A", "A", "A", "A", "B", "B", "B"],
"dt": [
datetime(2022, 1, 1, 0, 1),
Expand Down Expand Up @@ -1402,9 +1396,7 @@ def test_sum_duration() -> None:
]
).select(
[pl.col("duration").sum(), pl.col("duration").dt.seconds().alias("sec").sum()]
).to_dict(
False
) == {
).to_dict(False) == {
"duration": [timedelta(seconds=150)],
"sec": [150],
}
Expand Down
Loading

0 comments on commit 8adbf8e

Please sign in to comment.