Skip to content

Commit 0ffebf0

Browse files
authored
Fix C406 false positives (#491)
1 parent ff56086 commit 0ffebf0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.rst

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

5+
* Fix false positives in C406 “unnecessary dict literal”.
6+
7+
Fixes `Issue #260 <https://github.com/adamchainz/flake8-comprehensions/issues/260>`__.
8+
59
3.11.0 (2023-03-18)
610
-------------------
711

src/flake8_comprehensions/__init__.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
120120
elif (
121121
num_positional_args == 1
122122
and isinstance(node.args[0], (ast.Tuple, ast.List))
123-
and node.func.id in ("tuple", "list", "set", "dict")
123+
and (
124+
node.func.id in ("tuple", "list", "set")
125+
or (
126+
node.func.id == "dict"
127+
and all(
128+
isinstance(i, ast.Tuple) and len(i.elts) == 2
129+
for i in node.args[0].elts
130+
)
131+
)
132+
)
124133
):
125134
suffix = "rewrite as a {func} literal."
126135
msg_key = {

tests/test_flake8_comprehensions.py

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ def test_C405_fail(code, failures, flake8_path):
289289
"code",
290290
[
291291
"foo = dict(range)",
292+
"something = (1, 2); dict([something])",
293+
"dict([(1,)])",
292294
],
293295
)
294296
def test_C406_pass(code, flake8_path):

0 commit comments

Comments
 (0)