Skip to content

[flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/set (C400,C401) - #15929

Merged
dylwil3 merged 4 commits into
astral-sh:mainfrom
dylwil3:generator-comma
Feb 5, 2025
Merged

dylwil3 merged 4 commits into
astral-sh:mainfrom
dylwil3:generator-comma

Conversation

@dylwil3

@dylwil3 dylwil3 commented Feb 4, 2025

Copy link
Copy Markdown
Collaborator

The unsafe fixes for the rules unnecessary-generator-list (C400) and unnecessary-generator-set (C401) used to introduce syntax errors if the argument to list or set had a trailing comma, because the fix would retain the comma after transforming the function call to a comprehension.

This PR accounts for the trailing comma when replacing the end of the call with a ] or }.

Closes #15852

@dylwil3 dylwil3 added bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations labels Feb 4, 2025
@github-actions

github-actions Bot commented Feb 4, 2025 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, I've only two nit comments that apply to both rules

@dylwil3 dylwil3 changed the title [flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/comma (C400,C401) [flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/set (C400,C401) Feb 5, 2025
@dylwil3
dylwil3 merged commit c69b19f into astral-sh:main Feb 5, 2025
@dylwil3
dylwil3 deleted the generator-comma branch February 5, 2025 13:38
Comment on lines +127 to +134
// 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)
.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
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was wondering if we can use checker.tokens().in_range(...) instead here?

Comment on lines +130 to +136
// 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_brace_loc = tokenizer
.find(|token| token.kind == SimpleTokenKind::Comma)
.map_or(call.arguments.end(), |comma| comma.end())
- TextSize::from(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above.

dylwil3 added a commit that referenced this pull request Feb 15, 2025
## Summary

Resolves [#16099 ](#16099) based
on [#15929 ](#15929)

## Test Plan

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

---------

Co-authored-by: dylwil3 <dylwil3@gmail.com>
KotlinIsland pushed a commit to KotlinIsland/basedpython that referenced this pull request May 1, 2026
## Summary

Resolves [#16099 ](astral-sh/ruff#16099) based
on [#15929 ](astral-sh/ruff#15929)

## Test Plan

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

---------

Co-authored-by: dylwil3 <dylwil3@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C400 and C401 fixes fail when a comma follows the generator

3 participants