[flake8-simplify] Fix SIM911 autofix creating a syntax error (#18793)
Some checks are pending
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 / 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 / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
The fix would create a syntax error if there wasn't a space between the
`in` keyword and the following expression.
For example:
```python
for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
```

I also noticed that the tests for `SIM911` were note being run, so I
fixed that.

Fixes #18776

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

Add regression test
<!-- How was it tested? -->
This commit is contained in:
Victor Hugo Gomes 2025-06-23 11:24:47 -03:00 committed by GitHub
parent f4c6ff3f68
commit 9e9c4fe17b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View file

@ -29,3 +29,8 @@ def foo():
dict = {}
for country, stars in zip(dict.keys(), dict.values()):
...
# https://github.com/astral-sh/ruff/issues/18776
flag_stars = {}
for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...

View file

@ -4,6 +4,7 @@ use ruff_python_ast::{self as ast, Arguments, Expr};
use ruff_python_semantic::analyze::typing::is_dict;
use ruff_text_size::Ranged;
use crate::fix::edits;
use crate::{AlwaysFixableViolation, Edit, Fix};
use crate::{checkers::ast::Checker, fix::snippet::SourceCodeSnippet};
@ -101,7 +102,11 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall)
return;
}
let expected = format!("{}.items()", checker.locator().slice(var1));
let expected = edits::pad(
format!("{}.items()", checker.locator().slice(var1)),
expr.range(),
checker.locator(),
);
let actual = checker.locator().slice(expr);
let mut diagnostic = checker.report_diagnostic(

View file

@ -75,3 +75,21 @@ SIM911.py:30:27: SIM911 [*] Use `dict.items()` instead of `zip(dict.keys(), dict
30 |- for country, stars in zip(dict.keys(), dict.values()):
30 |+ for country, stars in dict.items():
31 31 | ...
32 32 |
33 33 |
SIM911.py:36:22: SIM911 [*] Use ` flag_stars.items()` instead of `(zip)(flag_stars.keys(), flag_stars.values())`
|
34 | # https://github.com/astral-sh/ruff/issues/18776
35 | flag_stars = {}
36 | for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM911
|
= help: Replace `(zip)(flag_stars.keys(), flag_stars.values())` with ` flag_stars.items()`
Safe fix
33 33 |
34 34 | # https://github.com/astral-sh/ruff/issues/18776
35 35 | flag_stars = {}
36 |-for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
36 |+for country, stars in flag_stars.items():...