-
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
Ignore the open parentheses when measuring BestFitsParenthesize
#8940
Conversation
Current dependencies on/for this PR: This stack of pull requests is managed by Graphite. |
- 3 | ||
- ) # type: int | ||
+ another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = 3 # type: int | ||
+ 3 # type: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The style here is different because black doesn't inline the type:
comment. Changing the comment to e.g. ctype
matches Ruff's formatting
@@ -123,14 +123,16 @@ def f( | |||
-z: Short | Short2 | Short3 | Short4 = 8 | |||
-z: int = 2.3 | |||
-z: int = foo() | |||
+z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = 7 | |||
+z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires other preview styles to match black
) | ||
@@ -35,10 +29,8 @@ | ||
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires other preview styles to match Black
@@ -165,7 +165,9 @@ def foo(): | |||
yield (e) # Too many parentheses | |||
# comment | |||
|
|||
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in l: | |||
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't match black although black's formatting here is very peculiar
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in (l):
pass
It ends parenthesizing l
but without splitting the line. IMO, both styles are not ideal but I prefer ours for consistency.
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = ( | ||
int | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a divergence from Black. Black doesn't parenthesize the right side. I don't see why and recommend keeping it because I don't see why type aliases should be different from other assignments.
|
cf3fa1c
to
d50fa88
Compare
Hmm, obviously, it is more complicated than this 🤯 Black does not parenthesize the right side here (for reasons?) this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function(arg1, arg2, arg3)
) Although this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
arg1, arg2, arg3
) Ultimately, the vertical spacing is about the same, although Black's layout requires considerably more vertical spacing. However, it avoids one pair of parentheses, which is something Black generally optimizes for. Which makes me wonder what their rule is. |
d50fa88
to
d013b71
Compare
Okay this is interesting. Black does the exact opposite if the right hand side is a function this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_i = (
function([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
) The I compared the similarity index for Poetry using either layout, and it remained unchanged. This feels edge-casey enough that I feel it isn't worth spending too much time on. However, I'm undecided which version I prefer:
Would love to hear some more opinions on this |
7d948b2
to
8fbb3c8
Compare
Summary
This PR changes the definition of
BestFitParenthesize
to only consider whether its content fits when parenthesizing and not whether the whole parenthesized expression fits.The difference is subtle but relevant when what comes before
BestFitParenthesize
already exceeds the configured line width:The
this_is_a...
exceeds the configured line width. The existing implementation doesn't parenthesizeaaaaa
because the opening(
doesn't fit on the line. This is unfortunate, because parenthesizing would makeaaaaa
fit in the configured line width and is what Black does:This PR implements Black's behavior by ignoring whether there's sufficient space to place the
(
and instead only measures whether its content (aaaaaa
) fits when indenting.Test Plan
The similarity index remains unchanged.