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

[flake8-comprehensions]: Handle trailing comma in C403 fix #16110

Merged
merged 4 commits into from
Feb 15, 2025

Conversation

ayushbaweja
Copy link
Contributor

Summary

Resolves #16099 based on #15929

Test Plan

Added test case s = set([x for x in range(3)],) and updated snapshot.

Copy link
Contributor

github-actions bot commented Feb 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 12, 2025
@ayushbaweja ayushbaweja changed the title fix(flake8-comprehensions): Handle trailing comma in C403 fix [flake8-comprehensions]: Handle trailing comma in C403 fix Feb 12, 2025
Comment on lines 74 to 82
// Place `}` at argument's end or at trailing comma if present
let mut tokenizer = SimpleTokenizer::starts_at(
argument.end(),
checker.locator().slice(TextRange::up_to(call.end())),
);
let right_brace_loc = tokenizer
.find(|token| token.kind == SimpleTokenKind::Comma)
.map_or(call.arguments.end() - one, |comma| {
comma.end() - TextSize::from(1)
});

Copy link
Member

Choose a reason for hiding this comment

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

I think we should use checker.tokens().in_range(...) here as we already have access to the parsed tokens. I think the range would be TextRange::new(argument.end(), call.end()). You can refer to other references of in_range method to check how it's being used.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good call @dhruvmanila ! Do you (@ayushbaweja) want to also make the same changes to unnecessary_generator_list.rs and unnecessary_generator_set.rs? Here's the patch for the first of those, for example:

diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
index cc389e1f4..2e3b7fbc9 100644
--- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
+++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs
@@ -4,7 +4,7 @@ use ruff_python_ast as ast;
 use ruff_python_ast::comparable::ComparableExpr;
 use ruff_python_ast::parenthesize::parenthesized_range;
 use ruff_python_ast::ExprGenerator;
-use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
+use ruff_python_parser::TokenKind;
 use ruff_text_size::{Ranged, TextRange, TextSize};
 
 use crate::checkers::ast::Checker;
@@ -125,10 +125,12 @@ pub(crate) fn unnecessary_generator_list(checker: &Checker, call: &ast::ExprCall
 
         // Replace `)` with `]`.
         // Place `]` at argument's end or at trailing comma if present
-        let mut tokenizer =
-            SimpleTokenizer::new(checker.source(), TextRange::new(argument.end(), call.end()));
-        let right_bracket_loc = tokenizer
-            .find(|token| token.kind == SimpleTokenKind::Comma)
+        let after_arg_tokens = checker
+            .tokens()
+            .in_range(TextRange::new(argument.end(), call.end()));
+        let right_bracket_loc = after_arg_tokens
+            .iter()
+            .find(|token| token.kind() == TokenKind::Comma)
             .map_or(call.arguments.end(), |comma| comma.end())
             - TextSize::from(1);
         let call_end = Edit::replacement("]".to_string(), right_bracket_loc, call.end());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Will make the change and also update the unnecessary_generator files

@dylwil3 dylwil3 self-assigned this Feb 15, 2025
@dylwil3 dylwil3 force-pushed the fix-c403-trailing-comma branch from fcd1884 to 4dfd52d Compare February 15, 2025 17:38
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thanks so much, this is great!

@dylwil3 dylwil3 merged commit df45a9d into astral-sh:main Feb 15, 2025
21 checks passed
@dylwil3 dylwil3 changed the title [flake8-comprehensions]: Handle trailing comma in C403 fix [flake8-comprehensions]: Handle trailing comma in C403 fix Feb 15, 2025
@ayushbaweja ayushbaweja deleted the fix-c403-trailing-comma branch February 16, 2025 01:33
dhruvmanila added a commit that referenced this pull request Feb 24, 2025
## Summary

This is mainly on me for not noticing this during the last release but I
noticed in the last changelog that there's only 1 bug fix which didn't
seem correct as I saw multiple of them so I looked at a couple of PRs
that are in "Rule changes" section and the PRs that were marked with the
`bug` label was categorized there because

1. It _also_ had other labels like `rule` and `fixes`
(#16080,
#16110,
#16219, etc.)
2. Some PRs didn't have the `bug` label (but the issue as marked as
`bug`) but _only_ labels like "fixes"
(#16011,
#16132, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C403 fix fails when a comma follows the list
4 participants