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

Consider the new f-string tokens for flake8-commas #8582

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

dhruvmanila
Copy link
Member

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

@dhruvmanila
Copy link
Member Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@dhruvmanila dhruvmanila added the bug Something isn't working label Nov 9, 2023
Comment on lines -32 to +35
struct Token<'tok> {
type_: TokenType,
// Underlying token.
spanned: Option<&'tok Spanned>,
struct Token {
r#type: TokenType,
range: TextRange,
Copy link
Member Author

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.

Comment on lines +243 to +271
.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
}
}
});
Copy link
Member Author

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.

Copy link
Contributor

github-actions bot commented Nov 9, 2023

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+0 -1 violations, +0 -0 fixes in 41 projects)

sphinx-doc/sphinx (+0 -1 violations, +0 -0 fixes)

- 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

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

Comment on lines +256 to +259
indexer
.fstring_ranges()
.outermost(tok_range.start())
.map(|range| Token::new(TokenType::String, range))
Copy link
Member Author

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.

@dhruvmanila
Copy link
Member Author

The ecosystem change is the fact that we've stopped checking for trailing comma within the expression part of f-strings.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@dhruvmanila dhruvmanila merged commit d5606b7 into main Nov 10, 2023
17 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/fstring-comma branch November 10, 2023 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

COM812 breaks conditionals inside multi-line f-strings
2 participants