[flake8-comprehensions] Preserve trailing commas for single-element lists (C409) (#19571)
Some checks are pending
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / test ruff-lsp (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Fixes #19568
This commit is contained in:
Dan Parizher 2025-09-19 09:27:14 -04:00 committed by GitHub
parent b5a3503a58
commit c0fb235a70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 70 additions and 1 deletions

View file

@ -42,3 +42,6 @@ tuple(
x for x in [1,2,3] x for x in [1,2,3]
} }
) )
t9 = tuple([1],)
t10 = tuple([1, 2],)

View file

@ -124,7 +124,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
let needs_trailing_comma = if let [item] = elts.as_slice() { let needs_trailing_comma = if let [item] = elts.as_slice() {
SimpleTokenizer::new( SimpleTokenizer::new(
checker.locator().contents(), checker.locator().contents(),
TextRange::new(item.end(), call.end()), TextRange::new(item.end(), argument.end()),
) )
.all(|token| token.kind != SimpleTokenKind::Comma) .all(|token| token.kind != SimpleTokenKind::Comma)
} else { } else {

View file

@ -247,3 +247,36 @@ help: Rewrite as a tuple literal
28 | tuple([x for x in range(5)]) 28 | tuple([x for x in range(5)])
29 | tuple({x for x in range(10)}) 29 | tuple({x for x in range(10)})
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
--> C409.py:46:6
|
44 | )
45 |
46 | t9 = tuple([1],)
| ^^^^^^^^^^^
47 | t10 = tuple([1, 2],)
|
help: Rewrite as a tuple literal
43 | }
44 | )
45 |
- t9 = tuple([1],)
46 + t9 = (1,)
47 | t10 = tuple([1, 2],)
note: This is an unsafe fix and may change runtime behavior
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
--> C409.py:47:7
|
46 | t9 = tuple([1],)
47 | t10 = tuple([1, 2],)
| ^^^^^^^^^^^^^^
|
help: Rewrite as a tuple literal
44 | )
45 |
46 | t9 = tuple([1],)
- t10 = tuple([1, 2],)
47 + t10 = (1, 2)
note: This is an unsafe fix and may change runtime behavior

View file

@ -344,3 +344,36 @@ help: Rewrite as a generator
42 | x for x in [1,2,3] 42 | x for x in [1,2,3]
43 | } 43 | }
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
--> C409.py:46:6
|
44 | )
45 |
46 | t9 = tuple([1],)
| ^^^^^^^^^^^
47 | t10 = tuple([1, 2],)
|
help: Rewrite as a tuple literal
43 | }
44 | )
45 |
- t9 = tuple([1],)
46 + t9 = (1,)
47 | t10 = tuple([1, 2],)
note: This is an unsafe fix and may change runtime behavior
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
--> C409.py:47:7
|
46 | t9 = tuple([1],)
47 | t10 = tuple([1, 2],)
| ^^^^^^^^^^^^^^
|
help: Rewrite as a tuple literal
44 | )
45 |
46 | t9 = tuple([1],)
- t10 = tuple([1, 2],)
47 + t10 = (1, 2)
note: This is an unsafe fix and may change runtime behavior