-
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
Consider the new f-string tokens for flake8-commas
#8582
Conversation
Current dependencies on/for this PR: This stack of pull requests is managed by Graphite. |
781aef7
to
0c12b82
Compare
struct Token<'tok> { | ||
type_: TokenType, | ||
// Underlying token. | ||
spanned: Option<&'tok Spanned>, | ||
struct Token { | ||
r#type: TokenType, | ||
range: TextRange, |
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 raw token was never used, so let's simplify this a bit.
I've also renamed type_
to r#type
. This is just a personal preference and I can revert if the former is better.
.filter_map(|spanned @ (tok, tok_range)| match tok { | ||
// Completely ignore comments -- they just interfere with the logic. | ||
Tok::Comment(_) => None, | ||
// F-strings are handled as `String` token type with the complete range | ||
// of the outermost f-string. This means that the expression inside the | ||
// f-string is not checked for trailing commas. | ||
Tok::FStringStart => { | ||
fstrings = fstrings.saturating_add(1); | ||
None | ||
} | ||
Tok::FStringEnd => { | ||
fstrings = fstrings.saturating_sub(1); | ||
if fstrings == 0 { | ||
indexer | ||
.fstring_ranges() | ||
.outermost(tok_range.start()) | ||
.map(|range| Token::new(TokenType::String, range)) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => { | ||
if fstrings == 0 { | ||
Some(Token::from(spanned)) | ||
} else { | ||
None | ||
} | ||
} | ||
}); |
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 the main change.
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
COM819 | 1 | 0 | 1 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+0 -1 violations, +0 -0 fixes in 41 projects)
sphinx-doc/sphinx (+0 -1 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --preview
- sphinx/writers/texinfo.py:433:68: COM819 [*] Trailing comma prohibited
Changes by rule (1 rules affected)
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
COM819 | 1 | 0 | 1 | 0 | 0 |
indexer | ||
.fstring_ranges() | ||
.outermost(tok_range.start()) | ||
.map(|range| Token::new(TokenType::String, range)) |
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.
We're getting the range on FStringEnd
, so the None
case isn't possible.
The ecosystem change is the fact that we've stopped checking for trailing comma within the expression part of f-strings. |
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.
Great, thanks!
Summary
This fixes the bug where the
flake8-commas
rules weren't taking the new f-string tokens into account.Test Plan
Add new test cases around f-strings for all of
flake8-commas
's rules.fixes: #8556