[flake8-simplify] Skip SIM911 when unknown arguments are present (#20697)
Some checks are pending
CI / cargo clippy (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (${{ github.repository == 'astral-sh/ruff' && 'depot-windows-2022-16' || 'windows-latest' }}) (push) Blocked by required conditions
CI / cargo test (macos-latest) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
CI / cargo test (wasm) (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 / ty completion evaluation (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 (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (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

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

Fixes #18778

Prevent SIM911 from triggering when zip() is called on .keys()/.values()
that take any positional or keyword arguments, so Ruff
never suggests the lossy rewrite.

## Test Plan

<!-- How was it tested? -->

Added a test case to SIM911.py.
This commit is contained in:
Takayuki Maeda 2025-10-22 05:58:48 +09:00 committed by GitHub
parent 4b0fa5f270
commit a51a0f16e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 6 deletions

View file

@ -34,3 +34,7 @@ def foo():
# https://github.com/astral-sh/ruff/issues/18776
flag_stars = {}
for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
# Regression test for https://github.com/astral-sh/ruff/issues/18778
d = {}
for country, stars in zip(d.keys(*x), d.values("hello")):...

View file

@ -78,13 +78,18 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall)
let [arg1, arg2] = &args[..] else {
return;
};
let Some((var1, attr1)) = get_var_attr(arg1) else {
let Some((var1, attr1, args1)) = get_var_attr_args(arg1) else {
return;
};
let Some((var2, attr2)) = get_var_attr(arg2) else {
let Some((var2, attr2, args2)) = get_var_attr_args(arg2) else {
return;
};
if var1.id != var2.id || attr1 != "keys" || attr2 != "values" {
if var1.id != var2.id
|| attr1 != "keys"
|| attr2 != "values"
|| !args1.is_empty()
|| !args2.is_empty()
{
return;
}
if !checker.semantic().match_builtin_expr(func, "zip") {
@ -122,8 +127,11 @@ pub(crate) fn zip_dict_keys_and_values(checker: &Checker, expr: &ast::ExprCall)
)));
}
fn get_var_attr(expr: &Expr) -> Option<(&ExprName, &Identifier)> {
let Expr::Call(ast::ExprCall { func, .. }) = expr else {
fn get_var_attr_args(expr: &Expr) -> Option<(&ExprName, &Identifier, &Arguments)> {
let Expr::Call(ast::ExprCall {
func, arguments, ..
}) = expr
else {
return None;
};
let Expr::Attribute(ExprAttribute { value, attr, .. }) = func.as_ref() else {
@ -132,5 +140,5 @@ fn get_var_attr(expr: &Expr) -> Option<(&ExprName, &Identifier)> {
let Expr::Name(var_name) = value.as_ref() else {
return None;
};
Some((var_name, attr))
Some((var_name, attr, arguments))
}

View file

@ -81,6 +81,8 @@ SIM911 [*] Use ` flag_stars.items()` instead of `(zip)(flag_stars.keys(), flag_s
35 | flag_stars = {}
36 | for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37 |
38 | # Regression test for https://github.com/astral-sh/ruff/issues/18778
|
help: Replace `(zip)(flag_stars.keys(), flag_stars.values())` with ` flag_stars.items()`
33 |
@ -88,3 +90,6 @@ help: Replace `(zip)(flag_stars.keys(), flag_stars.values())` with ` flag_stars.
35 | flag_stars = {}
- for country, stars in(zip)(flag_stars.keys(), flag_stars.values()):...
36 + for country, stars in flag_stars.items():...
37 |
38 | # Regression test for https://github.com/astral-sh/ruff/issues/18778
39 | d = {}