HumpbackWhale194
HumpbackWhale194

Reputation: 418

How do I configure Python "yapf" to format method calls with chained methods in args (Polars-style) like this?

How do I configure YAPF to auto-format Python code like the following example? This is very useful in the domain-specific language (DSL) that Polars creates for data transformations.

This is roughly how I want it to be formatted (note the indented chained method calls):

df = df.select(
    event_type=pl.col("log_entry")
        .str.extract(r"Event Type: ([\w ]+)")
        .cast(pl.Utf8),
    event_duration=pl.col("log_entry")
        .str.extract(r"Duration: (\d+)")
        .cast(pl.Int32),
)

With the default settings, it likes to left-align .select/.with_columns calls with many arguments. The following is how yapf/ruff/black all format the code (which is difficult to read):

import polars as pl

df = df.select(
    event_type=pl.col("log_entry")
    .str.extract(r"Event Type: ([\w ]+)")
    .cast(pl.Utf8),
    event_duration=pl.col("log_entry")
    .str.extract(r"Duration: (\d+)")
    .cast(pl.Int32),
)

Upvotes: 1

Views: 108

Answers (0)

Related Questions