Skip to content

Commit

Permalink
Avoid flagging starred elements in C402 (#7466)
Browse files Browse the repository at this point in the history
## Summary

Rewriting these is not valid syntax.

Part of #7455.
  • Loading branch information
charliermarsh committed Sep 17, 2023
1 parent 12acd19 commit 28273eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ def f(x):

# Regression test for: https://github.com/astral-sh/ruff/issues/7086
dict((k,v)for k,v in d.iteritems() if k in only_args)

# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458940
dict((*v, k) for k, v in enumerate(calendar.month_abbr))
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ pub(crate) fn unnecessary_generator_dict(
else {
return;
};
if let Expr::GeneratorExp(ast::ExprGeneratorExp { elt, .. }) = argument {
match elt.as_ref() {
Expr::Tuple(ast::ExprTuple { elts, .. }) if elts.len() == 2 => {
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_generator_dict(expr, checker).map(Fix::suggested)
});
}
checker.diagnostics.push(diagnostic);
}
_ => {}
}
let Expr::GeneratorExp(ast::ExprGeneratorExp { elt, .. }) = argument else {
return;
};
let Expr::Tuple(ast::ExprTuple { elts, .. }) = elt.as_ref() else {
return;
};
if elts.len() != 2 {
return;
}
if elts.iter().any(Expr::is_starred_expr) {
return;
}
let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorDict, expr.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_generator_dict(expr, checker).map(Fix::suggested)
});
}
checker.diagnostics.push(diagnostic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ C402.py:21:1: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension)
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
21 | dict((k,v)for k,v in d.iteritems() if k in only_args)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402
22 |
23 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458940
|
= help: Rewrite as a `dict` comprehension

Expand All @@ -260,5 +262,8 @@ C402.py:21:1: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension)
20 20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
21 |-dict((k,v)for k,v in d.iteritems() if k in only_args)
21 |+{k: v for k,v in d.iteritems() if k in only_args}
22 22 |
23 23 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458940
24 24 | dict((*v, k) for k, v in enumerate(calendar.month_abbr))


0 comments on commit 28273eb

Please sign in to comment.