diff --git a/HISTORY.rst b/HISTORY.rst index 36d4af8..84c640b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,10 @@ History ======= +* Fix false positive in rules C402 and C404 for ``dict()`` calls with keyword arguments. + + Thanks to Anders Kaseorg for the report in `Issue #457 `__. + 3.10.0 (2022-05-19) ------------------- diff --git a/src/flake8_comprehensions/__init__.py b/src/flake8_comprehensions/__init__.py index 8edec34..27f519b 100644 --- a/src/flake8_comprehensions/__init__.py +++ b/src/flake8_comprehensions/__init__.py @@ -68,10 +68,11 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]: elif ( num_positional_args == 1 + and node.func.id == "dict" + and len(node.keywords) == 0 and isinstance(node.args[0], (ast.GeneratorExp, ast.ListComp)) and isinstance(node.args[0].elt, ast.Tuple) and len(node.args[0].elt.elts) == 2 - and node.func.id == "dict" ): if isinstance(node.args[0], ast.GeneratorExp): msg = "C402" diff --git a/tests/test_flake8_comprehensions.py b/tests/test_flake8_comprehensions.py index e699456..5e9626a 100644 --- a/tests/test_flake8_comprehensions.py +++ b/tests/test_flake8_comprehensions.py @@ -129,6 +129,8 @@ def test_C401_fail(code, failures, flake8_path): foo = [('a', 1), ('b', 2), ('c', 3)] dict(pair for pair in foo if pair[1] % 2 == 0) """, + # Previously a false positive: + "dict(((x, str(x)) for x in range(10)), c=1)", ], ) def test_C402_pass(code, flake8_path): @@ -204,6 +206,8 @@ def test_C403_fail(code, failures, flake8_path): "foo = {x: x for x in range(10)}", # Previously a false positive: "foo = dict([x.split('=') for x in ['a=1', 'b=2']])", + # Previously a false positive: + "dict([(x, x) for x in range(10)], y=2)", ], ) def test_C404_pass(code, flake8_path):