Add required space when fixing C402 (#7152)

This commit is contained in:
Charlie Marsh 2023-09-05 14:19:33 +02:00 committed by GitHub
parent e428099e4c
commit 7ead2c17b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View file

@ -16,3 +16,6 @@ def f(x):
return x return x
print(f'Hello {dict((x,f(x)) for x in "abc")} World') 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)

View file

@ -110,10 +110,20 @@ pub(crate) fn fix_unnecessary_generator_dict(checker: &Checker, expr: &Expr) ->
bail!("Expected tuple to contain two elements"); 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 { tree = Expression::DictComp(Box::new(DictComp {
key: Box::new(key.clone()), key: Box::new(key.clone()),
value: Box::new(value.clone()), value: Box::new(value.clone()),
for_in: generator_exp.for_in.clone(), for_in,
lbrace: LeftCurlyBrace { lbrace: LeftCurlyBrace {
whitespace_after: call.whitespace_before_args.clone(), whitespace_after: call.whitespace_before_args.clone(),
}, },

View file

@ -231,6 +231,8 @@ 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 {dict((x,f(x)) for x in "abc")} World')
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402
19 |
20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086
| |
= help: Rewrite as a `dict` comprehension = help: Rewrite as a `dict` comprehension
@ -240,5 +242,23 @@ C402.py:18:16: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension
17 17 | 17 17 |
18 |-print(f'Hello {dict((x,f(x)) for x in "abc")} World') 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') 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}