Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

overlapping_exceptions.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  3. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  4. """Looks for overlapping exceptions."""
  5. import astroid
  6. from pylint import interfaces
  7. from pylint import checkers
  8. from pylint.checkers import utils
  9. from pylint.checkers.exceptions import _annotated_unpack_infer
  10. class OverlappingExceptionsChecker(checkers.BaseChecker):
  11. """Checks for two or more exceptions in the same exception handler
  12. clause that are identical or parts of the same inheritance hierarchy
  13. (i.e. overlapping)."""
  14. __implements__ = interfaces.IAstroidChecker
  15. name = 'overlap-except'
  16. msgs = {'W0714': ('Overlapping exceptions (%s)',
  17. 'overlapping-except',
  18. 'Used when exceptions in handler overlap or are identical')}
  19. priority = -2
  20. options = ()
  21. @utils.check_messages('overlapping-except')
  22. def visit_tryexcept(self, node):
  23. """check for empty except"""
  24. for handler in node.handlers:
  25. if handler.type is None:
  26. continue
  27. if isinstance(handler.type, astroid.BoolOp):
  28. continue
  29. try:
  30. excs = list(_annotated_unpack_infer(handler.type))
  31. except astroid.InferenceError:
  32. continue
  33. handled_in_clause = []
  34. for part, exc in excs:
  35. if exc is astroid.YES:
  36. continue
  37. if (isinstance(exc, astroid.Instance) and
  38. utils.inherit_from_std_ex(exc)):
  39. # pylint: disable=protected-access
  40. exc = exc._proxied
  41. if not isinstance(exc, astroid.ClassDef):
  42. continue
  43. exc_ancestors = [anc for anc in exc.ancestors()
  44. if isinstance(anc, astroid.ClassDef)]
  45. for prev_part, prev_exc in handled_in_clause:
  46. prev_exc_ancestors = [anc for anc in prev_exc.ancestors()
  47. if isinstance(anc, astroid.ClassDef)]
  48. if exc == prev_exc:
  49. self.add_message('overlapping-except',
  50. node=handler.type,
  51. args='%s and %s are the same' %
  52. (prev_part.as_string(),
  53. part.as_string()))
  54. elif (prev_exc in exc_ancestors or
  55. exc in prev_exc_ancestors):
  56. ancestor = part if exc in prev_exc_ancestors else prev_part
  57. descendant = part if prev_exc in exc_ancestors else prev_part
  58. self.add_message('overlapping-except',
  59. node=handler.type,
  60. args='%s is an ancestor class of %s' %
  61. (ancestor.as_string(), descendant.as_string()))
  62. handled_in_clause += [(part, exc)]
  63. def register(linter):
  64. """Required method to auto register this checker."""
  65. linter.register_checker(OverlappingExceptionsChecker(linter))