Skip to content

Commit d4e1350

Browse files
authored
EHN: also raise B006 for list/dict/set comprehensions (#186)
* EHN: also raise B006 for list/dict comprehensions * Oh AST inconsistency ... you're not a friend * I forgot about set comprehensions * sigh, hi black
1 parent 931d95a commit d4e1350

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

README.rst

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ MIT
222222
Change Log
223223
----------
224224

225+
Unreleased
226+
~~~~~~~~~~
227+
228+
* Update B006: list, dictionary, and set comprehensions are now also disallowed (#186)
229+
225230
21.9.1
226231
~~~~~~
227232

bugbear.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ def check_for_b005(self, node):
328328

329329
def check_for_b006(self, node):
330330
for default in node.args.defaults + node.args.kw_defaults:
331-
if isinstance(default, B006.mutable_literals):
331+
if isinstance(
332+
default, (*B006.mutable_literals, *B006.mutable_comprehensions)
333+
):
332334
self.errors.append(B006(default.lineno, default.col_offset))
333335
elif isinstance(default, ast.Call):
334336
call_path = ".".join(self.compose_call_path(default.func))
@@ -653,6 +655,7 @@ def visit(self, node):
653655
)
654656
)
655657
B006.mutable_literals = (ast.Dict, ast.List, ast.Set)
658+
B006.mutable_comprehensions = (ast.ListComp, ast.DictComp, ast.SetComp)
656659
B006.mutable_calls = {
657660
"Counter",
658661
"OrderedDict",

tests/b006_b008.py

+12
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,15 @@ def operators_ok_unqualified(
119119
v=attrgetter("foo"), v2=itemgetter("foo"), v3=methodcaller("foo")
120120
):
121121
pass
122+
123+
124+
def list_comprehension_also_not_okay(default=[i ** 2 for i in range(3)]):
125+
pass
126+
127+
128+
def dict_comprehension_also_not_okay(default={i: i ** 2 for i in range(3)}):
129+
pass
130+
131+
132+
def set_comprehension_also_not_okay(default={i ** 2 for i in range(3)}):
133+
pass

tests/test_bugbear.py

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def test_b006_b008(self):
103103
B006(70, 32),
104104
B008(98, 29),
105105
B008(102, 44),
106+
B006(124, 45 if sys.version_info >= (3, 8) else 46),
107+
B006(128, 45),
108+
B006(132, 44),
106109
),
107110
)
108111

0 commit comments

Comments
 (0)