Conversation
There was a problem hiding this comment.
Yeah, you can ignore that issue. This is related to expression formatting.
It would be great if you could add a few more tests with comments in different positions to see that this doesn't cause formatting errors:
raise (
# comment
a
) from (
b
# comment
)
| if let Some(value) = exc { | ||
| write!( | ||
| f, | ||
| [space(), value.format().with_options(Parenthesize::Optional)] |
There was a problem hiding this comment.
I think we'll need Parenthesize::IfBreaks here to support
raise aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd(Yes, this is a very made up example)
There was a problem hiding this comment.
I was using Parenthesize::IfBreaks at first but then I noticed that with black:
# input
raise (aaaaa.aaa(a).a) from (aksjdhflsakhdflkjsadlfajkslhf)
raise bbbbb.bbb(bb) from c
# black output
raise (aaaaa.aaa(a).a) from (aksjdhflsakhdflkjsadlfajkslhf)
raise bbbbb.bbb(bb) from c
# ruff (IfBreak) ouput
raise aaaaa.aaa(a).a from aksjdhflsakhdflkjsadlfajkslhf
raise bbbbb.bbb(bb) from cAnd I found that Parenthesize::Optional should work if I understand the doc correctly. And I got:
# ruff (Optional) output
raise (aaaaa.aaa(a).a) from (aksjdhflsakhdflkjsadlfajkslhf)
raise bbbbb.bbb(bb) from cThere was a problem hiding this comment.
But with your exemple I found another issue:
Black does not break if there is no parentheses but ruff does.
# input
raise aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
raise (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd)
# black output
raise aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
raise (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccc
+ ddddddddddddddddddddddddd
)
# ruff (Optional) ouput
raise (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccc
+ ddddddddddddddddddddddddd
)
raise (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccc
+ ddddddddddddddddddddddddd
)And Parenthesize::Preserve does not work.
Is there something like: if in parenthesis then format else don't.
There was a problem hiding this comment.
I find it weird that black does not format these in the way that ruff does in the examples you shared 🤔
There was a problem hiding this comment.
I found the same behaviour with the lambda body:
# input
lambda a: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
lambda a: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + (cccccccccccccccccccccc + ddddddddddddddddddddddddd)
lambda a: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd)
# black output
lambda a: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
lambda a: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + (
cccccccccccccccccccccc + ddddddddddddddddddddddddd
)
lambda a: (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccc
+ ddddddddddddddddddddddddd
)There was a problem hiding this comment.
and the yield value:
# input
yield aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
yield aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + (cccccccccccccccccccccc + ddddddddddddddddddddddddd)
yield (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd)
# black output
yield aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccc + ddddddddddddddddddddddddd
yield aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbb + (
cccccccccccccccccccccc + ddddddddddddddddddddddddd
)
yield (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccc
+ ddddddddddddddddddddddddd
)There was a problem hiding this comment.
Sorry, I confused Parentheses::Optional and Parentheses::Preserve. Optional should be fine here. I find black's behavior surprising. Either way, this is something you can ignore for your PR, because it affects all expression formatting.
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinuxWindows |
|
I was looking at the same formatting of Black in other statements, I found The Feel free to close the issue if the diff with black is not acceptable. I will continue to read the code but don't except me to find a solution. Love what you are doing, such an amazing work! Sorry for the inconvenience. |
breaks formatting stability
Break format stability
|
I found some issues with stability, and I don't know how to solve it. Input: raise ( # some comment
# another one
)First format: raise (
# some comment
# another one
)Second format: raise (
# some comment
# another one
) |
@konstin what do you think. Should we make the trailing comment a leading comment of the |
|
i don't have a good intuition, i'm fine with merging whatever is stable. Function call e.g. currently format a = x( # b
# c
)to a = x() # b
# cI think we should add an empty parentheses comment util, unless someone else has specific ideas, i'll prototype something today or tomorrow that keeps both kinds of comments where they are for general empty parentheses |
|
I found the stability issue, it was in the tuple formatting implementation. I pushed a simple fix but if you want I can open a separate PR for the fix. I copied the |
thank you! i'm fine with merging it with this PR with this fix In general, is this PR ready to be merged? |
Yes I'm putting this PR in ready status. I also found the same issue with |
thanks, that would be great! |
Summary
This PR implements the formatting of
raisestatements. I haven't looked at the black implementation, this is inspired from from thereturnstatements formatting.Test Plan
The black differences with insta.
I also compared manually some edge cases with very long string and call chaining and it seems to do the same formatting as black.
There is one issue:
But I'm not sure this diff is the raise formatting implementation.