From d79b326c3dec0fc43967cccbed8b899920986a83 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 15 Sep 2021 16:14:55 -0400 Subject: [PATCH 1/4] EHN: also raise B006 for list/dict comprehensions --- README.rst | 5 +++++ bugbear.py | 5 ++++- tests/b006_b008.py | 8 ++++++++ tests/test_bugbear.py | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 221bf3f..a06f11f 100644 --- a/README.rst +++ b/README.rst @@ -222,6 +222,11 @@ MIT Change Log ---------- +Unreleased +~~~~~~~~~~ + +* Update B006: list and dictionary comprehension are now also disallowed (#186) + 21.9.1 ~~~~~~ diff --git a/bugbear.py b/bugbear.py index 3d05ae6..43f7dac 100644 --- a/bugbear.py +++ b/bugbear.py @@ -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)) @@ -653,6 +655,7 @@ def visit(self, node): ) ) B006.mutable_literals = (ast.Dict, ast.List, ast.Set) +B006.mutable_comprehensions = (ast.ListComp, ast.DictComp) B006.mutable_calls = { "Counter", "OrderedDict", diff --git a/tests/b006_b008.py b/tests/b006_b008.py index 40bf797..898fc10 100644 --- a/tests/b006_b008.py +++ b/tests/b006_b008.py @@ -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 diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 3f91edc..f77cb81 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -103,6 +103,8 @@ def test_b006_b008(self): B006(70, 32), B008(98, 29), B008(102, 44), + B006(124, 45), + B006(128, 45), ), ) From b9da45d1e23987ee608e3342d94ce3a8054975eb Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 15 Sep 2021 16:24:27 -0400 Subject: [PATCH 2/4] Oh AST inconsistency ... you're not a friend --- tests/test_bugbear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index f77cb81..29ba0d5 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -103,7 +103,7 @@ def test_b006_b008(self): B006(70, 32), B008(98, 29), B008(102, 44), - B006(124, 45), + B006(124, 45 if sys.version_info >= (3, 8) else 46), B006(128, 45), ), ) From 0a83c05043f28e27dbb654a69751f6df5300dd53 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 18 Sep 2021 12:14:32 -0400 Subject: [PATCH 3/4] I forgot about set comprehensions --- README.rst | 2 +- bugbear.py | 2 +- tests/b006_b008.py | 4 ++++ tests/test_bugbear.py | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index a06f11f..f2fdb8b 100644 --- a/README.rst +++ b/README.rst @@ -225,7 +225,7 @@ Change Log Unreleased ~~~~~~~~~~ -* Update B006: list and dictionary comprehension are now also disallowed (#186) +* Update B006: list, dictionary, and set comprehensions are now also disallowed (#186) 21.9.1 ~~~~~~ diff --git a/bugbear.py b/bugbear.py index 43f7dac..b47f0d7 100644 --- a/bugbear.py +++ b/bugbear.py @@ -655,7 +655,7 @@ def visit(self, node): ) ) B006.mutable_literals = (ast.Dict, ast.List, ast.Set) -B006.mutable_comprehensions = (ast.ListComp, ast.DictComp) +B006.mutable_comprehensions = (ast.ListComp, ast.DictComp, ast.SetComp) B006.mutable_calls = { "Counter", "OrderedDict", diff --git a/tests/b006_b008.py b/tests/b006_b008.py index 898fc10..e095323 100644 --- a/tests/b006_b008.py +++ b/tests/b006_b008.py @@ -127,3 +127,7 @@ def list_comprehension_also_not_okay(default=[i ** 2 for i in range(3)]): 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 diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 29ba0d5..fd551d8 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -105,6 +105,7 @@ def test_b006_b008(self): B008(102, 44), B006(124, 45 if sys.version_info >= (3, 8) else 46), B006(128, 45), + B006(132, 44) ), ) From dfb8039fc790b684fbae5b52d035b3750761ad07 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 18 Sep 2021 12:17:18 -0400 Subject: [PATCH 4/4] sigh, hi black --- tests/test_bugbear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index fd551d8..6a0fb55 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -105,7 +105,7 @@ def test_b006_b008(self): B008(102, 44), B006(124, 45 if sys.version_info >= (3, 8) else 46), B006(128, 45), - B006(132, 44) + B006(132, 44), ), )