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

include tuples in b018 useless-statement check #432

Merged
merged 3 commits into from Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.rst
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
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
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
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
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
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))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we not need 34 here when we include 32 for modules tests? What am I missing here, cause the variable assign happens in a function it's fine?

Copy link
Contributor Author

@r-downing r-downing Nov 30, 2023

Choose a reason for hiding this comment

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

Oh it's a little confusing because the line numbers for the three different test cases are referring to the 3 different b018_....py files (but the line numbers are almost the same for 2 of the files). Added the same 3 lines to each of the files and corresponding test cases, in all cases the first 2 lines are bad and the 3rd is good

    (1,)  # bad
    (2, 3)  # bad
    t = (4, 5)  # good

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