Conversation
153c3a8 to
106c0e3
Compare
|
Personally, i find it easier to add fixtures first so i can directly see what my code currently does, with fixtures in the PR it is also easier to give guidance since i can directly see how it currently behaves (I usually run |
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinuxWindows |
| ... | ||
| # before except 2 | ||
| except ( | ||
| KeyError # except line 2 |
There was a problem hiding this comment.
as far as i can see this is currently the only misplaced comment, right?
black also seems to remove the parentheses here even though it otherwise doesn't
There was a problem hiding this comment.
comment placement looks ok to me here (except line 2)
but i don't understand what causes us to break the (KeyError) over multiple lines
There was a problem hiding this comment.
note that this is just a call to .format(), on a tuple of types, nothing manual here
https://github.com/astral-sh/ruff/pull/5222/files/ccd96e6ae3d7e38b833f530d99f5d0b9d46b35ef#diff-30bdf42955f6a58b1f41492d192056475d73d2d27906cb200ba4beba5d010d75R39
There was a problem hiding this comment.
black format
try:
...
except (KeyError) as key: # except line 2
...as
try:
...
except KeyError as key: # except line 2
...which looks better to me. This is another one of the rare cases where black removes parentheses
There was a problem hiding this comment.
i agree that it's better to remove them. but i'm not sure what is preserving them (i was wrong above, here it isn't a tuple but just an ExprName)
There was a problem hiding this comment.
You might need to change the attachment of # except line 2 in placement.rs, either attach it trailing to a node that doesn't break the line if it doesn't need to or mark it as dangling and manually format after the colon. we're doing the latter (formatting after : manually) in some other nodes
There was a problem hiding this comment.
Yeah... The fact that there's no explicit node for bodies really makes things annoying. The issue here is that the except line 2 comment falls into the middle of the ExceptHandlerExceptHandler node (it is inside of that node's range). The last preceding node is the KeyError because key (Identifier) is not an AST node (it probably should, but it is not 😢).
We probably need something similar to (or extend)
ruff/crates/ruff_python_formatter/src/comments/placement.rs
Lines 551 to 554 in 8a73a7b
just to clarify: did you look at the pr before i committed the fixtures (a bit late in the process) or are you suggesting committing them first (even before any code changes?) in a separate commit to then see changes as the impl is added? |
...thon_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__try_py.snap
Outdated
Show resolved
Hide resolved
|
til about very helpful, have been doing lots of this manually with |
No my idea was just to put code you tested in the fixtures so other people can also see them when looking at your PR. We squash merge PRs, so commit order or structure is not important for ruff PRs. Personally, i try to skim external contributor draft PRs once they are created and comment if i see something, and add a thorough review once the PR is marked as ready. (You can of course always ping me or others on draft PRs if you have specific questions) |
are you still missing something i've referenced but not included here (in if this is about timing, makes sense, i'll try to commit my scratch into a fixture sooner |
|
The |
ccd96e6 to
5976bd5
Compare
| ... | ||
| # before except 2 | ||
| except ( | ||
| KeyError # except line 2 |
There was a problem hiding this comment.
You might need to change the attachment of # except line 2 in placement.rs, either attach it trailing to a node that doesn't break the line if it doesn't need to or mark it as dangling and manually format after the colon. we're doing the latter (formatting after : manually) in some other nodes
| } | ||
| } | ||
|
|
||
| leading_node_comments(except_handler_except_handler).fmt(f)?; |
There was a problem hiding this comment.
nit: can you merge those into the write call below?
MichaReiser
left a comment
There was a problem hiding this comment.
This looks really good. I putting this back in your queue, mainly to move the ExceptHandler formatting to FormatExceptHandlerExceptHandler.
Feel free to ignore the incorrectly placed comment for now and instead add a TODO. We can tackle this as a separate pR
| ... | ||
| # before except 2 | ||
| except ( | ||
| KeyError # except line 2 |
There was a problem hiding this comment.
Yeah... The fact that there's no explicit node for bodies really makes things annoying. The issue here is that the except line 2 comment falls into the middle of the ExceptHandlerExceptHandler node (it is inside of that node's range). The last preceding node is the KeyError because key (Identifier) is not an AST node (it probably should, but it is not 😢).
We probably need something similar to (or extend)
ruff/crates/ruff_python_formatter/src/comments/placement.rs
Lines 551 to 554 in 8a73a7b
|
thanks for the comments, will see how i get on. while waiting for them, i also tried to see if i could re-use this code for annoyance: since their contents is the same i tried destructing the item and then passing the components to a helper function to do the work, but when borrowing them across the function barrier i had e.g. blocker: the |
One pattern that worked well in the past is to either:
I don't have a strong preference for either.
You can implement See |
9ea0e06 to
8d74f49
Compare
968726f to
cfe8ac1
Compare
|
I'm so impressed by how you tackle this. You're probably working on the most complicated statement! |
Thanks, and again, thanks for your patience and help! |
|
|
||
| return CommentPlacement::dangling(comment.enclosing_node(), comment); | ||
| } | ||
|
|
There was a problem hiding this comment.
would probably be a good idea to have some tests for these functions. not sure what we have available to make tests ergonomic. will have a look around, but you might have good ideas already
There was a problem hiding this comment.
We have some snapshot tests in the comments/mod.rs file, that test if the comments get associated with the right node. But they are quiet similar to the formatting snapshot tests.
ruff/crates/ruff_python_formatter/src/comments/mod.rs
Lines 587 to 608 in d3d69a0
I plan to review your PR on Monday.
There was a problem hiding this comment.
yes i did find those and tried to (ab?)use them to generate input i could pass to the functions here like handle_trailing_end_of_line_except_comment but i failed.
There was a problem hiding this comment.
Yeah. It is testing the whole place_comment and not only handle_treiling_end_of_line_except_comment. This is intentional because the construction of the DecoratedComment is very specific and easy to get wrong when constructing by hand (and we want to make sure that changes the DecoratedComment construction resulting in a failure of the except comment placement are catched too). But you can tailor your source code so that it calls your function with the arguments that you want to test.
MichaReiser
left a comment
There was a problem hiding this comment.
This looks good to me except that I think that some comment placement is incorrect. I'll leave it up to you if you want to:
- Split the comment placement fixes into its own PR
- Follow up as part of this PR
279e162 to
66405db
Compare
|
@davidszotten is this PR ready for merging from your side(besides the merge conflicts?). I lost the overview. |
still working on comments
i think there are still issues with blank lines between the sections
seems to fix some other cases but not `except (KeyError): # comment`
…h_comments TODO: now misses a blank newline before a leading comment to the first except block
refactor `handle_in_between_except_handlers_or_except_handler_and_else_or_finally_comment` with early returns to avoid very nested indentation and add a check for subsequent except handlers to attach comments to (as leading) instead of leaving as dangling on the try for else/finally
66405db to
52f90fe
Compare
#5222 (comment) and #5222 (comment) are waiting for your replies i think |
|
🎉 thanks |
|
Thank you! |
Format
StmtTry