Skip to content

Commit

Permalink
enh: ruff S rule fix (#526)
Browse files Browse the repository at this point in the history
* flake8-bandit

* rm agg arrow assertions

* not covering

* one more :(

* edit err msg and type
  • Loading branch information
FBruzzesi authored Jul 15, 2024
1 parent 2bc3a6f commit a5276cd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 6 additions & 3 deletions narwhals/_arrow/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ def agg_arrow(
simple_aggregations: dict[str, tuple[str, str]] = {}
for expr in exprs:
# e.g. agg(nw.mean('a')) # noqa: ERA001
assert expr._depth == 1
assert expr._root_names is not None
assert expr._output_names is not None
if (
expr._depth != 1 or expr._root_names is None or expr._output_names is None
): # pragma: no cover
msg = "Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues"
raise AssertionError(msg)

function_name = remove_prefix(expr._function_name, "col->")
function_name = POLARS_TO_ARROW_AGGREGATIONS.get(function_name, function_name)
for root_name, output_name in zip(expr._root_names, expr._output_names):
Expand Down
16 changes: 11 additions & 5 deletions narwhals/_expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ def func(df: CompliantDataFrame) -> list[CompliantSeries]:
out.append(plx._create_series_from_scalar(_out, column)) # type: ignore[arg-type]
else:
out.append(_out)
if expr._output_names is not None: # safety check
assert [s.name for s in out] == expr._output_names
if expr._output_names is not None and (
[s.name for s in out] != expr._output_names
): # pragma: no cover
msg = "Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues"
raise AssertionError(msg)
return out

# Try tracking root and output names by combining them from all
Expand All @@ -211,9 +214,12 @@ def func(df: CompliantDataFrame) -> list[CompliantSeries]:
output_names = None
break

assert (output_names is None and root_names is None) or (
output_names is not None and root_names is not None
) # safety check
if not (
(output_names is None and root_names is None)
or (output_names is not None and root_names is not None)
): # pragma: no cover
msg = "Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues"
raise AssertionError(msg)

return plx._create_expr_from_callable( # type: ignore[return-value]
func, # type: ignore[arg-type]
Expand Down
14 changes: 10 additions & 4 deletions narwhals/_pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ def agg_pandas(
for expr in exprs:
if expr._depth == 0:
# e.g. agg(nw.len()) # noqa: ERA001
assert expr._output_names is not None
if expr._output_names is None: # pragma: no cover
msg = "Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues"
raise AssertionError(msg)

function_name = POLARS_TO_PANDAS_AGGREGATIONS.get(
expr._function_name, expr._function_name
)
Expand All @@ -128,9 +131,12 @@ def agg_pandas(
continue

# e.g. agg(nw.mean('a')) # noqa: ERA001
assert expr._depth == 1
assert expr._root_names is not None
assert expr._output_names is not None
if (
expr._depth != 1 or expr._root_names is None or expr._output_names is None
): # pragma: no cover
msg = "Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues"
raise AssertionError(msg)

function_name = remove_prefix(expr._function_name, "col->")
function_name = POLARS_TO_PANDAS_AGGREGATIONS.get(
function_name, function_name
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ lint.ignore = [
'PLR2004',
'PTH',
'RET505',
'S',
'SLF001',
'TD003',
'TRY003', # TODO(Unassigned): enable
'TRY004'
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101"]
"utils/*" = ["S311"]

[tool.ruff.lint.isort]
force-single-line = true

Expand Down

0 comments on commit a5276cd

Please sign in to comment.