Skip to content

Commit

Permalink
EHN: also raise B006 for list/dict/set comprehensions (#186)
Browse files Browse the repository at this point in the history
* EHN: also raise B006 for list/dict comprehensions

* Oh AST inconsistency ... you're not a friend

* I forgot about set comprehensions

* sigh, hi black
  • Loading branch information
ichard26 committed Sep 28, 2021
1 parent 931d95a commit d4e1350
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -222,6 +222,11 @@ MIT
Change Log
----------

Unreleased
~~~~~~~~~~

* Update B006: list, dictionary, and set comprehensions are now also disallowed (#186)

21.9.1
~~~~~~

Expand Down
5 changes: 4 additions & 1 deletion bugbear.py
Expand Up @@ -328,7 +328,9 @@ def check_for_b005(self, node):

def check_for_b006(self, node):
for default in node.args.defaults + node.args.kw_defaults:
if isinstance(default, B006.mutable_literals):
if isinstance(
default, (*B006.mutable_literals, *B006.mutable_comprehensions)
):
self.errors.append(B006(default.lineno, default.col_offset))
elif isinstance(default, ast.Call):
call_path = ".".join(self.compose_call_path(default.func))
Expand Down Expand Up @@ -653,6 +655,7 @@ def visit(self, node):
)
)
B006.mutable_literals = (ast.Dict, ast.List, ast.Set)
B006.mutable_comprehensions = (ast.ListComp, ast.DictComp, ast.SetComp)
B006.mutable_calls = {
"Counter",
"OrderedDict",
Expand Down
12 changes: 12 additions & 0 deletions tests/b006_b008.py
Expand Up @@ -119,3 +119,15 @@ def operators_ok_unqualified(
v=attrgetter("foo"), v2=itemgetter("foo"), v3=methodcaller("foo")
):
pass


def list_comprehension_also_not_okay(default=[i ** 2 for i in range(3)]):
pass


def dict_comprehension_also_not_okay(default={i: i ** 2 for i in range(3)}):
pass


def set_comprehension_also_not_okay(default={i ** 2 for i in range(3)}):
pass
3 changes: 3 additions & 0 deletions tests/test_bugbear.py
Expand Up @@ -103,6 +103,9 @@ def test_b006_b008(self):
B006(70, 32),
B008(98, 29),
B008(102, 44),
B006(124, 45 if sys.version_info >= (3, 8) else 46),
B006(128, 45),
B006(132, 44),
),
)

Expand Down

0 comments on commit d4e1350

Please sign in to comment.