Skip to content

fix(python): Guard against dictionaries being passed to projection keywords #22928

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 5 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion py-polars/polars/_utils/parse/expr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import contextlib
from collections.abc import Iterable
from collections.abc import Iterable, Mapping
from typing import TYPE_CHECKING, Any

import polars._reexport as pl
Expand Down Expand Up @@ -120,6 +120,18 @@ def _parse_inputs_as_iterable(
if not inputs:
return []

# Ensures that the outermost element cannot be a Dictionary (as an iterable)
if len(inputs) == 1 and isinstance(inputs[0], Mapping):
msg = (
"Cannot pass a dictionary as a single positional argument.\n"
"If you merely want the *keys*, use:\n"
" • df.method(*your_dict.keys())\n"
"If you need the key value pairs, use one of:\n"
" • unpack as keywords: df.method(**your_dict)\n"
" • build expressions: df.method(expr.alias(k) for k, expr in your_dict.items())"
)
raise TypeError(msg)

# Treat elements of a single iterable as separate inputs
if len(inputs) == 1 and _is_iterable(inputs[0]):
return inputs[0]
Expand Down
26 changes: 26 additions & 0 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -3224,3 +3224,29 @@ def test_nan_to_null() -> None:
)

assert_frame_equal(df1, df2)


# Below 3 tests for https://github.com/pola-rs/polars/issues/17879


def test_with_columns_dict_direct_typeerror() -> None:
data = {"a": pl.col("a") * 2}
df = pl.select(a=1)
with pytest.raises(
TypeError, match="Cannot pass a dictionary as a single positional argument"
):
df.with_columns(data)


def test_with_columns_dict_unpacking() -> None:
data = {"a": pl.col("a") * 2}
df = pl.select(a=1).with_columns(**data)
expected = pl.DataFrame({"a": [2]})
assert df.equals(expected)


def test_with_columns_generator_alias() -> None:
data = {"a": pl.col("a") * 2}
df = pl.select(a=1).with_columns(expr.alias(name) for name, expr in data.items())
expected = pl.DataFrame({"a": [2]})
assert df.equals(expected)
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def test_projection_join_names_9955() -> None:
how="inner",
)

q = q.select(batting.collect_schema())
q = q.select(*batting.collect_schema().keys())

assert q.collect().schema == {
"playerID": pl.String,
Expand Down
Loading