ruff: How to use COM812 in linter but avoid conflict with formatter?

I would like to achieve the following things:

  • Use the ruff linter on my code
  • Use the ruff formatter on my code
  • Enable rule COM812 for the linter

I like the linter, I like the formatter, and I like rule COM812. However, when I have COM812 as an allowed rule I get a warning when I run the formatter that rule COM812 may cause conflicts and that it should be disabled to avoid unexpected behavior. Is there, then, a recommended way to achieve my three goals?

I see a number of issues discussing rule COM812 but I don’t see any addressing this exact question.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 18
  • Comments: 35 (20 by maintainers)

Most upvoted comments

I’m currently looking into what it takes to make COM812 compatible with the formatter. It sounds like a simple change, but it’s a very expensive token-based rule. I need to figure out how to test if all arguments (or list elements etc) are on their own line without having to track too much state.

I don’t intend to add a configuration option that gives users the old behavior because I don’t see the formatter’s behavior to be in contradiction to the rule’s goals.

An option that I would be supportive of is to add a config option to COM812 or introduce a new rule that’s compatible with the formatter. This way, users that just want to deny

def func(
    foo, bar, baz
):

can use it. Everyone else can keep using the rule as is, but they see the compatibility warning.

(Just to be clear, I think the current situation where COM812 is not compatible with the formatter is worse than a situation where COM812 doesn’t enforce comma in this case, so I think #10111 is an improvement. I’m just thinking if somehow we can have both).

As explained in flake8-commas and ruff doc, COM812 was created to reduce diff size and it’s working well. It’s approach is more aggressive than the current formatting style of ruff’s formatter which allow to group all arguments on a single line before exploding all arguments line by line.

In real world use cases, you might start with a few number of arguments and then add more afterwards. With COM812, you have two possible states:

  • the compact with everything on a single line
  • the diff friendly one, with everything on a separate line

Without COM812 and with ruff formatter, you get one more state in between:

  • a less compact state with the function name on the first line, all the arguments on the second line and the return annotation on the third

Each state transition is painful during code review because you need to read again everything to figure out what changed so COM812 really helps by reducing the number of states. I’m not trying to convince anyone to actually adopt this rule, but I simply emphasise why it’s important to keep it untouched because there are users who rely on it, as @charliermarsh mentioned.

As @MichaReiser pointed, this opinion is not compatible with the current formatter style. As a long time (convinced) user of COM812, this bothers me but I had the same problem with Black and the numerous benefits of ruff replacing black / isort / flake8 + having an uncompromising formatting style are worth it 🙂 For this reason, I’m going disable the rule on my projects.

Regarding what comes next, I have some questions:

  • while I see the clear benefits of having an uncompromising formatter, would it be acceptable in the future to have an option in the formatter to remove the “the less compact” in mention earlier ?
  • as of today, ruff’s formatting style seems deeply linked to Black style. Would it be possible in the future to suggest formatter style changes, similar to PEP ?

Thanks for all your explanations and for your time working on ruff, you keep saving us more and more time every day coding ❤️

Yeah, using the formatter and the linter together is great and very much intended. As you say, they solve different problems! I don’t have a solution here, but adding some more color from my perspective… The issue in this case is that this rule pre-dated the formatter – if the rule didn’t exist today, we likely wouldn’t add it to Ruff, since these kinds of linter-formatter conflicts create a lot of problems for users.

One answer would be to deprecate and remove the rule. But we’ve shied away from doing that, and instead we’ve:

  • Documented the rules that conflict, and warned against them.
  • Held back from adding new rules that cause linter-formatter conflicts.
  • In some cases, modified rules to make them non-conflicting.

Modifying the rule to allow Case (D) above would be a reasonable compromise, and would make it non-conflicting, but it’d probably also make it redundant, and my guess is that users of this rule actually like that it enforces that style.

I believe the formatter does a sensible job.

To solve the contradiction, COM812 should accept case “D” from above (for short enough lines, obviously). Isn’t that all there is to do?

(Or don’t I get the point of all the numerous cases from above? Frankly, I’ve never seen the other cases caused by the formatter.)

I use both the linter and the formatter, because they serve different jobs. I have all linter rules enabled, I try to make exceptions rare and file-based. Ruff is great for this, giving actionable feedback collected from years of investment of a significant number of people in the Python community.

A note aside, I try to stay away from --fix and also review changes made by the formatter. It makes sense more often than expected to take your time and come up with “creative solutions” that are easier to read and more intuitive from a Clean Code point of view. When those also make the formatter and linter happy, you’re done.

What’s the motivation for using both the formatter and COM812. Wouldn’t it be sufficient to only rely on the formatter that also fixes the issue automatically rather than having the lint rule enabled too? Or do you need COM812 enabled because you dislike the “all on one line” formatting that the formatter produces?

There are a few things I want to satisfy. The motivation for COM812 from the docs is

The presence of a trailing comma can reduce diff size when parameters or elements are added or removed from function calls, function definitions, literals, etc.

This is true in cases where there is one argument per line. So in those cases (one argument per line) I do want COM812 enforced.

Another constraint is I want arguments to be wrapped in some way when a single line like

def args(aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh):
    pass

is too long. But I would be happy with

def args(
    aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh
):
    pass

As a resolution to a line too long in the case that the arguments are too long to stand on one like with the function definition (def args( ... )) but short enough to stand on their own line. That is, I don’t think this case needs a trailing comma because I don’t think it shortens the diff.

So yes, I think I agree with @bittner that I don’t understand why this form violates COM812. Maybe COM812 should only be enforced when there is one argument per line. That way the trailing comma won’t be added by the linter after the re-formatting, thus inducing the formatter to again want to do more.

This wouldn’t even require a new option I don’t think. BUT if a new option is the way to go: Is it possible for LINTING rules to have optional configuration (other than inclusion or exclusion)? Or can only formatting rules be controlled by optional configuration?

I’m refactoring large code bases, and the case you quote above is particularly common as an outcome of reformatting.

def args(
    aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh
):
    ...

What the formatter does is indeed very natural and intuitive: When the line gets too long it first tries to put everything inside braces onto a line of its own. Shouldn’t COM812 accept this as a good case, in general? Kind of, “there are several arguments on a single line”, that’s a signal that it’s okay not to have a trailing comma.

The trailing-comma = "force" is probably just that. - Maybe "strict" vs. "forgiving" would reflect the intention even better, with "forgiving" as the default, probably.

I don’t think it’s currently possible based on our definition of “incompatible”. In short, we want to avoid cases in which running the formatter introduces new lint rule violations. In the case of COM812, you could have a function like:

def args(aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh):
    pass

Which the formatter may reformat as:

def args(
    aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh
):
    pass

Which would violate COM812. After adding the trailing comma to fix COM812, the formatter would then reformat again as:

def args(
    aaaaaaaa,
    bbbbbbbbb,
    cccccccccc,
    ddddddddd,
    eeeeeeee,
    ffffff,
    gggggggggggg,
    hhhh,
):
    pass

We would need a new trailing-comma setting in the formatter, like "force".

\cc @MichaReiser

I don’t care. I want formatter, linter and COM812 live together. And I want this:

def func(
    foo, bar, baz
):

formatted into this:

def func(
    foo, bar, baz,
):

I understand that there are programming languages that don’t support the trailing comma hence it’s not that common to have it when the arguments on one line, however, it looks incomplete and feels inconsistent with, let’s say,

def func(
    foo,
    bar,
    baz,
):

I understand that every programmer has their own style preferences that don’t necessarily adhere with mine and none of them is really the “correct” way to do stuff, however, I’d like the formatter to do stuff the way I like. If black style is not necessarily compatible with popular (like flake8-comma or some popular pylint defaults), then it’s wrong and it has to be configurable. People choose between black style and their own flake8 (or pylint) preferences, but in this case both the linter and the formatter are bundled with ruff.

For now, my Makefile has to look like this:

lint: $(VENV)
	$(PYTHON) -m ruff check src tests

format: $(VENV)
	$(PYTHON) -m ruff format src tests
	$(PYTHON) -m ruff check --fix src tests

My take is, the formatter does a decent job, also for people who really don’t want to compromise with COM812. If we had a configuration switch for the linter that would allow a less strict interpretation of COM812 (for accepting “the less compact” variant) wouldn’t we fix today’s situation, which would allow us to remove the conflict warning?

It would solve the conflict, but I don’t see the difference to disabling the lint and only use the formatter (except that the warning doesn’t show up by default when using --select ALL).

@AlexWaygood’s currently figuring out how we want to categorize Ruff’s rules in the future. This work also touches on if Ruff should support rules that are incompatible with the formatter (probably?) and, if so, how the user experience looks like.

That’s why I want to hold off from making any changes to COM812.

I can only reiterate that using both the formatter and the linter is not a strange idea. This issue/question is all about using both in a way that wouldn’t contradict each other.

The multi-line multi-parameter per line case is unusual. Black would never accept it. The single-line multi-parameter case is generated by the formatter, which is a behavior we have seen by Black.

We should get the linter not complain about the generated formatting. With or without a linter rule setting. The current state is not satisfactory. And suggesting to stop using both the formatter and the linter together is not satisfactory either.

Actually I generally don’t see much documentation about what changes I should expect from the formatter? Maybe I should check documentation for black?

Yeah, we might have to write up the rules eventually. For now, Black’s style guide also applies for Ruff and it explains how it breaks lines here.

To summarize, I think status quo is ok for me, but better documentation would be helpful. Especially in light of the fact that the multi-parameters per line not shared with def is kind of a funny edge case in my view.

That makes sense to me. It may also be worth improving the warning message with a clearer recommendation to disable COM812 when using the formatter because it is superfluous (except you disagree with the formatter’s style but want to use the formatter regardless, which is unlikely a case we want to support).

But I think there’s a case where someone who DOESN’T use the formatter might want something like G) above.

