-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Measuring LineSuffix
when formatting to include trailing comments
#5630
Comments
Yes, this is a generic problem applying to all trailing comments. The reason is that the I don't mind changing the implementation, depending on its performance impact. But I think that this can also be a reasonable deviation. The relevant line where we skip over line suffixes is here: ruff/crates/ruff_formatter/src/printer/mod.rs Lines 218 to 220 in 56bae34
I would probably add a new The main challenge is how to compute the width without actually printing the content. I guess, we could call into fits and then take the width from there but that feels like a hack. |
tuple
s with trailing comments exceeding line-lengthLineSuffix
when formatting to include trailing comments
Does it simplify the problem at all if we constrain the kinds of nodes that can be printed as line suffixes? (Do we ever need line suffix apart from for comments?) |
I can answer those questions myself. The bigger question is probably whether we want to deviate from Black here or fix this. |
I think linting influences some of my formatting decisions. So if my understanding is that line-length violations include trailing comments as part of the line suffix calculation, then naturally I'll probably expect the formatting to align with that constraint. In other words, I currently lean towards how |
That makes sense to me. But this raises the question of whether the formatting must be consistent with all lint rules Ruff supports. I don't think this is a desired property of the formatter because it would limit our options when choosing a formatting, because we could only design for the least common denominator. I'm leaning towards automatically disabling rules that we know conflict with the formatter (you shouldn't need formatting-related rules if you use our formatter).
The question is, what are comments ;) We need to support text and hard line breaks at least, possibly labels too. I don't see this as a high risk issue. I'm confident that we can support it and would prefer to delay working on it until we're further along with the formatter. However, we'll have to be extra careful to:
|
Regarding the consistency with the lint rule issue. I would recommend disabling the rule if someone uses our formatter, similar to what Prettier's eslint plugin does. There are cases where it is known that Black doesn't break your line (it may even not be allowed to break in these positions) and requiring a suppression comment in that case only makes things worse (to add on top of that, the formatter might move your comment, because the line is too long 😆) |
One good counter example of why Ruff should respect the line width that @zanieb made: if (
first_test()
and not settings.TEST_SUITE
): # nocoverage: We can't verify the signature in test suite since we fetch the events
pass If I deliberately formatted the comment on its own line for better readability, then the formatter shouldn't collapse the line and make the comment exceed the line width. |
One "simple" implementation idea: Add a @cnpryer would you be interested in implementing this? |
I promise I'll stop my spam after this. After discussing this internally. We want to align with black because we believe that respecting the line width improves the readability of code, this includes comments. There are examples of where Black/Ruff split the line weirdly if it has a long trailing comments. Comments may more likely trigger these problems but aren't limited to them. Let's take the subscript example: a[0] # a very long comment that exceeds the line width and makes the subscript split eventually
# Black
a[
0
] # a very long comment that exceeds the line width and makes the subscript split eventually This is worse formatting, in my view, but it isn't specific to comments: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[4]
# Ruff
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
4
] It's less likely that you trigger the second case, but the proper solution IMO is to never break subscripts if indexing by a number literal. This solves the odd formatting in both cases. |
I would, but I want to spend some time today looking at this again (and thinking about it) before I claim it. I've got several things I'm trying to juggle right now, so my main concern is that causing any delay on the formatter's progress. Off the bat I'm struggling to wrap my head around the client code impact (needing
Are you thinking about tossing this in the Alpha bucket? Or would it be Beta territory? |
Ah I see it's in Alpha tracked by #6069 |
Don't feel pressured by it. It's ready when it's ready.
The client code will need to compute the width of a comment. This means we'll have to duplicate some (or all of) ruff/crates/ruff_formatter/src/printer/mod.rs Lines 1278 to 1298 in 910dbbd
Specifically, the width of a tab is configurable (is a tab 2 or 4 spaces)?.
Totally up to you. I tagged you because you showed interest in working on the Printer, but I also don't want to pressure you. |
Okay, I lied... I have one other observation to share. Pyink excludes trailing pragma comments from the computed width. What's nice about the IR change we've been discussing is that we could support this as well, by simply setting the width to 0 if it is a pragma comment. |
Started looking into it this morning. I'd say leave this as unassigned in case someone wants to leapfrog me. At some point I'll have more time, and when that happens I'd be more comfortable claiming issues. |
This comment was marked as outdated.
This comment was marked as outdated.
i was expecting this to also fix (example from django) ( -UNUSABLE_PASSWORD_SUFFIX_LENGTH = 40 # number of random chars to add after UNUSABLE_PASSWORD_PREFIX
+UNUSABLE_PASSWORD_SUFFIX_LENGTH = (
+ 40 # number of random chars to add after UNUSABLE_PASSWORD_PREFIX
+) did i misunderstand something? |
Yes and no. This is related to #6872 The current implementation uses an heuristic of when to use this layout because it is expensive. ruff/crates/ruff_python_formatter/src/expression/parentheses.rs Lines 38 to 50 in fc89976
We could extend the heuristic to take trailing end of line comments into consideration (use it if the node has any trailing end of line comments). |
thanks for the explanation. your suggestion seems worth a try so started having a look. however, e.g. in the example above we call |
Hmm good point. You get the parent in |
hm. parent improves the django example but doesn't quite fix it UNUSABLE_PASSWORD_SUFFIX_LENGTH = (
- 40
-) # number of random chars to add after UNUSABLE_PASSWORD_PREFIX
+ 40 # number of random chars to add after UNUSABLE_PASSWORD_PREFIX
+) we now end up moving the comment |
Do I understand correctly that it now gets moved inside of the parentheses? That's rather unexpected. |
sorry for being unclear. it's the other way around input:
black
ruff:
oh, and starting with the black version as input, my ruff impl is unstable :( |
made a pr so we have code to discuss and a better place for this discussion #7023 |
In #5169 I'm playing with test cases and noticed
tuple
s with trailing comments exceeding line-length don't get formatted.I added this to the
ruff
fixtures fortuple
formatting:With
black
you'll get:Here's the modified snapshot results:
If I can scoop this up in #5169 I'll submit a separate PR, but I figured I could start by documenting this here.
UPDATE: Looking into it more this looks like a general trailing comment issue rather than specifically a
tuple
issue. You can repro withdict
using:The text was updated successfully, but these errors were encountered: