exceptions.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  4. """Checks for various exception related errors."""
  5. from __future__ import annotations
  6. import builtins
  7. import inspect
  8. import warnings
  9. from collections.abc import Generator
  10. from typing import TYPE_CHECKING, Any
  11. import astroid
  12. from astroid import nodes, objects, util
  13. from astroid.context import InferenceContext
  14. from astroid.typing import InferenceResult, SuccessfulInferenceResult
  15. from pylint import checkers
  16. from pylint.checkers import utils
  17. from pylint.interfaces import HIGH, INFERENCE
  18. from pylint.typing import MessageDefinitionTuple
  19. if TYPE_CHECKING:
  20. from pylint.lint import PyLinter
  21. def _builtin_exceptions() -> set[str]:
  22. def predicate(obj: Any) -> bool:
  23. return isinstance(obj, type) and issubclass(obj, BaseException)
  24. members = inspect.getmembers(builtins, predicate)
  25. return {exc.__name__ for (_, exc) in members}
  26. def _annotated_unpack_infer(
  27. stmt: nodes.NodeNG, context: InferenceContext | None = None
  28. ) -> Generator[tuple[nodes.NodeNG, SuccessfulInferenceResult], None, None]:
  29. """Recursively generate nodes inferred by the given statement.
  30. If the inferred value is a list or a tuple, recurse on the elements.
  31. Returns an iterator which yields tuples in the format
  32. ('original node', 'inferred node').
  33. """
  34. if isinstance(stmt, (nodes.List, nodes.Tuple)):
  35. for elt in stmt.elts:
  36. inferred = utils.safe_infer(elt)
  37. if inferred and not isinstance(inferred, util.UninferableBase):
  38. yield elt, inferred
  39. return
  40. for inferred in stmt.infer(context):
  41. if isinstance(inferred, util.UninferableBase):
  42. continue
  43. yield stmt, inferred
  44. def _is_raising(body: list[nodes.NodeNG]) -> bool:
  45. """Return whether the given statement node raises an exception."""
  46. return any(isinstance(node, nodes.Raise) for node in body)
  47. MSGS: dict[str, MessageDefinitionTuple] = {
  48. "E0701": (
  49. "Bad except clauses order (%s)",
  50. "bad-except-order",
  51. "Used when except clauses are not in the correct order (from the "
  52. "more specific to the more generic). If you don't fix the order, "
  53. "some exceptions may not be caught by the most specific handler.",
  54. ),
  55. "E0702": (
  56. "Raising %s while only classes or instances are allowed",
  57. "raising-bad-type",
  58. "Used when something which is neither a class nor an instance "
  59. "is raised (i.e. a `TypeError` will be raised).",
  60. ),
  61. "E0704": (
  62. "The raise statement is not inside an except clause",
  63. "misplaced-bare-raise",
  64. "Used when a bare raise is not used inside an except clause. "
  65. "This generates an error, since there are no active exceptions "
  66. "to be reraised. An exception to this rule is represented by "
  67. "a bare raise inside a finally clause, which might work, as long "
  68. "as an exception is raised inside the try block, but it is "
  69. "nevertheless a code smell that must not be relied upon.",
  70. ),
  71. "E0705": (
  72. "Exception cause set to something which is not an exception, nor None",
  73. "bad-exception-cause",
  74. 'Used when using the syntax "raise ... from ...", '
  75. "where the exception cause is not an exception, "
  76. "nor None.",
  77. {"old_names": [("E0703", "bad-exception-context")]},
  78. ),
  79. "E0710": (
  80. "Raising a new style class which doesn't inherit from BaseException",
  81. "raising-non-exception",
  82. "Used when a new style class which doesn't inherit from "
  83. "BaseException is raised.",
  84. ),
  85. "E0711": (
  86. "NotImplemented raised - should raise NotImplementedError",
  87. "notimplemented-raised",
  88. "Used when NotImplemented is raised instead of NotImplementedError",
  89. ),
  90. "E0712": (
  91. "Catching an exception which doesn't inherit from Exception: %s",
  92. "catching-non-exception",
  93. "Used when a class which doesn't inherit from "
  94. "Exception is used as an exception in an except clause.",
  95. ),
  96. "W0702": (
  97. "No exception type(s) specified",
  98. "bare-except",
  99. "A bare ``except:`` clause will catch ``SystemExit`` and "
  100. "``KeyboardInterrupt`` exceptions, making it harder to interrupt a program "
  101. "with ``Control-C``, and can disguise other problems. If you want to catch "
  102. "all exceptions that signal program errors, use ``except Exception:`` (bare "
  103. "except is equivalent to ``except BaseException:``).",
  104. ),
  105. "W0718": (
  106. "Catching too general exception %s",
  107. "broad-exception-caught",
  108. "If you use a naked ``except Exception:`` clause, you might end up catching "
  109. "exceptions other than the ones you expect to catch. This can hide bugs or "
  110. "make it harder to debug programs when unrelated errors are hidden.",
  111. {"old_names": [("W0703", "broad-except")]},
  112. ),
  113. "W0705": (
  114. "Catching previously caught exception type %s",
  115. "duplicate-except",
  116. "Used when an except catches a type that was already caught by "
  117. "a previous handler.",
  118. ),
  119. "W0706": (
  120. "The except handler raises immediately",
  121. "try-except-raise",
  122. "Used when an except handler uses raise as its first or only "
  123. "operator. This is useless because it raises back the exception "
  124. "immediately. Remove the raise operator or the entire "
  125. "try-except-raise block!",
  126. ),
  127. "W0707": (
  128. "Consider explicitly re-raising using %s'%s from %s'",
  129. "raise-missing-from",
  130. "Python's exception chaining shows the traceback of the current exception, "
  131. "but also of the original exception. When you raise a new exception after "
  132. "another exception was caught it's likely that the second exception is a "
  133. "friendly re-wrapping of the first exception. In such cases `raise from` "
  134. "provides a better link between the two tracebacks in the final error.",
  135. ),
  136. "W0711": (
  137. 'Exception to catch is the result of a binary "%s" operation',
  138. "binary-op-exception",
  139. "Used when the exception to catch is of the form "
  140. '"except A or B:". If intending to catch multiple, '
  141. 'rewrite as "except (A, B):"',
  142. ),
  143. "W0715": (
  144. "Exception arguments suggest string formatting might be intended",
  145. "raising-format-tuple",
  146. "Used when passing multiple arguments to an exception "
  147. "constructor, the first of them a string literal containing what "
  148. "appears to be placeholders intended for formatting",
  149. ),
  150. "W0716": (
  151. "Invalid exception operation. %s",
  152. "wrong-exception-operation",
  153. "Used when an operation is done against an exception, but the operation "
  154. "is not valid for the exception in question. Usually emitted when having "
  155. "binary operations between exceptions in except handlers.",
  156. ),
  157. "W0719": (
  158. "Raising too general exception: %s",
  159. "broad-exception-raised",
  160. "Raising exceptions that are too generic force you to catch exceptions "
  161. "generically too. It will force you to use a naked ``except Exception:`` "
  162. "clause. You might then end up catching exceptions other than the ones "
  163. "you expect to catch. This can hide bugs or make it harder to debug programs "
  164. "when unrelated errors are hidden.",
  165. ),
  166. }
  167. class BaseVisitor:
  168. """Base class for visitors defined in this module."""
  169. def __init__(self, checker: ExceptionsChecker, node: nodes.Raise) -> None:
  170. self._checker = checker
  171. self._node = node
  172. def visit(self, node: SuccessfulInferenceResult) -> None:
  173. name = node.__class__.__name__.lower()
  174. dispatch_meth = getattr(self, "visit_" + name, None)
  175. if dispatch_meth:
  176. dispatch_meth(node)
  177. else:
  178. self.visit_default(node)
  179. def visit_default(self, _: nodes.NodeNG) -> None:
  180. """Default implementation for all the nodes."""
  181. class ExceptionRaiseRefVisitor(BaseVisitor):
  182. """Visit references (anything that is not an AST leaf)."""
  183. def visit_name(self, node: nodes.Name) -> None:
  184. if node.name == "NotImplemented":
  185. self._checker.add_message(
  186. "notimplemented-raised", node=self._node, confidence=HIGH
  187. )
  188. return
  189. try:
  190. exceptions = list(_annotated_unpack_infer(node))
  191. except astroid.InferenceError:
  192. return
  193. for _, exception in exceptions:
  194. if isinstance(
  195. exception, nodes.ClassDef
  196. ) and self._checker._is_overgeneral_exception(exception):
  197. self._checker.add_message(
  198. "broad-exception-raised",
  199. args=exception.name,
  200. node=self._node,
  201. confidence=INFERENCE,
  202. )
  203. def visit_call(self, node: nodes.Call) -> None:
  204. if isinstance(node.func, nodes.Name):
  205. self.visit_name(node.func)
  206. if (
  207. len(node.args) > 1
  208. and isinstance(node.args[0], nodes.Const)
  209. and isinstance(node.args[0].value, str)
  210. ):
  211. msg = node.args[0].value
  212. if "%" in msg or ("{" in msg and "}" in msg):
  213. self._checker.add_message(
  214. "raising-format-tuple", node=self._node, confidence=HIGH
  215. )
  216. class ExceptionRaiseLeafVisitor(BaseVisitor):
  217. """Visitor for handling leaf kinds of a raise value."""
  218. def visit_const(self, node: nodes.Const) -> None:
  219. self._checker.add_message(
  220. "raising-bad-type",
  221. node=self._node,
  222. args=node.value.__class__.__name__,
  223. confidence=INFERENCE,
  224. )
  225. def visit_instance(self, instance: objects.ExceptionInstance) -> None:
  226. cls = instance._proxied
  227. self.visit_classdef(cls)
  228. # Exception instances have a particular class type
  229. visit_exceptioninstance = visit_instance
  230. def visit_classdef(self, node: nodes.ClassDef) -> None:
  231. if not utils.inherit_from_std_ex(node) and utils.has_known_bases(node):
  232. if node.newstyle:
  233. self._checker.add_message(
  234. "raising-non-exception",
  235. node=self._node,
  236. confidence=INFERENCE,
  237. )
  238. def visit_tuple(self, _: nodes.Tuple) -> None:
  239. self._checker.add_message(
  240. "raising-bad-type",
  241. node=self._node,
  242. args="tuple",
  243. confidence=INFERENCE,
  244. )
  245. def visit_default(self, node: nodes.NodeNG) -> None:
  246. name = getattr(node, "name", node.__class__.__name__)
  247. self._checker.add_message(
  248. "raising-bad-type",
  249. node=self._node,
  250. args=name,
  251. confidence=INFERENCE,
  252. )
  253. class ExceptionsChecker(checkers.BaseChecker):
  254. """Exception related checks."""
  255. name = "exceptions"
  256. msgs = MSGS
  257. options = (
  258. (
  259. "overgeneral-exceptions",
  260. {
  261. "default": ("builtins.BaseException", "builtins.Exception"),
  262. "type": "csv",
  263. "metavar": "<comma-separated class names>",
  264. "help": "Exceptions that will emit a warning when caught.",
  265. },
  266. ),
  267. )
  268. def open(self) -> None:
  269. self._builtin_exceptions = _builtin_exceptions()
  270. for exc_name in self.linter.config.overgeneral_exceptions:
  271. if "." not in exc_name:
  272. warnings.warn_explicit(
  273. "Specifying exception names in the overgeneral-exceptions option"
  274. " without module name is deprecated and support for it"
  275. " will be removed in pylint 3.0."
  276. f" Use fully qualified name (maybe 'builtins.{exc_name}' ?) instead.",
  277. category=UserWarning,
  278. filename="pylint: Command line or configuration file",
  279. lineno=1,
  280. module="pylint",
  281. )
  282. super().open()
  283. @utils.only_required_for_messages(
  284. "misplaced-bare-raise",
  285. "raising-bad-type",
  286. "raising-non-exception",
  287. "notimplemented-raised",
  288. "bad-exception-cause",
  289. "raising-format-tuple",
  290. "raise-missing-from",
  291. "broad-exception-raised",
  292. )
  293. def visit_raise(self, node: nodes.Raise) -> None:
  294. if node.exc is None:
  295. self._check_misplaced_bare_raise(node)
  296. return
  297. if node.cause is None:
  298. self._check_raise_missing_from(node)
  299. else:
  300. self._check_bad_exception_cause(node)
  301. expr = node.exc
  302. ExceptionRaiseRefVisitor(self, node).visit(expr)
  303. inferred = utils.safe_infer(expr)
  304. if inferred is None or isinstance(inferred, util.UninferableBase):
  305. return
  306. ExceptionRaiseLeafVisitor(self, node).visit(inferred)
  307. def _check_misplaced_bare_raise(self, node: nodes.Raise) -> None:
  308. # Filter out if it's present in __exit__.
  309. scope = node.scope()
  310. if (
  311. isinstance(scope, nodes.FunctionDef)
  312. and scope.is_method()
  313. and scope.name == "__exit__"
  314. ):
  315. return
  316. current = node
  317. # Stop when a new scope is generated or when the raise
  318. # statement is found inside a TryFinally.
  319. ignores = (nodes.ExceptHandler, nodes.FunctionDef)
  320. while current and not isinstance(current.parent, ignores):
  321. current = current.parent
  322. expected = (nodes.ExceptHandler,)
  323. if not current or not isinstance(current.parent, expected):
  324. self.add_message("misplaced-bare-raise", node=node, confidence=HIGH)
  325. def _check_bad_exception_cause(self, node: nodes.Raise) -> None:
  326. """Verify that the exception cause is properly set.
  327. An exception cause can be only `None` or an exception.
  328. """
  329. cause = utils.safe_infer(node.cause)
  330. if cause is None or isinstance(cause, util.UninferableBase):
  331. return
  332. if isinstance(cause, nodes.Const):
  333. if cause.value is not None:
  334. self.add_message("bad-exception-cause", node=node, confidence=INFERENCE)
  335. elif not isinstance(cause, nodes.ClassDef) and not utils.inherit_from_std_ex(
  336. cause
  337. ):
  338. self.add_message("bad-exception-cause", node=node, confidence=INFERENCE)
  339. def _check_raise_missing_from(self, node: nodes.Raise) -> None:
  340. if node.exc is None:
  341. # This is a plain `raise`, raising the previously-caught exception. No need for a
  342. # cause.
  343. return
  344. # We'd like to check whether we're inside an `except` clause:
  345. containing_except_node = utils.find_except_wrapper_node_in_scope(node)
  346. if not containing_except_node:
  347. return
  348. # We found a surrounding `except`! We're almost done proving there's a
  349. # `raise-missing-from` here. The only thing we need to protect against is that maybe
  350. # the `raise` is raising the exception that was caught, possibly with some shenanigans
  351. # like `exc.with_traceback(whatever)`. We won't analyze these, we'll just assume
  352. # there's a violation on two simple cases: `raise SomeException(whatever)` and `raise
  353. # SomeException`.
  354. if containing_except_node.name is None:
  355. # The `except` doesn't have an `as exception:` part, meaning there's no way that
  356. # the `raise` is raising the same exception.
  357. class_of_old_error = "Exception"
  358. if isinstance(containing_except_node.type, (nodes.Name, nodes.Tuple)):
  359. # 'except ZeroDivisionError' or 'except (ZeroDivisionError, ValueError)'
  360. class_of_old_error = containing_except_node.type.as_string()
  361. self.add_message(
  362. "raise-missing-from",
  363. node=node,
  364. args=(
  365. f"'except {class_of_old_error} as exc' and ",
  366. node.as_string(),
  367. "exc",
  368. ),
  369. confidence=HIGH,
  370. )
  371. elif (
  372. isinstance(node.exc, nodes.Call)
  373. and isinstance(node.exc.func, nodes.Name)
  374. or isinstance(node.exc, nodes.Name)
  375. and node.exc.name != containing_except_node.name.name
  376. ):
  377. # We have a `raise SomeException(whatever)` or a `raise SomeException`
  378. self.add_message(
  379. "raise-missing-from",
  380. node=node,
  381. args=("", node.as_string(), containing_except_node.name.name),
  382. confidence=HIGH,
  383. )
  384. def _check_catching_non_exception(
  385. self,
  386. handler: nodes.ExceptHandler,
  387. exc: SuccessfulInferenceResult,
  388. part: nodes.NodeNG,
  389. ) -> None:
  390. if isinstance(exc, nodes.Tuple):
  391. # Check if it is a tuple of exceptions.
  392. inferred = [utils.safe_infer(elt) for elt in exc.elts]
  393. if any(isinstance(node, util.UninferableBase) for node in inferred):
  394. # Don't emit if we don't know every component.
  395. return
  396. if all(
  397. node
  398. and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
  399. for node in inferred
  400. ):
  401. return
  402. if not isinstance(exc, nodes.ClassDef):
  403. # Don't emit the warning if the inferred stmt
  404. # is None, but the exception handler is something else,
  405. # maybe it was redefined.
  406. if isinstance(exc, nodes.Const) and exc.value is None:
  407. if (
  408. isinstance(handler.type, nodes.Const) and handler.type.value is None
  409. ) or handler.type.parent_of(exc):
  410. # If the exception handler catches None or
  411. # the exception component, which is None, is
  412. # defined by the entire exception handler, then
  413. # emit a warning.
  414. self.add_message(
  415. "catching-non-exception",
  416. node=handler.type,
  417. args=(part.as_string(),),
  418. )
  419. else:
  420. self.add_message(
  421. "catching-non-exception",
  422. node=handler.type,
  423. args=(part.as_string(),),
  424. )
  425. return
  426. if (
  427. not utils.inherit_from_std_ex(exc)
  428. and exc.name not in self._builtin_exceptions
  429. ):
  430. if utils.has_known_bases(exc):
  431. self.add_message(
  432. "catching-non-exception", node=handler.type, args=(exc.name,)
  433. )
  434. def _check_try_except_raise(self, node: nodes.TryExcept) -> None:
  435. def gather_exceptions_from_handler(
  436. handler: nodes.ExceptHandler,
  437. ) -> list[InferenceResult] | None:
  438. exceptions: list[InferenceResult] = []
  439. if handler.type:
  440. exceptions_in_handler = utils.safe_infer(handler.type)
  441. if isinstance(exceptions_in_handler, nodes.Tuple):
  442. exceptions = list(
  443. {
  444. exception
  445. for exception in exceptions_in_handler.elts
  446. if isinstance(exception, (nodes.Name, nodes.Attribute))
  447. }
  448. )
  449. elif exceptions_in_handler:
  450. exceptions = [exceptions_in_handler]
  451. else:
  452. # Break when we cannot infer anything reliably.
  453. return None
  454. return exceptions
  455. bare_raise = False
  456. handler_having_bare_raise = None
  457. exceptions_in_bare_handler: list[InferenceResult] | None = []
  458. for handler in node.handlers:
  459. if bare_raise:
  460. # check that subsequent handler is not parent of handler which had bare raise.
  461. # since utils.safe_infer can fail for bare except, check it before.
  462. # also break early if bare except is followed by bare except.
  463. excs_in_current_handler = gather_exceptions_from_handler(handler)
  464. if not excs_in_current_handler:
  465. break
  466. if exceptions_in_bare_handler is None:
  467. # It can be `None` when the inference failed
  468. break
  469. for exc_in_current_handler in excs_in_current_handler:
  470. inferred_current = utils.safe_infer(exc_in_current_handler)
  471. if any(
  472. utils.is_subclass_of(utils.safe_infer(e), inferred_current)
  473. for e in exceptions_in_bare_handler
  474. ):
  475. bare_raise = False
  476. break
  477. # `raise` as the first operator inside the except handler
  478. if _is_raising([handler.body[0]]):
  479. # flags when there is a bare raise
  480. if handler.body[0].exc is None:
  481. bare_raise = True
  482. handler_having_bare_raise = handler
  483. exceptions_in_bare_handler = gather_exceptions_from_handler(handler)
  484. else:
  485. if bare_raise:
  486. self.add_message("try-except-raise", node=handler_having_bare_raise)
  487. @utils.only_required_for_messages("wrong-exception-operation")
  488. def visit_binop(self, node: nodes.BinOp) -> None:
  489. if isinstance(node.parent, nodes.ExceptHandler):
  490. # except (V | A)
  491. suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
  492. self.add_message("wrong-exception-operation", node=node, args=(suggestion,))
  493. @utils.only_required_for_messages("wrong-exception-operation")
  494. def visit_compare(self, node: nodes.Compare) -> None:
  495. if isinstance(node.parent, nodes.ExceptHandler):
  496. # except (V < A)
  497. suggestion = (
  498. f"Did you mean '({node.left.as_string()}, "
  499. f"{', '.join(o.as_string() for _, o in node.ops)})' instead?"
  500. )
  501. self.add_message("wrong-exception-operation", node=node, args=(suggestion,))
  502. @utils.only_required_for_messages(
  503. "bare-except",
  504. "broad-exception-caught",
  505. "try-except-raise",
  506. "binary-op-exception",
  507. "bad-except-order",
  508. "catching-non-exception",
  509. "duplicate-except",
  510. )
  511. def visit_tryexcept(self, node: nodes.TryExcept) -> None:
  512. """Check for empty except."""
  513. self._check_try_except_raise(node)
  514. exceptions_classes: list[Any] = []
  515. nb_handlers = len(node.handlers)
  516. for index, handler in enumerate(node.handlers):
  517. if handler.type is None:
  518. if not _is_raising(handler.body):
  519. self.add_message("bare-except", node=handler, confidence=HIGH)
  520. # check if an "except:" is followed by some other
  521. # except
  522. if index < (nb_handlers - 1):
  523. msg = "empty except clause should always appear last"
  524. self.add_message(
  525. "bad-except-order", node=node, args=msg, confidence=HIGH
  526. )
  527. elif isinstance(handler.type, nodes.BoolOp):
  528. self.add_message(
  529. "binary-op-exception",
  530. node=handler,
  531. args=handler.type.op,
  532. confidence=HIGH,
  533. )
  534. else:
  535. try:
  536. exceptions = list(_annotated_unpack_infer(handler.type))
  537. except astroid.InferenceError:
  538. continue
  539. for part, exception in exceptions:
  540. if isinstance(
  541. exception, astroid.Instance
  542. ) and utils.inherit_from_std_ex(exception):
  543. exception = exception._proxied
  544. self._check_catching_non_exception(handler, exception, part)
  545. if not isinstance(exception, nodes.ClassDef):
  546. continue
  547. exc_ancestors = [
  548. anc
  549. for anc in exception.ancestors()
  550. if isinstance(anc, nodes.ClassDef)
  551. ]
  552. for previous_exc in exceptions_classes:
  553. if previous_exc in exc_ancestors:
  554. msg = f"{previous_exc.name} is an ancestor class of {exception.name}"
  555. self.add_message(
  556. "bad-except-order",
  557. node=handler.type,
  558. args=msg,
  559. confidence=INFERENCE,
  560. )
  561. if self._is_overgeneral_exception(exception) and not _is_raising(
  562. handler.body
  563. ):
  564. self.add_message(
  565. "broad-exception-caught",
  566. args=exception.name,
  567. node=handler.type,
  568. confidence=INFERENCE,
  569. )
  570. if exception in exceptions_classes:
  571. self.add_message(
  572. "duplicate-except",
  573. args=exception.name,
  574. node=handler.type,
  575. confidence=INFERENCE,
  576. )
  577. exceptions_classes += [exc for _, exc in exceptions]
  578. def _is_overgeneral_exception(self, exception: nodes.ClassDef) -> bool:
  579. return (
  580. exception.qname() in self.linter.config.overgeneral_exceptions
  581. # TODO: 3.0: not a qualified name, deprecated
  582. or "." not in exception.name
  583. and exception.name in self.linter.config.overgeneral_exceptions
  584. and exception.root().name == utils.EXCEPTIONS_MODULE
  585. )
  586. def register(linter: PyLinter) -> None:
  587. linter.register_checker(ExceptionsChecker(linter))