exceptions.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. # Copyright (c) 2006-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2011-2014 Google, Inc.
  3. # Copyright (c) 2012 Tim Hatch <tim@timhatch.com>
  4. # Copyright (c) 2013-2020 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  6. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  7. # Copyright (c) 2015 Rene Zhang <rz99@cornell.edu>
  8. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  9. # Copyright (c) 2015 Steven Myint <hg@stevenmyint.com>
  10. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  11. # Copyright (c) 2016 Erik <erik.eriksson@yahoo.com>
  12. # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
  13. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  14. # Copyright (c) 2017 Martin von Gagern <gagern@google.com>
  15. # Copyright (c) 2018 Lucas Cimon <lucas.cimon@gmail.com>
  16. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  17. # Copyright (c) 2018 Natalie Serebryakova <natalie.serebryakova@Natalies-MacBook-Pro.local>
  18. # Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
  19. # Copyright (c) 2018 Carey Metcalfe <carey@cmetcalfe.ca>
  20. # Copyright (c) 2018 Mike Frysinger <vapier@gmail.com>
  21. # Copyright (c) 2018 Alexander Todorov <atodorov@otb.bg>
  22. # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
  23. # Copyright (c) 2019, 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  24. # Copyright (c) 2019 Djailla <bastien.vallet@gmail.com>
  25. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  26. # Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
  27. # Copyright (c) 2020 Ram Rachum <ram@rachum.com>
  28. # Copyright (c) 2020 Anthony Sottile <asottile@umich.edu>
  29. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  30. # Copyright (c) 2021 Nick Drozd <nicholasdrozd@gmail.com>
  31. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  32. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  33. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  34. """Checks for various exception related errors."""
  35. import builtins
  36. import inspect
  37. from typing import Any, List, Optional
  38. import astroid
  39. from astroid import nodes, objects
  40. from pylint import checkers, interfaces
  41. from pylint.checkers import utils
  42. def _builtin_exceptions():
  43. def predicate(obj):
  44. return isinstance(obj, type) and issubclass(obj, BaseException)
  45. members = inspect.getmembers(builtins, predicate)
  46. return {exc.__name__ for (_, exc) in members}
  47. def _annotated_unpack_infer(stmt, context=None):
  48. """
  49. Recursively generate nodes inferred by the given statement.
  50. If the inferred value is a list or a tuple, recurse on the elements.
  51. Returns an iterator which yields tuples in the format
  52. ('original node', 'inferred node').
  53. """
  54. if isinstance(stmt, (nodes.List, nodes.Tuple)):
  55. for elt in stmt.elts:
  56. inferred = utils.safe_infer(elt)
  57. if inferred and inferred is not astroid.Uninferable:
  58. yield elt, inferred
  59. return
  60. for inferred in stmt.infer(context):
  61. if inferred is astroid.Uninferable:
  62. continue
  63. yield stmt, inferred
  64. def _is_raising(body: List) -> bool:
  65. """Return whether the given statement node raises an exception"""
  66. return any(isinstance(node, nodes.Raise) for node in body)
  67. OVERGENERAL_EXCEPTIONS = ("BaseException", "Exception")
  68. MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
  69. "E0701": (
  70. "Bad except clauses order (%s)",
  71. "bad-except-order",
  72. "Used when except clauses are not in the correct order (from the "
  73. "more specific to the more generic). If you don't fix the order, "
  74. "some exceptions may not be caught by the most specific handler.",
  75. ),
  76. "E0702": (
  77. "Raising %s while only classes or instances are allowed",
  78. "raising-bad-type",
  79. "Used when something which is neither a class, an instance or a "
  80. "string is raised (i.e. a `TypeError` will be raised).",
  81. ),
  82. "E0703": (
  83. "Exception context set to something which is not an exception, nor None",
  84. "bad-exception-context",
  85. 'Used when using the syntax "raise ... from ...", '
  86. "where the exception context is not an exception, "
  87. "nor None.",
  88. ),
  89. "E0704": (
  90. "The raise statement is not inside an except clause",
  91. "misplaced-bare-raise",
  92. "Used when a bare raise is not used inside an except clause. "
  93. "This generates an error, since there are no active exceptions "
  94. "to be reraised. An exception to this rule is represented by "
  95. "a bare raise inside a finally clause, which might work, as long "
  96. "as an exception is raised inside the try block, but it is "
  97. "nevertheless a code smell that must not be relied upon.",
  98. ),
  99. "E0710": (
  100. "Raising a new style class which doesn't inherit from BaseException",
  101. "raising-non-exception",
  102. "Used when a new style class which doesn't inherit from "
  103. "BaseException is raised.",
  104. ),
  105. "E0711": (
  106. "NotImplemented raised - should raise NotImplementedError",
  107. "notimplemented-raised",
  108. "Used when NotImplemented is raised instead of NotImplementedError",
  109. ),
  110. "E0712": (
  111. "Catching an exception which doesn't inherit from Exception: %s",
  112. "catching-non-exception",
  113. "Used when a class which doesn't inherit from "
  114. "Exception is used as an exception in an except clause.",
  115. ),
  116. "W0702": (
  117. "No exception type(s) specified",
  118. "bare-except",
  119. "Used when an except clause doesn't specify exceptions type to catch.",
  120. ),
  121. "W0703": (
  122. "Catching too general exception %s",
  123. "broad-except",
  124. "Used when an except catches a too general exception, "
  125. "possibly burying unrelated errors.",
  126. ),
  127. "W0705": (
  128. "Catching previously caught exception type %s",
  129. "duplicate-except",
  130. "Used when an except catches a type that was already caught by "
  131. "a previous handler.",
  132. ),
  133. "W0706": (
  134. "The except handler raises immediately",
  135. "try-except-raise",
  136. "Used when an except handler uses raise as its first or only "
  137. "operator. This is useless because it raises back the exception "
  138. "immediately. Remove the raise operator or the entire "
  139. "try-except-raise block!",
  140. ),
  141. "W0707": (
  142. "Consider explicitly re-raising using the 'from' keyword",
  143. "raise-missing-from",
  144. "Python 3's exception chaining means it shows the traceback of the "
  145. "current exception, but also the original exception. Not using `raise "
  146. "from` makes the traceback inaccurate, because the message implies "
  147. "there is a bug in the exception-handling code itself, which is a "
  148. "separate situation than wrapping an exception.",
  149. ),
  150. "W0711": (
  151. 'Exception to catch is the result of a binary "%s" operation',
  152. "binary-op-exception",
  153. "Used when the exception to catch is of the form "
  154. '"except A or B:". If intending to catch multiple, '
  155. 'rewrite as "except (A, B):"',
  156. ),
  157. "W0715": (
  158. "Exception arguments suggest string formatting might be intended",
  159. "raising-format-tuple",
  160. "Used when passing multiple arguments to an exception "
  161. "constructor, the first of them a string literal containing what "
  162. "appears to be placeholders intended for formatting",
  163. ),
  164. "W0716": (
  165. "Invalid exception operation. %s",
  166. "wrong-exception-operation",
  167. "Used when an operation is done against an exception, but the operation "
  168. "is not valid for the exception in question. Usually emitted when having "
  169. "binary operations between exceptions in except handlers.",
  170. ),
  171. }
  172. class BaseVisitor:
  173. """Base class for visitors defined in this module."""
  174. def __init__(self, checker, node):
  175. self._checker = checker
  176. self._node = node
  177. def visit(self, node):
  178. name = node.__class__.__name__.lower()
  179. dispatch_meth = getattr(self, "visit_" + name, None)
  180. if dispatch_meth:
  181. dispatch_meth(node)
  182. else:
  183. self.visit_default(node)
  184. def visit_default(self, _: nodes.NodeNG) -> None:
  185. """Default implementation for all the nodes."""
  186. class ExceptionRaiseRefVisitor(BaseVisitor):
  187. """Visit references (anything that is not an AST leaf)."""
  188. def visit_name(self, node: nodes.Name) -> None:
  189. if node.name == "NotImplemented":
  190. self._checker.add_message("notimplemented-raised", node=self._node)
  191. def visit_call(self, node: nodes.Call) -> None:
  192. if isinstance(node.func, nodes.Name):
  193. self.visit_name(node.func)
  194. if (
  195. len(node.args) > 1
  196. and isinstance(node.args[0], nodes.Const)
  197. and isinstance(node.args[0].value, str)
  198. ):
  199. msg = node.args[0].value
  200. if "%" in msg or ("{" in msg and "}" in msg):
  201. self._checker.add_message("raising-format-tuple", node=self._node)
  202. class ExceptionRaiseLeafVisitor(BaseVisitor):
  203. """Visitor for handling leaf kinds of a raise value."""
  204. def visit_const(self, node: nodes.Const) -> None:
  205. self._checker.add_message(
  206. "raising-bad-type", node=self._node, args=node.value.__class__.__name__
  207. )
  208. def visit_instance(self, instance: objects.ExceptionInstance) -> None:
  209. cls = instance._proxied
  210. self.visit_classdef(cls)
  211. # Exception instances have a particular class type
  212. visit_exceptioninstance = visit_instance
  213. def visit_classdef(self, node: nodes.ClassDef) -> None:
  214. if not utils.inherit_from_std_ex(node) and utils.has_known_bases(node):
  215. if node.newstyle:
  216. self._checker.add_message("raising-non-exception", node=self._node)
  217. def visit_tuple(self, _: nodes.Tuple) -> None:
  218. self._checker.add_message("raising-bad-type", node=self._node, args="tuple")
  219. def visit_default(self, node: nodes.NodeNG) -> None:
  220. name = getattr(node, "name", node.__class__.__name__)
  221. self._checker.add_message("raising-bad-type", node=self._node, args=name)
  222. class ExceptionsChecker(checkers.BaseChecker):
  223. """Exception related checks."""
  224. __implements__ = interfaces.IAstroidChecker
  225. name = "exceptions"
  226. msgs = MSGS
  227. priority = -4
  228. options = (
  229. (
  230. "overgeneral-exceptions",
  231. {
  232. "default": OVERGENERAL_EXCEPTIONS,
  233. "type": "csv",
  234. "metavar": "<comma-separated class names>",
  235. "help": "Exceptions that will emit a warning " # pylint: disable=consider-using-f-string
  236. 'when being caught. Defaults to "%s".'
  237. % (", ".join(OVERGENERAL_EXCEPTIONS),),
  238. },
  239. ),
  240. )
  241. def open(self):
  242. self._builtin_exceptions = _builtin_exceptions()
  243. super().open()
  244. @utils.check_messages(
  245. "misplaced-bare-raise",
  246. "raising-bad-type",
  247. "raising-non-exception",
  248. "notimplemented-raised",
  249. "bad-exception-context",
  250. "raising-format-tuple",
  251. "raise-missing-from",
  252. )
  253. def visit_raise(self, node: nodes.Raise) -> None:
  254. if node.exc is None:
  255. self._check_misplaced_bare_raise(node)
  256. return
  257. if node.cause is None:
  258. self._check_raise_missing_from(node)
  259. else:
  260. self._check_bad_exception_context(node)
  261. expr = node.exc
  262. ExceptionRaiseRefVisitor(self, node).visit(expr)
  263. try:
  264. inferred_value = expr.inferred()[-1]
  265. except astroid.InferenceError:
  266. pass
  267. else:
  268. if inferred_value:
  269. ExceptionRaiseLeafVisitor(self, node).visit(inferred_value)
  270. def _check_misplaced_bare_raise(self, node):
  271. # Filter out if it's present in __exit__.
  272. scope = node.scope()
  273. if (
  274. isinstance(scope, nodes.FunctionDef)
  275. and scope.is_method()
  276. and scope.name == "__exit__"
  277. ):
  278. return
  279. current = node
  280. # Stop when a new scope is generated or when the raise
  281. # statement is found inside a TryFinally.
  282. ignores = (nodes.ExceptHandler, nodes.FunctionDef)
  283. while current and not isinstance(current.parent, ignores):
  284. current = current.parent
  285. expected = (nodes.ExceptHandler,)
  286. if not current or not isinstance(current.parent, expected):
  287. self.add_message("misplaced-bare-raise", node=node)
  288. def _check_bad_exception_context(self, node: nodes.Raise) -> None:
  289. """Verify that the exception context is properly set.
  290. An exception context can be only `None` or an exception.
  291. """
  292. cause = utils.safe_infer(node.cause)
  293. if cause in (astroid.Uninferable, None):
  294. return
  295. if isinstance(cause, nodes.Const):
  296. if cause.value is not None:
  297. self.add_message("bad-exception-context", node=node)
  298. elif not isinstance(cause, nodes.ClassDef) and not utils.inherit_from_std_ex(
  299. cause
  300. ):
  301. self.add_message("bad-exception-context", node=node)
  302. def _check_raise_missing_from(self, node: nodes.Raise) -> None:
  303. if node.exc is None:
  304. # This is a plain `raise`, raising the previously-caught exception. No need for a
  305. # cause.
  306. return
  307. # We'd like to check whether we're inside an `except` clause:
  308. containing_except_node = utils.find_except_wrapper_node_in_scope(node)
  309. if not containing_except_node:
  310. return
  311. # We found a surrounding `except`! We're almost done proving there's a
  312. # `raise-missing-from` here. The only thing we need to protect against is that maybe
  313. # the `raise` is raising the exception that was caught, possibly with some shenanigans
  314. # like `exc.with_traceback(whatever)`. We won't analyze these, we'll just assume
  315. # there's a violation on two simple cases: `raise SomeException(whatever)` and `raise
  316. # SomeException`.
  317. if containing_except_node.name is None:
  318. # The `except` doesn't have an `as exception:` part, meaning there's no way that
  319. # the `raise` is raising the same exception.
  320. self.add_message("raise-missing-from", node=node)
  321. elif isinstance(node.exc, nodes.Call) and isinstance(node.exc.func, nodes.Name):
  322. # We have a `raise SomeException(whatever)`.
  323. self.add_message("raise-missing-from", node=node)
  324. elif (
  325. isinstance(node.exc, nodes.Name)
  326. and node.exc.name != containing_except_node.name.name
  327. ):
  328. # We have a `raise SomeException`.
  329. self.add_message("raise-missing-from", node=node)
  330. def _check_catching_non_exception(self, handler, exc, part):
  331. if isinstance(exc, nodes.Tuple):
  332. # Check if it is a tuple of exceptions.
  333. inferred = [utils.safe_infer(elt) for elt in exc.elts]
  334. if any(node is astroid.Uninferable for node in inferred):
  335. # Don't emit if we don't know every component.
  336. return
  337. if all(
  338. node
  339. and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
  340. for node in inferred
  341. ):
  342. return
  343. if not isinstance(exc, nodes.ClassDef):
  344. # Don't emit the warning if the inferred stmt
  345. # is None, but the exception handler is something else,
  346. # maybe it was redefined.
  347. if isinstance(exc, nodes.Const) and exc.value is None:
  348. if (
  349. isinstance(handler.type, nodes.Const) and handler.type.value is None
  350. ) or handler.type.parent_of(exc):
  351. # If the exception handler catches None or
  352. # the exception component, which is None, is
  353. # defined by the entire exception handler, then
  354. # emit a warning.
  355. self.add_message(
  356. "catching-non-exception",
  357. node=handler.type,
  358. args=(part.as_string(),),
  359. )
  360. else:
  361. self.add_message(
  362. "catching-non-exception",
  363. node=handler.type,
  364. args=(part.as_string(),),
  365. )
  366. return
  367. if (
  368. not utils.inherit_from_std_ex(exc)
  369. and exc.name not in self._builtin_exceptions
  370. ):
  371. if utils.has_known_bases(exc):
  372. self.add_message(
  373. "catching-non-exception", node=handler.type, args=(exc.name,)
  374. )
  375. def _check_try_except_raise(self, node):
  376. def gather_exceptions_from_handler(
  377. handler,
  378. ) -> Optional[List[nodes.NodeNG]]:
  379. exceptions: List[nodes.NodeNG] = []
  380. if handler.type:
  381. exceptions_in_handler = utils.safe_infer(handler.type)
  382. if isinstance(exceptions_in_handler, nodes.Tuple):
  383. exceptions = list(
  384. {
  385. exception
  386. for exception in exceptions_in_handler.elts
  387. if isinstance(exception, nodes.Name)
  388. }
  389. )
  390. elif exceptions_in_handler:
  391. exceptions = [exceptions_in_handler]
  392. else:
  393. # Break when we cannot infer anything reliably.
  394. return None
  395. return exceptions
  396. bare_raise = False
  397. handler_having_bare_raise = None
  398. excs_in_bare_handler = []
  399. for handler in node.handlers:
  400. if bare_raise:
  401. # check that subsequent handler is not parent of handler which had bare raise.
  402. # since utils.safe_infer can fail for bare except, check it before.
  403. # also break early if bare except is followed by bare except.
  404. excs_in_current_handler = gather_exceptions_from_handler(handler)
  405. if not excs_in_current_handler:
  406. break
  407. if excs_in_bare_handler is None:
  408. # It can be `None` when the inference failed
  409. break
  410. for exc_in_current_handler in excs_in_current_handler:
  411. inferred_current = utils.safe_infer(exc_in_current_handler)
  412. if any(
  413. utils.is_subclass_of(utils.safe_infer(e), inferred_current)
  414. for e in excs_in_bare_handler
  415. ):
  416. bare_raise = False
  417. break
  418. # `raise` as the first operator inside the except handler
  419. if _is_raising([handler.body[0]]):
  420. # flags when there is a bare raise
  421. if handler.body[0].exc is None:
  422. bare_raise = True
  423. handler_having_bare_raise = handler
  424. excs_in_bare_handler = gather_exceptions_from_handler(handler)
  425. else:
  426. if bare_raise:
  427. self.add_message("try-except-raise", node=handler_having_bare_raise)
  428. @utils.check_messages("wrong-exception-operation")
  429. def visit_binop(self, node: nodes.BinOp) -> None:
  430. if isinstance(node.parent, nodes.ExceptHandler):
  431. # except (V | A)
  432. suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
  433. self.add_message("wrong-exception-operation", node=node, args=(suggestion,))
  434. @utils.check_messages("wrong-exception-operation")
  435. def visit_compare(self, node: nodes.Compare) -> None:
  436. if isinstance(node.parent, nodes.ExceptHandler):
  437. # except (V < A)
  438. suggestion = f"Did you mean '({node.left.as_string()}, {', '.join(operand.as_string() for _, operand in node.ops)})' instead?"
  439. self.add_message("wrong-exception-operation", node=node, args=(suggestion,))
  440. @utils.check_messages(
  441. "bare-except",
  442. "broad-except",
  443. "try-except-raise",
  444. "binary-op-exception",
  445. "bad-except-order",
  446. "catching-non-exception",
  447. "duplicate-except",
  448. )
  449. def visit_tryexcept(self, node: nodes.TryExcept) -> None:
  450. """check for empty except"""
  451. self._check_try_except_raise(node)
  452. exceptions_classes: List[Any] = []
  453. nb_handlers = len(node.handlers)
  454. for index, handler in enumerate(node.handlers):
  455. if handler.type is None:
  456. if not _is_raising(handler.body):
  457. self.add_message("bare-except", node=handler)
  458. # check if an "except:" is followed by some other
  459. # except
  460. if index < (nb_handlers - 1):
  461. msg = "empty except clause should always appear last"
  462. self.add_message("bad-except-order", node=node, args=msg)
  463. elif isinstance(handler.type, nodes.BoolOp):
  464. self.add_message(
  465. "binary-op-exception", node=handler, args=handler.type.op
  466. )
  467. else:
  468. try:
  469. excs = list(_annotated_unpack_infer(handler.type))
  470. except astroid.InferenceError:
  471. continue
  472. for part, exc in excs:
  473. if exc is astroid.Uninferable:
  474. continue
  475. if isinstance(exc, astroid.Instance) and utils.inherit_from_std_ex(
  476. exc
  477. ):
  478. exc = exc._proxied
  479. self._check_catching_non_exception(handler, exc, part)
  480. if not isinstance(exc, nodes.ClassDef):
  481. continue
  482. exc_ancestors = [
  483. anc
  484. for anc in exc.ancestors()
  485. if isinstance(anc, nodes.ClassDef)
  486. ]
  487. for previous_exc in exceptions_classes:
  488. if previous_exc in exc_ancestors:
  489. msg = f"{previous_exc.name} is an ancestor class of {exc.name}"
  490. self.add_message(
  491. "bad-except-order", node=handler.type, args=msg
  492. )
  493. if (
  494. exc.name in self.config.overgeneral_exceptions
  495. and exc.root().name == utils.EXCEPTIONS_MODULE
  496. and not _is_raising(handler.body)
  497. ):
  498. self.add_message(
  499. "broad-except", args=exc.name, node=handler.type
  500. )
  501. if exc in exceptions_classes:
  502. self.add_message(
  503. "duplicate-except", args=exc.name, node=handler.type
  504. )
  505. exceptions_classes += [exc for _, exc in excs]
  506. def register(linter):
  507. """required method to auto register this checker"""
  508. linter.register_checker(ExceptionsChecker(linter))