-
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
Fix format named expr comments #5705
Fix format named expr comments #5705
Conversation
let leading_value_comments = comments.leading_comments(value.as_ref()); | ||
let dangling_item_comments = comments.dangling_comments(item); | ||
|
||
if trailing_target_comments.is_empty() { |
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.
i tried making a single write!
call with these if
s inside, but that doesn't work since space()
and soft_line_break
have different types. is there some trick to make this work? (or do we prefer multiple write
calls?
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.
There's format_with
but personally i like this style better, multiple write calls are fine here, if required we can still optimize later
e707349
to
982b712
Compare
let leading_value_comments = comments.leading_comments(value.as_ref()); | ||
let dangling_item_comments = comments.dangling_comments(item); | ||
|
||
if trailing_target_comments.is_empty() { |
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.
There's format_with
but personally i like this style better, multiple write calls are fine here, if required we can still optimize later
if trailing_target_comments.is_empty() { | ||
write!(f, [space()])?; | ||
} else { | ||
write!(f, [soft_line_break()])?; |
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.
What's the reason for this being a soft line break?
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.
as opposed to?
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.
a hard_line_break
, since as far as i can tell we always want to break after the comments if there are any. I realized it doesn't make a difference here because a trailing comment will expand the group, but it might be easier to read
write!(f, [text(":="), dangling_comments(dangling_item_comments)])?; | ||
|
||
if leading_value_comments.is_empty() { | ||
write!(f, [space()])?; |
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.
What happens when there are dangling_item_comments but no leading leading_value_comments? this would make a good test case
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.
good catch. definitely something not what i expected. will continue investigating
@@ -185,6 +185,10 @@ pub(crate) enum TokenKind { | |||
/// `.`. | |||
Dot, | |||
|
|||
/// `:=`. | |||
/// TODO: name? |
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.
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
pass | ||
... | ||
|
||
# should not add brackets |
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.
Hey. Just wanted to let you know that I'm working on changing NeedsParentheses
to pass through the parent node. This should make it straightforward to implement the correct parentheses logic for the walrus operator.
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.
👍
atm i can't even figure out what's causing these parens to be added. parenthesize gets set to "ifbreaks" and nothing is breaking...
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 PR ships the infrastructure: #5708
The parentheses are added because FormatExpr
first calls into NeedsParentheses
to determine if parentheses are necessary
let parentheses = item.needs_parentheses(self.parenthesize, f.context()); |
And the implementation of NamedExpr
returns Always
ruff/crates/ruff_python_formatter/src/expression/expr_named_expr.rs
Lines 39 to 44 in 33a9177
match default_expression_needs_parentheses(self.into(), parenthesize, context) { | |
// Unlike tuples, named expression parentheses are not part of the range even when | |
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details. | |
Parentheses::Optional => Parentheses::Always, | |
parentheses => parentheses, | |
} |
We should implement the same rules as in https://github.com/psf/black/blob/d1248ca9beaf0ba526d265f4108836d89cf551b7/src/black/linegen.py#L1381-L1395 (requires access to the parent node)
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.
aah. thanks
do i understand correctly that his is separate to (i.e. won't help with) parens for generator comprehensions (that we were discussing yesterday)?
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.
That's correct. The problem is related but needs a separate fix (and I first need to figure out what even the expected output is... I have suspicion I know how it's supposed to behave but I haven't verified it yet)
I suspect this can now be closed as the originating issue is resolved. |
fixes #5695