In my Python code I have a line that looks like so:
tm = tm[0:3] + [0,] + tm[3:6] + [0,]
Ruff attempts to fix this with the following 10 lines, which are arguably less legible:
tm = (
tm[0:3]
+ [
0,
]
+ tm[3:6]
+ [
0,
]
)
Why is Ruff expanding the statement? Is there a way to disable this behaviour?
tm = (
tm[0:3]
+ [
0, # <---
]
+ tm[3:6]
+ [
0, # <---
]
)
Both of those "<---" lines end with what
ruff calls "the magic comma".
It is an explicit request to please use multi-line formatting.
A more obvious example might look like
+ [
1,
2,
3, # <---
]
where the final , comma after 3 is the one that asks for single element per line.
Remove that optional comma, and both ruff and black will revert to a more compact notation.
In many source files it's a big advantage to request such formatting,
as it minimizes diffs.
Some non-python languages, such as SQL, prohibit a trailing , comma.
This leads to needlessly verbose diffs,
which show that final line was "3" (no comma),
and in the edited version we have "3," and a new final line of "4" (no comma).
Inevitably "5" is added, requiring us to dirty the "4" line to add a comma.
Occasionally you may see some craziness like this,
which is a very painful way to improve the accuracy
of git annotate foo.sql output.
SELECT
a
, b
, c
FROM some_table ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With