Skip to content

Commit

Permalink
include tuples in b018 useless-statement check (#432)
Browse files Browse the repository at this point in the history
* include tuples in b018 useless-statement check

* annotate the bad and good test lines

* update readme w/ notes about comma
  • Loading branch information
r-downing authored Dec 1, 2023
1 parent 09f44c3 commit 6686e52
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ def check_for_b018(self, node):
ast.List,
ast.Set,
ast.Dict,
ast.Tuple,
),
) or (
isinstance(subnode.value, ast.Constant)
Expand Down
3 changes: 3 additions & 0 deletions tests/b018_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ class Foo3:
a = 2
"str"
1
(1,) # bad
(2, 3) # bad
t = (4, 5) # good
3 changes: 3 additions & 0 deletions tests/b018_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ def foo3():
a = 2
"str"
3
(1,) # bad
(2, 3) # bad
t = (4, 5) # good
3 changes: 3 additions & 0 deletions tests/b018_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[1, 2] # list
{1, 2} # set
{"foo": "bar"} # dict
(1,) # bad
(2, 3) # bad
t = (4, 5) # good
6 changes: 5 additions & 1 deletion tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -280,14 +282,16 @@ 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):
filename = Path(__file__).absolute().parent / "b018_modules.py"
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):
Expand Down

0 comments on commit 6686e52

Please sign in to comment.