Skip to content

Commit 315f4e7

Browse files
FozzieHicooperlees
andauthored
Split B001 into B029 for empty tuple exception handling (#344)
* Split B001 into B029 for empty tuple exception handling * Update bugbear.py * Update README.rst * Update description in README --------- Co-authored-by: Cooper Lees <[email protected]>
1 parent a25fa41 commit 315f4e7

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ limitations make it difficult.
177177
stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called.
178178
It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user.
179179

180+
**B029**: Using ``except: ()`` with an empty tuple does not handle/catch anything. Add exceptions to handle.
181+
180182
Opinionated warnings
181183
~~~~~~~~~~~~~~~~~~~~
182184

@@ -325,6 +327,7 @@ Future
325327
order to maintain backwards compatibility with Python 3.7.
326328
* B016: Warn when raising f-strings.
327329
* Add B028: Check for an explicit stacklevel keyword argument on the warn method from the warnings module.
330+
* Add B029: Check when trying to use ``except`` with an empty tuple i.e. ``except: ()``.
328331

329332
23.1.20
330333
~~~~~~~~~

bugbear.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,12 @@ def visit(self, node):
276276

277277
def visit_ExceptHandler(self, node):
278278
if node.type is None:
279-
self.errors.append(
280-
B001(node.lineno, node.col_offset, vars=("bare `except:`",))
281-
)
279+
self.errors.append(B001(node.lineno, node.col_offset))
282280
elif isinstance(node.type, ast.Tuple):
283281
names = [_to_name_str(e) for e in node.type.elts]
284282
as_ = " as " + node.name if node.name is not None else ""
285283
if len(names) == 0:
286-
vs = (f"`except (){as_}:`",)
287-
self.errors.append(B001(node.lineno, node.col_offset, vars=vs))
284+
self.errors.append(B029(node.lineno, node.col_offset))
288285
elif len(names) == 1:
289286
self.errors.append(B013(node.lineno, node.col_offset, vars=names))
290287
else:
@@ -1285,7 +1282,7 @@ def visit_Lambda(self, node):
12851282

12861283
B001 = Error(
12871284
message=(
1288-
"B001 Do not use {}, it also catches unexpected "
1285+
"B001 Do not use bare `except:`, it also catches unexpected "
12891286
"events like memory errors, interrupts, system exit, and so on. "
12901287
"Prefer `except Exception:`. If you're sure what you're doing, "
12911288
"be explicit and write `except BaseException:`."
@@ -1530,6 +1527,12 @@ def visit_Lambda(self, node):
15301527
" greater to provide more information to the user."
15311528
)
15321529
)
1530+
B029 = Error(
1531+
message=(
1532+
"B029 Using `except: ()` with an empty tuple does not handle/catch "
1533+
"anything. Add exceptions to handle."
1534+
)
1535+
)
15331536

15341537
# Warnings disabled by default.
15351538
B901 = Error(

tests/b001.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Should emit:
3-
B001 - on lines 8, 40, and 54
3+
B001 - on lines 8 and 40
44
"""
55

66
try:
@@ -40,10 +40,3 @@ def func(**kwargs):
4040
except:
4141
# should be except KeyError:
4242
return
43-
44-
45-
try:
46-
pass
47-
except ():
48-
# Literal empty tuple is just like bare except:
49-
pass

tests/b029.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Should emit:
3+
B029 - on lines 8 and 13
4+
"""
5+
6+
try:
7+
pass
8+
except ():
9+
pass
10+
11+
try:
12+
pass
13+
except () as e:
14+
pass

tests/test_bugbear.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
B026,
4141
B027,
4242
B028,
43+
B029,
4344
B901,
4445
B902,
4546
B903,
@@ -64,9 +65,8 @@ def test_b001(self):
6465
bbc = BugBearChecker(filename=str(filename))
6566
errors = list(bbc.run())
6667
expected = self.errors(
67-
B001(8, 0, vars=("bare `except:`",)),
68-
B001(40, 4, vars=("bare `except:`",)),
69-
B001(47, 0, vars=("`except ():`",)),
68+
B001(8, 0),
69+
B001(40, 4),
7070
)
7171
self.assertEqual(errors, expected)
7272

@@ -438,6 +438,16 @@ def test_b028(self):
438438
expected = self.errors(B028(8, 0), B028(9, 0))
439439
self.assertEqual(errors, expected)
440440

441+
def test_b029(self):
442+
filename = Path(__file__).absolute().parent / "b029.py"
443+
bbc = BugBearChecker(filename=str(filename))
444+
errors = list(bbc.run())
445+
expected = self.errors(
446+
B029(8, 0),
447+
B029(13, 0),
448+
)
449+
self.assertEqual(errors, expected)
450+
441451
@unittest.skipIf(sys.version_info < (3, 8), "not implemented for <3.8")
442452
def test_b907(self):
443453
filename = Path(__file__).absolute().parent / "b907.py"

0 commit comments

Comments
 (0)