Skip to content
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

fix(python): Improve map_elements typing #16257

Merged
merged 2 commits into from
May 19, 2024

Conversation

max-muoto
Copy link
Contributor

@max-muoto max-muoto commented May 16, 2024

Map elements accepts a callable Callable[[Series], Series] | Callable[[Any], Any], this for all intensive purposes should just accept Callable[[Any], Any]. Previously, the former was fine in type-checkers such as Pyright, however as Eric Traut explains here, there's not a great way for Pyright to deterministically determine which to use when evaluating lambdas, which can lead to Pyright errors when calling this function. Take this example, which will lead to an error in the latest version of Pyright, which I would imagine should be fully supported:

import polars as pl

df = pl.DataFrame(
    {
        "a": [1, 2, None],
        "b": [1.0, 2.0, 3.0],
        "c": ["a", "b", "c"],
    }
)


def transform_int(x: int) -> int:
    return x + 1

# Argument of type "Series" cannot be assigned to parameter "x" of type "int" in function "transform_int" "Series" is incompatible with "int"
df.with_columns([pl.col("a").map_elements(lambda x: transform_int(x) if x else None)])

Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.87%. Comparing base (79a2cf1) to head (93e2fc0).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16257      +/-   ##
==========================================
- Coverage   80.92%   80.87%   -0.06%     
==========================================
  Files        1393     1394       +1     
  Lines      179568   179488      -80     
  Branches     2909     2913       +4     
==========================================
- Hits       145321   145166     -155     
- Misses      33742    33819      +77     
+ Partials      505      503       -2     

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

@alexander-beedie alexander-beedie changed the title Fix Map Elements Typing fix(python): Improve map_elements typing May 16, 2024
@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels May 16, 2024
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

Thanks, not sure why the Callable[[Series], Series] was on there in the first place, but it's definitely not needed.

@stinodego stinodego merged commit 16fbb5f into pola-rs:main May 19, 2024
16 checks passed
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.

None yet

2 participants