-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Keep trailing part comment associated with its part #15378
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,3 +291,83 @@ | |
f"testeeeeeeeeeeeeeeeeeeeeeeeee{a | ||
=}" "moreeeeeeeeeeeeeeeeeetest" # comment | ||
) | ||
|
||
|
||
# Trailing last-part comments | ||
|
||
a = ( | ||
"a" | ||
"b" # belongs to `b` | ||
) | ||
|
||
a: Literal[str] = ( | ||
"a" | ||
"b" # belongs to `b` | ||
) | ||
|
||
a += ( | ||
"a" | ||
"b" # belongs to `b` | ||
) | ||
|
||
a = ( | ||
r"a" | ||
r"b" # belongs to `b` | ||
) | ||
|
||
a = ( | ||
"a" | ||
"b" | ||
) # belongs to the assignment | ||
|
||
a = ((( | ||
"a" | ||
"b" # belongs to `b` | ||
))) | ||
|
||
a = ((( | ||
"a" | ||
"b" | ||
) # belongs to the f-string expression | ||
)) | ||
|
||
a = ( | ||
"a" "b" # belongs to the f-string expression | ||
) | ||
|
||
a = ( | ||
"a" "b" | ||
# belongs to the f-string expression | ||
) | ||
|
||
a = ( | ||
"a" | ||
"b" "c" # belongs to the f-string expression | ||
) | ||
|
||
logger.error( | ||
f"Failed to run task {task} for job" | ||
f"with id {str(job.id)}" # type: ignore[union-attr] | ||
) | ||
|
||
a = (10 + | ||
"Exception in {call_back_name} when handling msg on " | ||
f"'{msg.topic}': '{msg.payload}'" # belongs to binary operation | ||
) | ||
|
||
a = 10 + ( | ||
"Exception in {call_back_name} when handling msg on " | ||
f"'{msg.topic}': '{msg.payload}'" # belongs to last-part | ||
) | ||
Comment on lines
+348
to
+361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful to shorten these strings (similar to other test cases) just to make sure that the formatter isn't going to collapse them because I think otherwise the line length won't even allow this to be tested. |
||
|
||
self._attr_unique_id = ( | ||
f"{self._device.temperature.group_address_state}_" | ||
f"{self._device.target_temperature.group_address_state}_" | ||
f"{self._device.target_temperature.group_address}_" | ||
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001 | ||
) | ||
|
||
return ( | ||
f"Exception in {call_back_name} when handling msg on " | ||
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
use std::cmp::Ordering; | ||
|
||
use ast::helpers::comment_indentation_after; | ||
use ruff_python_ast::whitespace::indentation; | ||
use ruff_python_ast::{ | ||
self as ast, AnyNodeRef, Comprehension, Expr, ModModule, Parameter, Parameters, | ||
self as ast, AnyNodeRef, Comprehension, Expr, ModModule, Parameter, Parameters, StringLike, | ||
}; | ||
use ruff_python_trivia::{ | ||
find_only_token_in_range, first_non_trivia_token, indentation_at_offset, BackwardsTokenizer, | ||
CommentRanges, SimpleToken, SimpleTokenKind, SimpleTokenizer, | ||
}; | ||
use ruff_source_file::LineRanges; | ||
use ruff_text_size::{Ranged, TextLen, TextRange}; | ||
use std::cmp::Ordering; | ||
|
||
use crate::comments::visitor::{CommentPlacement, DecoratedComment}; | ||
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection}; | ||
use crate::expression::parentheses::is_expression_parenthesized; | ||
use crate::other::parameters::{ | ||
assign_argument_separator_comment_placement, find_parameter_separators, | ||
}; | ||
|
@@ -355,6 +355,41 @@ fn handle_enclosed_comment<'a>( | |
AnyNodeRef::ExprGenerator(generator) if generator.parenthesized => { | ||
handle_bracketed_end_of_line_comment(comment, source) | ||
} | ||
AnyNodeRef::StmtReturn(_) => { | ||
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to cover other statements like raise Exception(
"a"
"b" # belongs to `b`
)
assert False, (
"a"
"b" # belongs to `b`
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, only statements that use |
||
} | ||
AnyNodeRef::StmtAssign(assignment) | ||
if comment.preceding_node().is_some_and(|preceding| { | ||
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value)) | ||
}) => | ||
{ | ||
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source) | ||
} | ||
AnyNodeRef::StmtAnnAssign(assignment) | ||
if comment.preceding_node().is_some_and(|preceding| { | ||
assignment | ||
.value | ||
.as_deref() | ||
.is_some_and(|value| preceding.ptr_eq(value.into())) | ||
}) => | ||
{ | ||
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source) | ||
} | ||
AnyNodeRef::StmtAugAssign(assignment) | ||
if comment.preceding_node().is_some_and(|preceding| { | ||
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value)) | ||
}) => | ||
{ | ||
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source) | ||
} | ||
AnyNodeRef::StmtTypeAlias(assignment) | ||
if comment.preceding_node().is_some_and(|preceding| { | ||
preceding.ptr_eq(AnyNodeRef::from(&*assignment.value)) | ||
}) => | ||
{ | ||
handle_trailing_implicit_concatenated_string_comment(comment, comment_ranges, source) | ||
} | ||
|
||
_ => CommentPlacement::Default(comment), | ||
} | ||
} | ||
|
@@ -2086,6 +2121,75 @@ fn handle_comprehension_comment<'a>( | |
CommentPlacement::Default(comment) | ||
} | ||
|
||
/// Handle end-of-line comments for parenthesized implicitly concatenated strings when used in | ||
/// a `FormatStatementLastExpression` context: | ||
/// | ||
/// ```python | ||
/// a = ( | ||
/// "a" | ||
/// "b" | ||
/// "c" # comment | ||
/// ) | ||
/// ``` | ||
/// | ||
/// `# comment` is a trailing comment of the last part and not a trailing comment of the entire f-string. | ||
/// Associating the comment with the last part is important or the assignment formatting might move | ||
/// the comment at the end of the assignment, making it impossible to suppress an error for the last part. | ||
/// | ||
/// On the other hand, `# comment` is a trailing end-of-line f-string comment for: | ||
/// | ||
/// ```python | ||
/// a = ( | ||
/// "a" "b" "c" # comment | ||
/// ) | ||
/// | ||
/// a = ( | ||
/// "a" | ||
/// "b" | ||
/// "c" | ||
/// ) # comment | ||
/// ``` | ||
/// | ||
/// Associating the comment with the f-string is desired in those cases because it allows | ||
/// joining the string literals into a single string literal if it fits on the line. | ||
fn handle_trailing_implicit_concatenated_string_comment<'a>( | ||
comment: DecoratedComment<'a>, | ||
comment_ranges: &CommentRanges, | ||
source: &str, | ||
) -> CommentPlacement<'a> { | ||
if !comment.line_position().is_end_of_line() { | ||
return CommentPlacement::Default(comment); | ||
} | ||
|
||
let Some(string_like) = comment | ||
.preceding_node() | ||
.and_then(|preceding| StringLike::try_from(preceding).ok()) | ||
else { | ||
return CommentPlacement::Default(comment); | ||
}; | ||
|
||
let mut parts = string_like.parts(); | ||
|
||
let (Some(last), Some(second_last)) = (parts.next_back(), parts.next_back()) else { | ||
return CommentPlacement::Default(comment); | ||
}; | ||
|
||
if source.contains_line_break(TextRange::new(second_last.end(), last.start())) | ||
&& is_expression_parenthesized(string_like.as_expression_ref(), comment_ranges, source) | ||
{ | ||
let range = TextRange::new(last.end(), comment.start()); | ||
|
||
if !SimpleTokenizer::new(source, range) | ||
.skip_trivia() | ||
.any(|token| token.kind() == SimpleTokenKind::RParen) | ||
{ | ||
return CommentPlacement::trailing(AnyNodeRef::from(last), comment); | ||
} | ||
} | ||
|
||
CommentPlacement::Default(comment) | ||
} | ||
|
||
/// Returns `true` if the parameters are parenthesized (as in a function definition), or `false` if | ||
/// not (as in a lambda). | ||
fn are_parameters_parenthesized(parameters: &Parameters, contents: &str) -> bool { | ||
|
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 understand the change but I want to confirm as to why does this comment belongs to the f-string expression. I think the reason is that without that, the formatting might move the last part of the string to it's own line along with the comment which means the comment won't be applicable for the last second part. Is that correct?
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 an edge case where there's no "right" solution because the formatter can't tell if the comment belongs to the middle part, the last part, or the entire f-string.
That's why I decided to associate it with the f-string. This matches the default behavior where we associate a comment with the outer most node that ends right before it. We could also decide to associate it with the last part, if we think this is more appropriate.