Agree and thanks for the detailed explanation. Changing COM812 to make the trailing comma optional makes it less useful for users who don’t use the formatter.

The next steps are:

  • Improve deprecation warning
  • Improve the rule’s documentation related to formatter compatibility

@MichaReiser ok, I think the suggestion to disable COM812 and just use the formatter will indeed give me outputs that are satisfactory. I think the expected behaviors are a bit confusing though and the documentation wasn’t very clarifying for me. Actually I generally don’t see much documentation about what changes I should expect from the formatter? Maybe I should check documentation for black?

In any case, it seems like the formatter will:

(1) If the line is too long to have params/args share with def then it will (2) try to put all parameters on a single line after def( and before ): on a third line. It does this WITHOUT a trailing comma (3) If the line is still too long then it breaks out each param/arg onto its own line WITH a trailing comma

Under my current workflow I will never get output formatted like (2) because of the lack of trailing comma. But if I were to disable the lint rule COM812 I would sometimes get output like (2). That’s ok with me, I don’t have anything against (2).

So for me I would be happy to disable COM812. The reason I didn’t want to do that was I thought it would allow output like

def args(
    aaaaaaaa, 
    bbbbbbbbb, 
    cccccccccc, 
    ddddddddd, 
    eeeeeeee, 
    ffffff, 
    gggggggggggg, 
    hhhh, 
    iiii
):
    pass

