From b32f9ac7893e31beeaf37349117c6afb4f19ab15 Mon Sep 17 00:00:00 2001 From: Ryan Downing <26515643+r-downing@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:59:33 -0500 Subject: [PATCH 1/3] include tuples in b018 useless-statement check --- bugbear.py | 1 + tests/b018_classes.py | 3 +++ tests/b018_functions.py | 3 +++ tests/b018_modules.py | 3 +++ tests/test_bugbear.py | 6 +++++- 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bugbear.py b/bugbear.py index 54a13a8..81e619d 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1173,6 +1173,7 @@ def check_for_b018(self, node): ast.List, ast.Set, ast.Dict, + ast.Tuple, ), ) or ( isinstance(subnode.value, ast.Constant) diff --git a/tests/b018_classes.py b/tests/b018_classes.py index 4dc9ca2..742e1bb 100644 --- a/tests/b018_classes.py +++ b/tests/b018_classes.py @@ -30,3 +30,6 @@ class Foo3: a = 2 "str" 1 + (1,) + (2, 3) + t = (4, 5) diff --git a/tests/b018_functions.py b/tests/b018_functions.py index a4c2b49..582e7c5 100644 --- a/tests/b018_functions.py +++ b/tests/b018_functions.py @@ -29,3 +29,6 @@ def foo3(): a = 2 "str" 3 + (1,) + (2, 3) + t = (4, 5) diff --git a/tests/b018_modules.py b/tests/b018_modules.py index 70d6e59..69ce85e 100644 --- a/tests/b018_modules.py +++ b/tests/b018_modules.py @@ -16,3 +16,6 @@ [1, 2] # list {1, 2} # set {"foo": "bar"} # dict +(1,) +(2, 3) +t = (4, 5) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 3ea3e42..aa20b69 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -270,6 +270,8 @@ def test_b018_functions(self): expected = [B018(line, 4) for line in range(15, 25)] expected.append(B018(28, 4)) expected.append(B018(31, 4)) + expected.append(B018(32, 4)) + expected.append(B018(33, 4)) self.assertEqual(errors, self.errors(*expected)) def test_b018_classes(self): @@ -280,6 +282,8 @@ def test_b018_classes(self): expected = [B018(line, 4) for line in range(16, 26)] expected.append(B018(29, 4)) expected.append(B018(32, 4)) + expected.append(B018(33, 4)) + expected.append(B018(34, 4)) self.assertEqual(errors, self.errors(*expected)) def test_b018_modules(self): @@ -287,7 +291,7 @@ def test_b018_modules(self): bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - expected = [B018(line, 0) for line in range(9, 19)] + expected = [B018(line, 0) for line in range(9, 21)] self.assertEqual(errors, self.errors(*expected)) def test_b019(self): From 1a14418842bf329c8b3083d3361eaa303edd3cba Mon Sep 17 00:00:00 2001 From: Ryan Downing <26515643+r-downing@users.noreply.github.com> Date: Thu, 30 Nov 2023 17:09:07 -0500 Subject: [PATCH 2/3] annotate the bad and good test lines --- tests/b018_classes.py | 6 +++--- tests/b018_functions.py | 6 +++--- tests/b018_modules.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/b018_classes.py b/tests/b018_classes.py index 742e1bb..8150a33 100644 --- a/tests/b018_classes.py +++ b/tests/b018_classes.py @@ -30,6 +30,6 @@ class Foo3: a = 2 "str" 1 - (1,) - (2, 3) - t = (4, 5) + (1,) # bad + (2, 3) # bad + t = (4, 5) # good diff --git a/tests/b018_functions.py b/tests/b018_functions.py index 582e7c5..c3e5440 100644 --- a/tests/b018_functions.py +++ b/tests/b018_functions.py @@ -29,6 +29,6 @@ def foo3(): a = 2 "str" 3 - (1,) - (2, 3) - t = (4, 5) + (1,) # bad + (2, 3) # bad + t = (4, 5) # good diff --git a/tests/b018_modules.py b/tests/b018_modules.py index 69ce85e..eb94008 100644 --- a/tests/b018_modules.py +++ b/tests/b018_modules.py @@ -16,6 +16,6 @@ [1, 2] # list {1, 2} # set {"foo": "bar"} # dict -(1,) -(2, 3) -t = (4, 5) +(1,) # bad +(2, 3) # bad +t = (4, 5) # good From 4cd8b6ae9ac9fa22be8288e7e97e50d9e759101c Mon Sep 17 00:00:00 2001 From: Ryan Downing <26515643+r-downing@users.noreply.github.com> Date: Thu, 30 Nov 2023 23:53:04 -0500 Subject: [PATCH 3/3] update readme w/ notes about comma --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index d7a4c0f..be5c7df 100644 --- a/README.rst +++ b/README.rst @@ -137,6 +137,9 @@ using ``pytest.raises``), or use the context manager form with a target (e.g. ``with self.assertRaises(Exception) as ex:``). **B018**: Found useless expression. Either assign it to a variable or remove it. +Note that dangling commas will cause things to be interpreted as useless tuples. +For example, in the statement ``print(".."),`` is the same as ``(print(".."),)`` +which is an unassigned tuple. Simply remove the comma to clear the error. **B019**: Use of ``functools.lru_cache`` or ``functools.cache`` on methods can lead to memory leaks. The cache may retain instance references, preventing