Skip to content

Comments

[flake8-comprehensions] Preserve trailing commas for single-element lists (C409)#19571

Merged
ntBre merged 6 commits intoastral-sh:mainfrom
danparizher:fix-19568
Sep 19, 2025
Merged

[flake8-comprehensions] Preserve trailing commas for single-element lists (C409)#19571
ntBre merged 6 commits intoastral-sh:mainfrom
danparizher:fix-19568

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes #19568

@github-actions
Copy link
Contributor

github-actions bot commented Jul 26, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre self-requested a review July 26, 2025 18:42
Comment on lines 124 to 129
let has_trailing_comma = {
// Look for a comma after the argument in the call
let after_argument = checker
.locator()
.slice(TextRange::new(argument.end(), call.end()));
after_argument.trim_start().starts_with(',')
Copy link
Contributor

Choose a reason for hiding this comment

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

These two checks, needs_trailing_comma and has_trailing_comma aren't mutually exclusive. We may need to either add a new comma (needs_trailing_comma) or preserve it (has_trailing_comma). You can see that deleting needs_trailing_comma was wrong because the t6 test case below has now lost its comma.

I'm wondering if we could reuse the token-based check for has_trailing_comma too, but the main issue first is restoring the old functionality.

@danparizher danparizher requested a review from ntBre July 30, 2025 05:28
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Aug 7, 2025
@ntBre ntBre changed the title [flake8_comprehensions] Fix C409 to preserve trailing commas in tuple calls [flake8-comprehensions] Fix C409 to preserve trailing commas in tuple calls Aug 7, 2025
@ntBre
Copy link
Contributor

ntBre commented Aug 7, 2025

Okay I looked at this much more closely today, and I believe the only diff we needed was this:

diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs
index b2d3af263f..30a250e7ce 100644
--- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs
+++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs
@@ -124,7 +124,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
                 let needs_trailing_comma = if let [item] = elts.as_slice() {
                     SimpleTokenizer::new(
                         checker.locator().contents(),
-                        TextRange::new(item.end(), call.end()),
+                        TextRange::new(item.end(), argument.end()),
                     )
                     .all(|token| token.kind != SimpleTokenKind::Comma)
                 } else {

This preserves the comma in tuple([1],) and also doesn't add an unnecessary comma to the t10 case (t10 = (1, 2,) in the current fix).

We shouldn't check the tokens all the way to the end of the call because those are not preserved.

Comment on lines 124 to 132
// Check if the original call had a trailing comma after the argument
let has_trailing_comma = {
// Look for a comma after the argument in the call
let after_argument = checker
.locator()
.slice(TextRange::new(argument.end(), call.end()));
after_argument.trim_start().starts_with(',')
};

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to revert all of this with the change I suggested.

@MichaReiser
Copy link
Member

@danparizher do you plan to come back to this or should we close this PR?

@danparizher danparizher requested a review from ntBre September 19, 2025 02:59
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre ntBre changed the title [flake8-comprehensions] Fix C409 to preserve trailing commas in tuple calls [flake8-comprehensions] Preserve trailing commas for single-element lists (C409) Sep 19, 2025
@ntBre ntBre merged commit c0fb235 into astral-sh:main Sep 19, 2025
35 checks passed
@danparizher danparizher deleted the fix-19568 branch September 19, 2025 13:36
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.

C409 fix omits comma for tuple([1],)

3 participants