Skip to content

Commit e655c26

Browse files
authored
Fix C402/C404 false positive for dict() with kwargs (#458)
1 parent 7d8f47f commit e655c26

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

HISTORY.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
History
33
=======
44

5+
* Fix false positive in rules C402 and C404 for ``dict()`` calls with keyword arguments.
6+
7+
Thanks to Anders Kaseorg for the report in `Issue #457 <https://github.com/adamchainz/flake8-comprehensions/issues/457>`__.
8+
59
3.10.0 (2022-05-19)
610
-------------------
711

src/flake8_comprehensions/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
6868

6969
elif (
7070
num_positional_args == 1
71+
and node.func.id == "dict"
72+
and len(node.keywords) == 0
7173
and isinstance(node.args[0], (ast.GeneratorExp, ast.ListComp))
7274
and isinstance(node.args[0].elt, ast.Tuple)
7375
and len(node.args[0].elt.elts) == 2
74-
and node.func.id == "dict"
7576
):
7677
if isinstance(node.args[0], ast.GeneratorExp):
7778
msg = "C402"

tests/test_flake8_comprehensions.py

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def test_C401_fail(code, failures, flake8_path):
129129
foo = [('a', 1), ('b', 2), ('c', 3)]
130130
dict(pair for pair in foo if pair[1] % 2 == 0)
131131
""",
132+
# Previously a false positive:
133+
"dict(((x, str(x)) for x in range(10)), c=1)",
132134
],
133135
)
134136
def test_C402_pass(code, flake8_path):
@@ -204,6 +206,8 @@ def test_C403_fail(code, failures, flake8_path):
204206
"foo = {x: x for x in range(10)}",
205207
# Previously a false positive:
206208
"foo = dict([x.split('=') for x in ['a=1', 'b=2']])",
209+
# Previously a false positive:
210+
"dict([(x, x) for x in range(10)], y=2)",
207211
],
208212
)
209213
def test_C404_pass(code, flake8_path):

0 commit comments

Comments
 (0)