mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-29 11:07:45 +00:00
Add required space when fixing C402 (#7152)
This commit is contained in:
parent
e428099e4c
commit
7ead2c17b1
3 changed files with 34 additions and 1 deletions
|
|
@ -16,3 +16,6 @@ def f(x):
|
|||
return x
|
||||
|
||||
print(f'Hello {dict((x,f(x)) for x in "abc")} World')
|
||||
|
||||
# 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)
|
||||
|
|
|
|||
|
|
@ -110,10 +110,20 @@ pub(crate) fn fix_unnecessary_generator_dict(checker: &Checker, expr: &Expr) ->
|
|||
bail!("Expected tuple to contain two elements");
|
||||
};
|
||||
|
||||
// Insert whitespace before the `for`, since we're removing parentheses, as in:
|
||||
// ```python
|
||||
// dict((x, x)for x in range(3))
|
||||
// ```
|
||||
let mut for_in = generator_exp.for_in.clone();
|
||||
if for_in.whitespace_before == ParenthesizableWhitespace::default() {
|
||||
for_in.whitespace_before =
|
||||
ParenthesizableWhitespace::SimpleWhitespace(SimpleWhitespace(" "));
|
||||
}
|
||||
|
||||
tree = Expression::DictComp(Box::new(DictComp {
|
||||
key: Box::new(key.clone()),
|
||||
value: Box::new(value.clone()),
|
||||
for_in: generator_exp.for_in.clone(),
|
||||
for_in,
|
||||
lbrace: LeftCurlyBrace {
|
||||
whitespace_after: call.whitespace_before_args.clone(),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -231,6 +231,8 @@ C402.py:18:16: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension
|
|||
17 |
|
||||
18 | print(f'Hello {dict((x,f(x)) for x in "abc")} World')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402
|
||||
19 |
|
||||
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
|
||||
|
|
||||
= help: Rewrite as a `dict` comprehension
|
||||
|
||||
|
|
@ -240,5 +242,23 @@ C402.py:18:16: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension
|
|||
17 17 |
|
||||
18 |-print(f'Hello {dict((x,f(x)) for x in "abc")} World')
|
||||
18 |+print(f'Hello { {x: f(x) for x in "abc"} } World')
|
||||
19 19 |
|
||||
20 20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
|
||||
21 21 | dict((k,v)for k,v in d.iteritems() if k in only_args)
|
||||
|
||||
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
|
||||
|
|
||||
= help: Rewrite as a `dict` comprehension
|
||||
|
||||
ℹ Suggested fix
|
||||
18 18 | print(f'Hello {dict((x,f(x)) for x in "abc")} World')
|
||||
19 19 |
|
||||
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}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue