Let's say I have a file like this:
import pandas as pd
df = pd.DataFrame(
{
"A": [" a: yu", "b: stuff ", "c: more_stuff"],
"B": [4, 5, 6],
"C": [7, 8, 9],
}
)
df["A"] = (
df["A"]
.str.strip()
.str.replace(":", "")
.str[0]
)
new_df = pd.melt(
df,
id_vars=["A"]
)
print(new_df)
If I then run
ruff format --diff play_line_breaks.py
I get
-df["A"] = (
- df["A"]
- .str.strip()
- .str.replace(":", "")
- .str[0]
-)
+df["A"] = df["A"].str.strip().str.replace(":", "").str[0]
-new_df = pd.melt(
- df,
- id_vars=["A"]
-)
+new_df = pd.melt(df, id_vars=["A"])
print(new_df)
So, the ruff
formatter would convert my multiline statements into a single line. I find the multiline version far more readable and would like to keep it. Is there any setting in ruff
that would allow me to say "don't touch any line breaks"?
The best I could find are
skip-magic-trailing-comma = false (in my pyproject.toml)
which does not impact the output from above though
or wrapping the two statements like this
# fmt: off
< all code after df assignment >
# fmt: on
That works, but I find it rather cumbersome to do this for all the statements I have in my code base.
Are there any smarter ways of doing this?
It seems there is no option available at the moment in ruff
. See: ruff formatter: one call per line for chained method calls.
Seems like putting
# fmt: skip
next to the closing parentheses works as a quick hack.import pandas as pd df = pd.DataFrame( { "A": [" a: yu", "b: stuff ", "c: more_stuff"], "B": [4, 5, 6], "C": [7, 8, 9], } ) df["A"] = ( df["A"] .str.strip() .str.replace(":", "") .str[0] ) # fmt: skip new_df = pd.melt( df, id_vars=["A"] ) # fmt: skip print(new_df)