without a trailing comma. That is, since COM812 was disabled I didn’t expect anything to be there to enforce the presence of the trailing comma. But, it seems the formatter on its own already enforces the trailing comma in this case.

I would be helped by more clear documentation pointing out that running the formatter will already enforce trailing commas in the single param/arg per line format. Though the gotcha edge case here, as @bluetech highlights, is that the formatter will NOT enforce the trailing comma in the multiple params on one line not shared with def case. This is ok from a VCS perspective since if parameters are added the signature would be totally reformatted to the single param per line style. This is ok if you’re totally relying on the formatter though because you’ll never be outside one of the three possible cases.

@bluetech indicates that they want the trailing comma so that they can extend multi-params not shared with def onto more than one line. But in this case they are operating outside the rules of the formatter so they should not use the formatter. In this case I think COM812 alone would suffice.


To summarize, I think status quo is ok for me, but better documentation would be helpful. Especially in light of the fact that the multi-parameters per line not shared with def is kind of a funny edge case in my view.

@MichaReiser really appreciate your detailed consideration of this issue.

Though I admit to being a bit confused at the outcome (and in my previous comments I was confused myself):

  • The current situation is that COM812 and formatter are officially incompatible, possibly this even becomes a hard error in the future.

  • This issue asks them to not be incompatible. If I understand the reasoning from @jagerber48 and @bittner (and myself) correctly, the reason why we want COM812 + formatter is to avoid the “SINGLE LINE, DON’T SHARE WITH DEF” case.

  • The PR makes COM812 + formatter compatible, by making COM812 not complain about the “SINGLE LINE, DON’T SHARE WITH DEF” case.

It therefore seems to that the PR wouldn’t actually satisfy what’s being requested in this issue, and actually make the situation worse than the current situation for people not using the formatter (because they want a comma there).

Really sorry if I’m misunderstanding something and just wasting your time…

Like, would you write this as:

Yes, though of course with tab -> 4 spaces, and a trailing comma on iiii, that is

def args(
    aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh, 
    iiii,
):
    pass

though realistically when this happens I prefer to explode.

because it’s missing the trailing comma, and that means adding a new param will change two lines instead of one, and avoiding this is the main purpose of COM812.

Can you explain how you add the parameter that it changes two lines? The formatting I’m thinking of either change one line or all parameter lines.

I would expect this to either become:

def args(
    aaaaaaaa, bbbbbbbbb, cccccccccc, ddddddddd, eeeeeeee, ffffff, gggggggggggg, hhhh, iiii
):
    pass

in which case it only changes one line or

def args(
    aaaaaaaa, 
	bbbbbbbbb, 
	cccccccccc, 
	ddddddddd, 
	eeeeeeee, 
	ffffff, 
	gggggggggggg, 
	hhhh,
):
    pass

In which case it changes all lines.

Interesting, the existing rule isn’t able to catch all cases where I would suspect a trailing comma to be required because it only tests if there’s a newline between the last token and the closing parentheses:

E.g. the following is okay

def call(a, b,
    c): pass