Skip to content

Commit

Permalink
feat(python)!: Restrict certain function parameters to be keyword-only (
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Jan 26, 2023
1 parent c0d5139 commit bc9dc6d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
16 changes: 11 additions & 5 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2948,6 +2948,7 @@ def replace_at_idx(self: DF, index: int, series: pli.Series) -> DF:
def sort(
self: DF,
by: str | pli.Expr | Sequence[str] | Sequence[pli.Expr],
*,
reverse: bool | list[bool] = False,
nulls_last: bool = False,
) -> DF | DataFrame:
Expand Down Expand Up @@ -3004,7 +3005,11 @@ def sort(
"""
if not isinstance(by, str) and isinstance(by, (Sequence, pli.Expr)):
df = self.lazy().sort(by, reverse, nulls_last).collect(no_optimization=True)
df = (
self.lazy()
.sort(by, reverse=reverse, nulls_last=nulls_last)
.collect(no_optimization=True)
)
return df
return self._from_pydf(self._df.sort(by, reverse, nulls_last))

Expand Down Expand Up @@ -3439,6 +3444,7 @@ def with_row_count(self: DF, name: str = "row_nr", offset: int = 0) -> DF:
def groupby(
self: DF,
by: str | pli.Expr | Sequence[str | pli.Expr],
*,
maintain_order: bool = False,
) -> GroupBy[DF]:
"""
Expand Down Expand Up @@ -5228,8 +5234,8 @@ def unstack(
def partition_by(
self: DF,
groups: str | Sequence[str],
maintain_order: bool = False,
*,
maintain_order: bool = ...,
as_dict: Literal[False] = ...,
) -> list[DF]:
...
Expand All @@ -5238,8 +5244,8 @@ def partition_by(
def partition_by(
self: DF,
groups: str | Sequence[str],
maintain_order: bool = False,
*,
maintain_order: bool = ...,
as_dict: Literal[True],
) -> dict[Any, DF]:
...
Expand All @@ -5248,17 +5254,17 @@ def partition_by(
def partition_by(
self: DF,
groups: str | Sequence[str],
maintain_order: bool,
*,
maintain_order: bool,
as_dict: bool,
) -> list[DF] | dict[Any, DF]:
...

def partition_by(
self: DF,
groups: str | Sequence[str],
maintain_order: bool = True,
*,
maintain_order: bool = True,
as_dict: bool = False,
) -> list[DF] | dict[Any, DF]:
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/internals/dataframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def head(self, n: int = 5) -> DF:
df = (
pli.wrap_df(self._df)
.lazy()
.groupby(self.by, self.maintain_order)
.groupby(self.by, maintain_order=self.maintain_order)
.head(n)
.collect(no_optimization=True)
)
Expand Down Expand Up @@ -355,7 +355,7 @@ def tail(self, n: int = 5) -> DF:
df = (
pli.wrap_df(self._df)
.lazy()
.groupby(self.by, self.maintain_order)
.groupby(self.by, maintain_order=self.maintain_order)
.tail(n)
.collect(no_optimization=True)
)
Expand Down
1 change: 1 addition & 0 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,7 @@ def search_sorted(
def sort_by(
self,
by: Expr | str | list[Expr | str],
*,
reverse: bool | list[bool] = False,
) -> Expr:
"""
Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ def sort(
| Sequence[pli.Expr]
| Sequence[str | pli.Expr]
),
*,
reverse: bool | Sequence[bool] = False,
nulls_last: bool = False,
) -> LDF:
Expand Down Expand Up @@ -1684,6 +1685,7 @@ def select(
def groupby(
self: LDF,
by: str | pli.Expr | Sequence[str | pli.Expr],
*,
maintain_order: bool = False,
) -> LazyGroupBy[LDF]:
"""
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_invalid_sort_by() -> None:
pl.ComputeError,
match="The sortby operation produced a different length than the Series that has to be sorted.", # noqa: E501
):
df.select(pl.col("a").filter(pl.col("b") == "M").sort_by("c", True))
df.select(pl.col("a").filter(pl.col("b") == "M").sort_by("c", reverse=True))


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

# Invalid input: `by` not specified as a sequence
with pytest.raises(TypeError):
df.groupby("a", "b") # type: ignore[arg-type]
df.groupby("a", "b") # type: ignore[arg-type, misc]


def test_groupby_iteration() -> None:
Expand Down

0 comments on commit bc9dc6d

Please sign in to comment.