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

feat: Add no_operation_for_linting_chain_calls operation to Series, D… #14223

Closed

Conversation

ghuls
Copy link
Collaborator

@ghuls ghuls commented Feb 2, 2024

…ataFrame and LazyFrame

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Feb 2, 2024
@ghuls
Copy link
Collaborator Author

ghuls commented Feb 2, 2024

This allows relatively easily keeping short chain commands formatted as intended after running black or ruff format:

# Original.
import polars as pl

df = (
    cell_annotations.filter(pl.col("a") == 5)
    .select(pl.col("b"))
)

a = pl.Series("a", range(20))
b = (
    a.filter(a > 15)
    .sum()
)

# Original after  `black` or `ruff format`:
import polars as pl

df = cell_annotations.filter(pl.col("a") == 5).select(pl.col("b"))

a = pl.Series("a", range(20))
b = a.filter(a > 15).sum()


# After adding `no_operation_for_linting_chain_calls()` calls to originals to prevent `black` or `ruff format` putting all code on one line:
import polars as pl

df = (
    cell_annotations.filter(pl.col("a") == 5)
    .select(pl.col("b"))
    .no_operation_for_linting_chain_calls()
)

a = pl.Series("a", range(20))
b = (
    a.filter(a > 15)
    .sum()
    .no_operation_for_linting_chain_calls()
    .no_operation_for_linting_chain_calls()
)


# After adding `no_operation_for_linting_chain_calls()` calls and running `black` or `ruff format`:
import polars as pl

df = (
    cell_annotations.filter(pl.col("a") == 5)
    .select(pl.col("b"))
    .no_operation_for_linting_chain_calls()
)

a = pl.Series("a", range(20))
b = (
    a.filter(a > 15)
    .sum()
    .no_operation_for_linting_chain_calls()
    .no_operation_for_linting_chain_calls()
)

There is more than one issue about keeping Polars code in chain mode on the ruff repo. Adding an "empty" call, allows users to do it:
astral-sh/ruff#8598

@mcrumiller
Copy link
Contributor

Am I correct in that the solution implemented here is to implement a function with a really long name in order to get the formatter to do what you want? This seems like a really bad idea. For now, just use a fmt: off and fmt: on:

a = pl.Series("a", range(20))
b = (
    # fmt: off
    a.filter(a > 15)
    .sum()
    # fmt: on
)

Copy link
Contributor

@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 for the PR, but I don't think this is the right solution to the problem.

I would argue the initial 'problem' isn't even a problem, since the whole point of formatters is that they are opinionated and the opinions of the people writing the code no longer matter.

If the formatter does not do what you want it to do, it should be solved by the formatter. Either they should change their rules, or by turning it off (e.g. # fmt: off or the various other possibilities).

We definitely should not be including no-op methods in our public API just to produce a certain formatting.

If you really want to do this, define your own custom function and pipe it, or a similar hack. But I strongly discourage this practice.

@stinodego stinodego closed this Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants