Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")):...
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Comment on lines +87 to +92
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially thought I have to modify SIM911 to skip only the autofix, but I found a similar implementation here and aligned with that instead. If you’d prefer we go with it, I can update the implementation accordingly.

if !(args.is_empty() && keywords.is_empty()) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way works, I guess it makes sense to be consistent with the other SIM rule.

return;
}
if !checker.semantic().match_builtin_expr(func, "zip") {
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ 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 |
34 | # https://github.com/astral-sh/ruff/issues/18776
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 = {}
Loading