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.

exceptions.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright (c) 2007, 2009-2010, 2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2014 Google, Inc.
  3. # Copyright (c) 2015-2016 Cara Vinson <ceridwenv@gmail.com>
  4. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  5. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  6. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  7. """this module contains exceptions used in the astroid library
  8. """
  9. from astroid import util
  10. class AstroidError(Exception):
  11. """base exception class for all astroid related exceptions
  12. AstroidError and its subclasses are structured, intended to hold
  13. objects representing state when the exception is thrown. Field
  14. values are passed to the constructor as keyword-only arguments.
  15. Each subclass has its own set of standard fields, but use your
  16. best judgment to decide whether a specific exception instance
  17. needs more or fewer fields for debugging. Field values may be
  18. used to lazily generate the error message: self.message.format()
  19. will be called with the field names and values supplied as keyword
  20. arguments.
  21. """
  22. def __init__(self, message='', **kws):
  23. super(AstroidError, self).__init__(message)
  24. self.message = message
  25. for key, value in kws.items():
  26. setattr(self, key, value)
  27. def __str__(self):
  28. return self.message.format(**vars(self))
  29. class AstroidBuildingError(AstroidError):
  30. """exception class when we are unable to build an astroid representation
  31. Standard attributes:
  32. modname: Name of the module that AST construction failed for.
  33. error: Exception raised during construction.
  34. """
  35. def __init__(self, message='Failed to import module {modname}.', **kws):
  36. super(AstroidBuildingError, self).__init__(message, **kws)
  37. class AstroidImportError(AstroidBuildingError):
  38. """Exception class used when a module can't be imported by astroid."""
  39. class TooManyLevelsError(AstroidImportError):
  40. """Exception class which is raised when a relative import was beyond the top-level.
  41. Standard attributes:
  42. level: The level which was attempted.
  43. name: the name of the module on which the relative import was attempted.
  44. """
  45. level = None
  46. name = None
  47. def __init__(self, message='Relative import with too many levels '
  48. '({level}) for module {name!r}', **kws):
  49. super(TooManyLevelsError, self).__init__(message, **kws)
  50. class AstroidSyntaxError(AstroidBuildingError):
  51. """Exception class used when a module can't be parsed."""
  52. class NoDefault(AstroidError):
  53. """raised by function's `default_value` method when an argument has
  54. no default value
  55. Standard attributes:
  56. func: Function node.
  57. name: Name of argument without a default.
  58. """
  59. func = None
  60. name = None
  61. def __init__(self, message='{func!r} has no default for {name!r}.', **kws):
  62. super(NoDefault, self).__init__(message, **kws)
  63. class ResolveError(AstroidError):
  64. """Base class of astroid resolution/inference error.
  65. ResolveError is not intended to be raised.
  66. Standard attributes:
  67. context: InferenceContext object.
  68. """
  69. context = None
  70. class MroError(ResolveError):
  71. """Error raised when there is a problem with method resolution of a class.
  72. Standard attributes:
  73. mros: A sequence of sequences containing ClassDef nodes.
  74. cls: ClassDef node whose MRO resolution failed.
  75. context: InferenceContext object.
  76. """
  77. mros = ()
  78. cls = None
  79. def __str__(self):
  80. mro_names = ", ".join("({})".format(", ".join(b.name for b in m))
  81. for m in self.mros)
  82. return self.message.format(mros=mro_names, cls=self.cls)
  83. class DuplicateBasesError(MroError):
  84. """Error raised when there are duplicate bases in the same class bases."""
  85. class InconsistentMroError(MroError):
  86. """Error raised when a class's MRO is inconsistent."""
  87. class SuperError(ResolveError):
  88. """Error raised when there is a problem with a super call.
  89. Standard attributes:
  90. super_: The Super instance that raised the exception.
  91. context: InferenceContext object.
  92. """
  93. super_ = None
  94. def __str__(self):
  95. return self.message.format(**vars(self.super_))
  96. class InferenceError(ResolveError):
  97. """raised when we are unable to infer a node
  98. Standard attributes:
  99. node: The node inference was called on.
  100. context: InferenceContext object.
  101. """
  102. node = None
  103. context = None
  104. def __init__(self, message='Inference failed for {node!r}.', **kws):
  105. super(InferenceError, self).__init__(message, **kws)
  106. # Why does this inherit from InferenceError rather than ResolveError?
  107. # Changing it causes some inference tests to fail.
  108. class NameInferenceError(InferenceError):
  109. """Raised when a name lookup fails, corresponds to NameError.
  110. Standard attributes:
  111. name: The name for which lookup failed, as a string.
  112. scope: The node representing the scope in which the lookup occurred.
  113. context: InferenceContext object.
  114. """
  115. name = None
  116. scope = None
  117. def __init__(self, message='{name!r} not found in {scope!r}.', **kws):
  118. super(NameInferenceError, self).__init__(message, **kws)
  119. class AttributeInferenceError(ResolveError):
  120. """Raised when an attribute lookup fails, corresponds to AttributeError.
  121. Standard attributes:
  122. target: The node for which lookup failed.
  123. attribute: The attribute for which lookup failed, as a string.
  124. context: InferenceContext object.
  125. """
  126. target = None
  127. attribute = None
  128. def __init__(self, message='{attribute!r} not found on {target!r}.', **kws):
  129. super(AttributeInferenceError, self).__init__(message, **kws)
  130. class UseInferenceDefault(Exception):
  131. """exception to be raised in custom inference function to indicate that it
  132. should go back to the default behaviour
  133. """
  134. class _NonDeducibleTypeHierarchy(Exception):
  135. """Raised when is_subtype / is_supertype can't deduce the relation between two types."""
  136. class AstroidIndexError(AstroidError):
  137. """Raised when an Indexable / Mapping does not have an index / key."""
  138. class AstroidTypeError(AstroidError):
  139. """Raised when a TypeError would be expected in Python code."""
  140. class InferenceOverwriteError(AstroidError):
  141. """Raised when an inference tip is overwritten
  142. Currently only used for debugging.
  143. """
  144. # Backwards-compatibility aliases
  145. OperationError = util.BadOperationMessage
  146. UnaryOperationError = util.BadUnaryOperationMessage
  147. BinaryOperationError = util.BadBinaryOperationMessage
  148. SuperArgumentTypeError = SuperError
  149. UnresolvableName = NameInferenceError
  150. NotFoundError = AttributeInferenceError
  151. AstroidBuildingException = AstroidBuildingError