Skip to content
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

Avoid inserting trailing commas within f-strings #8574

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
4,
)

foo = 3,
foo = 3,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My editor is stripping this.


class A(object):
foo = 3
Expand Down Expand Up @@ -639,3 +639,10 @@ def foo(
:20
],
)

# Make sure we don't insert commas within f-strings.
f"""This is a test. {
"Another sentence."
if True else
"Alternative route!"
}"""
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,24 @@ pub(crate) fn trailing_commas(
tokens: &[LexResult],
locator: &Locator,
) {
let mut fstrings = 0u32;
let tokens = tokens
.iter()
.flatten()
// Completely ignore comments -- they just interfere with the logic.
.filter(|&r| !matches!(r, (Tok::Comment(_), _)))
.filter(|(tok, _)| match tok {
// Completely ignore comments -- they just interfere with the logic.
Tok::Comment(_) => false,
// Ignore content within f-strings.
Tok::FStringStart => {
fstrings = fstrings.saturating_add(1);
false
}
Tok::FStringEnd => {
fstrings = fstrings.saturating_sub(1);
false
}
_ => fstrings == 0,
})
.map(Token::from_spanned);
let tokens = [Token::irrelevant(), Token::irrelevant()]
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ COM81.py:45:8: COM818 Trailing comma on bare tuple prohibited
|
43 | )
44 |
45 | foo = 3,
45 | foo = 3,
| ^ COM818
46 |
47 | class A(object):
Expand Down