Ruff trailing commas

Since I updated to the preview version of ruff, any function call that is just slighly too long gets updated as

def f(x, y, z, a, b, c, d):
  ...
def f(
  x, y, z, a, b, c, d
):
  ...
def f(
  x, y, z, a, b, c, d,
):
  ...
def f(
  x,
  y,
  z,
  a,
  b,
  c,
  d,
):
  ...

which is a bit silly.

def f(
  x, y, z, a, b, c, d
):
  ...

is often fine.

What causes ruff to format in this way? Do you agree with ruff? Do you know how I could tweak my ruff rules to change the behaviour?

While it’s true that not everyone is willing to use Discord, Ruff is supported using a Discord guild/server/site, and that’s likely to be the fastest way to get an answer for this question.

2 Likes

This rule is following a convention created in black, where a trailing comma indicates that you want the code to be reformatted. I believe you can turn off that rule by setting the skip-magic-trailing-comma parameter to true. Although that might change other formatting you like.

1 Like

If the parameters are short it’s fine but I like the multi-line version better in general.

Indeed, while I’m not a user of ruff or black, that formatting makes
sense particularly from a revision control and code review
standpoint. When the time comes that you want to insert or delete
items in a list-like structure, having one per line makes it easier
to see what a new commit is adding or removing because you don’t
have to compare the original line of entries to the new version, you
directly see the inserted and deleted lines of source on their own.
That’s useful regardless of whether the entries are long or short.

1 Like

The issue is where Ruff automatically inserts the trailing comma.

I am familiar with inserting a trailing comma to indicate that the arguments should be formatted at one argument per line.
My current experience is that Ruff inserts a trailing comma, and then interprets that trailing comma as a indication to itself.
Which is a bit silly.

sorry if it wasn’t clear enough that Ruff goes through the entire sequence (from top to bottom) when I use Ruff Format 3 times sequentially.

I got an answer on the Discord.
The problem is COM812 combined with the aforementioned formatter rule. I solved the problem by turning off COM812.

1 Like