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

Conversation

kutal10
Copy link
Contributor

@kutal10 kutal10 commented May 24, 2025

Fixes #17879

Rationale

This enables dictionary information (which is a raw python iterable type under the hood) to not be passed to column projection operators like select and with_columns.

By guarding at the python level, this prevents it ever hitting the query planner and doing further work.

Information in the format of a dictionary should be explicitly stated to meet the API requirements.

Changes

  • Adds guard in the core _parse_inputs_as_iterable method with an instructional error message

Tests

  • 3 tests added to test_df.py (if there's a better place to put these let me know)
  • Existing make tests test suite passes
  • make pre-commit run before PR

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels May 24, 2025
Copy link

codecov bot commented May 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.64%. Comparing base (5be0a9b) to head (35e6b3c).
Report is 46 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #22928      +/-   ##
==========================================
- Coverage   80.99%   80.64%   -0.36%     
==========================================
  Files        1674     1677       +3     
  Lines      236899   222202   -14697     
  Branches     2787     2801      +14     
==========================================
- Hits       191882   179196   -12686     
+ Misses      44350    42338    -2012     
- Partials      667      668       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -5851,6 +5851,16 @@ def with_columns(
│ 4 ┆ 13.0 ┆ {1,3.0} │
└─────┴──────┴─────────────┘
"""
# Ensures that the outermost element cannot be a Dictionary (as an iterable)
if len(exprs) == 1 and isinstance(exprs[0], Mapping):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guard position should be at the generic location. I believe that is in _parse_inputs_as_iterable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move it to the generic location _parse_inputs_as_iterable - then we move the radius of the guard to impact more syntax than just with_columns.

For example, moving it will cause a test failure of test_projection_join_names_9955 where a select statement takes in a dict equivalent:

def test_projection_join_names_9955() -> None:
    batting = pl.LazyFrame(
        {
            "playerID": ["abercda01"],
            "yearID": [1871],
            "lgID": ["NA"],
        }
    )

    awards_players = pl.LazyFrame(
        {
            "playerID": ["bondto01"],
            "yearID": [1877],
            "lgID": ["NL"],
        }
    )

    right = awards_players.filter(pl.col("lgID") == "NL").select("playerID")

    q = batting.join(
        right,
        left_on=[pl.col("playerID")],
        right_on=[pl.col("playerID")],
        how="inner",
    )

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

    assert q.collect().schema == {
        "playerID": pl.String,
        "yearID": pl.Int64,
        "lgID": pl.String,
    }

Which is why i'm hesitant to move it inwards to the generic, because that theoretically is a breaking change, instead of leaving it at with_columns.

Copy link
Contributor Author

@kutal10 kutal10 May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ritchie46 As an alternative, if I was to move the guard down to the generic function - I can do the following:

  1. Update the above failing test to just make it work in the new _parse_inputs_as_iterable
-q = q.select(batting.collect_schema())
+q = q.select(*batting.collect_schema().keys())
  1. Update the message so it's relevant for all projection keywords, not just with_columns
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())"
        )
  1. Change the PR scope / title to point out it impacts all such column projection keywords, not just with_columns

Let me know if that sounds reasonable!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's good. In select we also should not accept dictionaries.

@kutal10 kutal10 requested a review from ritchie46 May 26, 2025 11:03
@ritchie46
Copy link
Member

Looks great. Thank you!

@ritchie46 ritchie46 merged commit e2f8e48 into pola-rs:main May 26, 2025
16 checks passed
@kutal10 kutal10 changed the title fix(python): Guard against dictionaries being passed to with_columns fix(python): Guard against dictionaries being passed to column projection keywords May 26, 2025
@kutal10 kutal10 changed the title fix(python): Guard against dictionaries being passed to column projection keywords fix(python): Guard against dictionaries being passed to projection keywords May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

with_columns(dict) silently ignores dictionary values
2 participants