From 4ca0e6bf7a06dea444192e7f0f20f900472de9fb Mon Sep 17 00:00:00 2001 From: Ryan Downing <26515643+r-downing@users.noreply.github.com> Date: Sat, 2 Dec 2023 17:47:58 -0500 Subject: [PATCH] add classname to b018 useless-expression output (#433) * add classname to b018 useless-expression output * update unittests --- bugbear.py | 10 ++++++-- tests/test_bugbear.py | 59 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/bugbear.py b/bugbear.py index 81e619d..2aba936 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1182,7 +1182,13 @@ def check_for_b018(self, node): or subnode.value.value is None ) ): - self.errors.append(B018(subnode.lineno, subnode.col_offset)) + self.errors.append( + B018( + subnode.lineno, + subnode.col_offset, + vars=(subnode.value.__class__.__name__,), + ) + ) def check_for_b021(self, node): if ( @@ -1835,7 +1841,7 @@ def visit_Lambda(self, node): ) B018 = Error( message=( - "B018 Found useless expression. Consider either assigning it to a " + "B018 Found useless {} expression. Consider either assigning it to a " "variable or removing it." ) ) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index aa20b69..00ad471 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -267,11 +267,23 @@ def test_b018_functions(self): bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - 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)) + expected = [ + B018(15, 4, vars=("Constant",)), + B018(16, 4, vars=("Constant",)), + B018(17, 4, vars=("Constant",)), + B018(18, 4, vars=("Constant",)), + B018(19, 4, vars=("Constant",)), + B018(20, 4, vars=("Constant",)), + B018(21, 4, vars=("Constant",)), + B018(22, 4, vars=("List",)), + B018(23, 4, vars=("Set",)), + B018(24, 4, vars=("Dict",)), + B018(28, 4, vars=("Constant",)), + B018(31, 4, vars=("Constant",)), + B018(32, 4, vars=("Tuple",)), + B018(33, 4, vars=("Tuple",)), + ] + self.assertEqual(errors, self.errors(*expected)) def test_b018_classes(self): @@ -279,11 +291,23 @@ def test_b018_classes(self): bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - 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)) + expected = [ + B018(16, 4, vars=("Constant",)), + B018(17, 4, vars=("Constant",)), + B018(18, 4, vars=("Constant",)), + B018(19, 4, vars=("Constant",)), + B018(20, 4, vars=("Constant",)), + B018(21, 4, vars=("Constant",)), + B018(22, 4, vars=("Constant",)), + B018(23, 4, vars=("List",)), + B018(24, 4, vars=("Set",)), + B018(25, 4, vars=("Dict",)), + B018(29, 4, vars=("Constant",)), + B018(32, 4, vars=("Constant",)), + B018(33, 4, vars=("Tuple",)), + B018(34, 4, vars=("Tuple",)), + ] + self.assertEqual(errors, self.errors(*expected)) def test_b018_modules(self): @@ -291,7 +315,20 @@ def test_b018_modules(self): bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - expected = [B018(line, 0) for line in range(9, 21)] + expected = [ + B018(9, 0, vars=("Constant",)), + B018(10, 0, vars=("Constant",)), + B018(11, 0, vars=("Constant",)), + B018(12, 0, vars=("Constant",)), + B018(13, 0, vars=("Constant",)), + B018(14, 0, vars=("Constant",)), + B018(15, 0, vars=("Constant",)), + B018(16, 0, vars=("List",)), + B018(17, 0, vars=("Set",)), + B018(18, 0, vars=("Dict",)), + B018(19, 0, vars=("Tuple",)), + B018(20, 0, vars=("Tuple",)), + ] self.assertEqual(errors, self.errors(*expected)) def test_b019(self):