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 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..8150a33 100644 --- a/tests/b018_classes.py +++ b/tests/b018_classes.py @@ -30,3 +30,6 @@ class Foo3: a = 2 "str" 1 + (1,) # bad + (2, 3) # bad + t = (4, 5) # good diff --git a/tests/b018_functions.py b/tests/b018_functions.py index a4c2b49..c3e5440 100644 --- a/tests/b018_functions.py +++ b/tests/b018_functions.py @@ -29,3 +29,6 @@ def foo3(): a = 2 "str" 3 + (1,) # bad + (2, 3) # bad + t = (4, 5) # good diff --git a/tests/b018_modules.py b/tests/b018_modules.py index 70d6e59..eb94008 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,) # bad +(2, 3) # bad +t = (4, 5) # good 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):