Skip to content

Comments

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

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

[flake8-comprehensions] Handle trailing comma in fixes for unnecessary-generator-list/set (C400,C401)#15929
dylwil3 merged 4 commits intoastral-sh:mainfrom
dylwil3:generator-comma

Conversation

@dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Feb 4, 2025

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 Something isn't working fixes Related to suggested fixes for violations labels Feb 4, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

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
21 checks passed
@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
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
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 <[email protected]>
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.

C400 and C401 fixes fail when a comma follows the generator

3 participants