Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EHN: also raise B006 for list/dict/set comprehensions #186

Merged
merged 4 commits into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -222,6 +222,11 @@ MIT
Change Log
----------

Unreleased
~~~~~~~~~~

* Update B006: list and dictionary comprehension 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think set comprehensions ({x for x in y}) is also suitable in this context

Suggested change
B006.mutable_comprehensions = (ast.ListComp, ast.DictComp)
B006.mutable_comprehensions = (ast.ListComp, ast.DictComp, ast.SetComp)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I knew I was forgetting something, thanks for noticing I'll fix it soon once I get back to my computer at home.

B006.mutable_calls = {
"Counter",
"OrderedDict",
Expand Down
8 changes: 8 additions & 0 deletions tests/b006_b008.py
Expand Up @@ -119,3 +119,11 @@ 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
2 changes: 2 additions & 0 deletions tests/test_bugbear.py
Expand Up @@ -103,6 +103,8 @@ 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),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree lets only care for newer versions ...

B006(128, 45),
),
)

Expand Down