Skip to content

Commit 160345d

Browse files
authored
Add confidence levels to all exception checkers (#7716)
1 parent 3c180df commit 160345d

File tree

13 files changed

+87
-54
lines changed

13 files changed

+87
-54
lines changed

pylint/checkers/exceptions.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ class ExceptionRaiseRefVisitor(BaseVisitor):
198198

199199
def visit_name(self, node: nodes.Name) -> None:
200200
if node.name == "NotImplemented":
201-
self._checker.add_message("notimplemented-raised", node=self._node)
201+
self._checker.add_message(
202+
"notimplemented-raised", node=self._node, confidence=HIGH
203+
)
202204
elif node.name in OVERGENERAL_EXCEPTIONS:
203205
self._checker.add_message(
204206
"broad-exception-raised",
@@ -217,15 +219,20 @@ def visit_call(self, node: nodes.Call) -> None:
217219
):
218220
msg = node.args[0].value
219221
if "%" in msg or ("{" in msg and "}" in msg):
220-
self._checker.add_message("raising-format-tuple", node=self._node)
222+
self._checker.add_message(
223+
"raising-format-tuple", node=self._node, confidence=HIGH
224+
)
221225

222226

223227
class ExceptionRaiseLeafVisitor(BaseVisitor):
224228
"""Visitor for handling leaf kinds of a raise value."""
225229

226230
def visit_const(self, node: nodes.Const) -> None:
227231
self._checker.add_message(
228-
"raising-bad-type", node=self._node, args=node.value.__class__.__name__
232+
"raising-bad-type",
233+
node=self._node,
234+
args=node.value.__class__.__name__,
235+
confidence=INFERENCE,
229236
)
230237

231238
def visit_instance(self, instance: objects.ExceptionInstance) -> None:
@@ -238,14 +245,28 @@ def visit_instance(self, instance: objects.ExceptionInstance) -> None:
238245
def visit_classdef(self, node: nodes.ClassDef) -> None:
239246
if not utils.inherit_from_std_ex(node) and utils.has_known_bases(node):
240247
if node.newstyle:
241-
self._checker.add_message("raising-non-exception", node=self._node)
248+
self._checker.add_message(
249+
"raising-non-exception",
250+
node=self._node,
251+
confidence=INFERENCE,
252+
)
242253

243254
def visit_tuple(self, _: nodes.Tuple) -> None:
244-
self._checker.add_message("raising-bad-type", node=self._node, args="tuple")
255+
self._checker.add_message(
256+
"raising-bad-type",
257+
node=self._node,
258+
args="tuple",
259+
confidence=INFERENCE,
260+
)
245261

246262
def visit_default(self, node: nodes.NodeNG) -> None:
247263
name = getattr(node, "name", node.__class__.__name__)
248-
self._checker.add_message("raising-bad-type", node=self._node, args=name)
264+
self._checker.add_message(
265+
"raising-bad-type",
266+
node=self._node,
267+
args=name,
268+
confidence=INFERENCE,
269+
)
249270

250271

251272
class ExceptionsChecker(checkers.BaseChecker):
@@ -316,7 +337,7 @@ def _check_misplaced_bare_raise(self, node: nodes.Raise) -> None:
316337

317338
expected = (nodes.ExceptHandler,)
318339
if not current or not isinstance(current.parent, expected):
319-
self.add_message("misplaced-bare-raise", node=node)
340+
self.add_message("misplaced-bare-raise", node=node, confidence=HIGH)
320341

321342
def _check_bad_exception_cause(self, node: nodes.Raise) -> None:
322343
"""Verify that the exception cause is properly set.
@@ -522,17 +543,22 @@ def visit_tryexcept(self, node: nodes.TryExcept) -> None:
522543
for index, handler in enumerate(node.handlers):
523544
if handler.type is None:
524545
if not _is_raising(handler.body):
525-
self.add_message("bare-except", node=handler)
546+
self.add_message("bare-except", node=handler, confidence=HIGH)
526547

527548
# check if an "except:" is followed by some other
528549
# except
529550
if index < (nb_handlers - 1):
530551
msg = "empty except clause should always appear last"
531-
self.add_message("bad-except-order", node=node, args=msg)
552+
self.add_message(
553+
"bad-except-order", node=node, args=msg, confidence=HIGH
554+
)
532555

533556
elif isinstance(handler.type, nodes.BoolOp):
534557
self.add_message(
535-
"binary-op-exception", node=handler, args=handler.type.op
558+
"binary-op-exception",
559+
node=handler,
560+
args=handler.type.op,
561+
confidence=HIGH,
536562
)
537563
else:
538564
try:
@@ -561,7 +587,10 @@ def visit_tryexcept(self, node: nodes.TryExcept) -> None:
561587
if previous_exc in exc_ancestors:
562588
msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
563589
self.add_message(
564-
"bad-except-order", node=handler.type, args=msg
590+
"bad-except-order",
591+
node=handler.type,
592+
args=msg,
593+
confidence=INFERENCE,
565594
)
566595
if (
567596
exception.name in self.linter.config.overgeneral_exceptions
@@ -572,11 +601,15 @@ def visit_tryexcept(self, node: nodes.TryExcept) -> None:
572601
"broad-exception-caught",
573602
args=exception.name,
574603
node=handler.type,
604+
confidence=INFERENCE,
575605
)
576606

577607
if exception in exceptions_classes:
578608
self.add_message(
579-
"duplicate-except", args=exception.name, node=handler.type
609+
"duplicate-except",
610+
args=exception.name,
611+
node=handler.type,
612+
confidence=INFERENCE,
580613
)
581614

582615
exceptions_classes += [exc for _, exc in exceptions]
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
bad-except-order:9:7:9:16::Bad except clauses order (Exception is an ancestor class of TypeError):UNDEFINED
2-
bad-except-order:16:7:16:17::Bad except clauses order (LookupError is an ancestor class of IndexError):UNDEFINED
3-
bad-except-order:23:7:23:38::Bad except clauses order (LookupError is an ancestor class of IndexError):UNDEFINED
4-
bad-except-order:23:7:23:38::Bad except clauses order (NameError is an ancestor class of UnboundLocalError):UNDEFINED
5-
bad-except-order:26:0:31:8::Bad except clauses order (empty except clause should always appear last):UNDEFINED
1+
bad-except-order:9:7:9:16::Bad except clauses order (Exception is an ancestor class of TypeError):INFERENCE
2+
bad-except-order:16:7:16:17::Bad except clauses order (LookupError is an ancestor class of IndexError):INFERENCE
3+
bad-except-order:23:7:23:38::Bad except clauses order (LookupError is an ancestor class of IndexError):INFERENCE
4+
bad-except-order:23:7:23:38::Bad except clauses order (NameError is an ancestor class of UnboundLocalError):INFERENCE
5+
bad-except-order:26:0:31:8::Bad except clauses order (empty except clause should always appear last):HIGH

tests/functional/b/bare_except.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bare-except:5:0:6:8::No exception type(s) specified:UNDEFINED
1+
bare-except:5:0:6:8::No exception type(s) specified:HIGH
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
broad-exception-caught:6:7:6:16::Catching too general exception Exception:UNDEFINED
2-
broad-exception-caught:12:7:12:20::Catching too general exception BaseException:UNDEFINED
1+
broad-exception-caught:6:7:6:16::Catching too general exception Exception:INFERENCE
2+
broad-exception-caught:12:7:12:20::Catching too general exception BaseException:INFERENCE
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
broad-exception-raised:5:4:5:41:exploding_apple:"Raising too general exception: Exception":HIGH
22
broad-exception-raised:10:8:10:34:raise_and_catch:"Raising too general exception: Exception":HIGH
3-
broad-exception-caught:11:11:11:20:raise_and_catch:Catching too general exception Exception:UNDEFINED
3+
broad-exception-caught:11:11:11:20:raise_and_catch:Catching too general exception Exception:INFERENCE
44
broad-exception-raised:14:0:14:17::"Raising too general exception: Exception":HIGH
55
broad-exception-raised:15:0:15:21::"Raising too general exception: BaseException":HIGH
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
duplicate-except:9:11:9:21:main:Catching previously caught exception type ValueError:UNDEFINED
1+
duplicate-except:9:11:9:21:main:Catching previously caught exception type ValueError:INFERENCE
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
binary-op-exception:5:0:6:20::"Exception to catch is the result of a binary ""or"" operation":UNDEFINED
2-
binary-op-exception:7:0:8:20::"Exception to catch is the result of a binary ""and"" operation":UNDEFINED
3-
binary-op-exception:9:0:10:20::"Exception to catch is the result of a binary ""or"" operation":UNDEFINED
4-
binary-op-exception:11:0:12:20::"Exception to catch is the result of a binary ""or"" operation":UNDEFINED
1+
binary-op-exception:5:0:6:20::"Exception to catch is the result of a binary ""or"" operation":HIGH
2+
binary-op-exception:7:0:8:20::"Exception to catch is the result of a binary ""and"" operation":HIGH
3+
binary-op-exception:9:0:10:20::"Exception to catch is the result of a binary ""or"" operation":HIGH
4+
binary-op-exception:11:0:12:20::"Exception to catch is the result of a binary ""or"" operation":HIGH
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
unreachable:25:4:25:25:test_ignores_raise_uninferable:Unreachable code:HIGH
22
missing-raises-doc:28:0:28:45:test_ignores_returns_from_inner_functions:"""RuntimeError"" not documented as being raised":HIGH
33
unreachable:42:4:42:25:test_ignores_returns_from_inner_functions:Unreachable code:HIGH
4-
raising-bad-type:54:4:54:22:test_ignores_returns_use_only_names:Raising int while only classes or instances are allowed:UNDEFINED
4+
raising-bad-type:54:4:54:22:test_ignores_returns_use_only_names:Raising int while only classes or instances are allowed:INFERENCE
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
raising-non-exception:38:4:38:30:bad_case0:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
2-
raising-non-exception:42:4:42:25:bad_case1:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
3-
raising-non-exception:48:4:48:30:bad_case2:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
4-
raising-non-exception:52:4:52:23:bad_case3:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
5-
notimplemented-raised:56:4:56:31:bad_case4:NotImplemented raised - should raise NotImplementedError:UNDEFINED
6-
raising-bad-type:60:4:60:11:bad_case5:Raising int while only classes or instances are allowed:UNDEFINED
7-
raising-bad-type:64:4:64:14:bad_case6:Raising NoneType while only classes or instances are allowed:UNDEFINED
8-
raising-non-exception:68:4:68:14:bad_case7:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
9-
raising-non-exception:72:4:72:15:bad_case8:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
10-
raising-non-exception:76:4:76:14:bad_case9:Raising a new style class which doesn't inherit from BaseException:UNDEFINED
11-
raising-bad-type:110:4:110:18:bad_case10:Raising str while only classes or instances are allowed:UNDEFINED
1+
raising-non-exception:38:4:38:30:bad_case0:Raising a new style class which doesn't inherit from BaseException:INFERENCE
2+
raising-non-exception:42:4:42:25:bad_case1:Raising a new style class which doesn't inherit from BaseException:INFERENCE
3+
raising-non-exception:48:4:48:30:bad_case2:Raising a new style class which doesn't inherit from BaseException:INFERENCE
4+
raising-non-exception:52:4:52:23:bad_case3:Raising a new style class which doesn't inherit from BaseException:INFERENCE
5+
notimplemented-raised:56:4:56:31:bad_case4:NotImplemented raised - should raise NotImplementedError:HIGH
6+
raising-bad-type:60:4:60:11:bad_case5:Raising int while only classes or instances are allowed:INFERENCE
7+
raising-bad-type:64:4:64:14:bad_case6:Raising NoneType while only classes or instances are allowed:INFERENCE
8+
raising-non-exception:68:4:68:14:bad_case7:Raising a new style class which doesn't inherit from BaseException:INFERENCE
9+
raising-non-exception:72:4:72:15:bad_case8:Raising a new style class which doesn't inherit from BaseException:INFERENCE
10+
raising-non-exception:76:4:76:14:bad_case9:Raising a new style class which doesn't inherit from BaseException:INFERENCE
11+
raising-bad-type:110:4:110:18:bad_case10:Raising str while only classes or instances are allowed:INFERENCE
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
misplaced-bare-raise:6:4:6:9::The raise statement is not inside an except clause:UNDEFINED
2-
misplaced-bare-raise:36:16:36:21:test1.best:The raise statement is not inside an except clause:UNDEFINED
3-
misplaced-bare-raise:39:4:39:9:test1:The raise statement is not inside an except clause:UNDEFINED
4-
misplaced-bare-raise:40:0:40:5::The raise statement is not inside an except clause:UNDEFINED
5-
misplaced-bare-raise:49:4:49:9::The raise statement is not inside an except clause:UNDEFINED
6-
misplaced-bare-raise:57:4:57:9:A:The raise statement is not inside an except clause:UNDEFINED
7-
misplaced-bare-raise:68:4:68:9::The raise statement is not inside an except clause:UNDEFINED
1+
misplaced-bare-raise:6:4:6:9::The raise statement is not inside an except clause:HIGH
2+
misplaced-bare-raise:36:16:36:21:test1.best:The raise statement is not inside an except clause:HIGH
3+
misplaced-bare-raise:39:4:39:9:test1:The raise statement is not inside an except clause:HIGH
4+
misplaced-bare-raise:40:0:40:5::The raise statement is not inside an except clause:HIGH
5+
misplaced-bare-raise:49:4:49:9::The raise statement is not inside an except clause:HIGH
6+
misplaced-bare-raise:57:4:57:9:A:The raise statement is not inside an except clause:HIGH
7+
misplaced-bare-raise:68:4:68:9::The raise statement is not inside an except clause:HIGH

0 commit comments

Comments
 (0)