__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. """Python Abstract Syntax Tree New Generation.
  5. The aim of this module is to provide a common base representation of
  6. python source code for projects such as pychecker, pyreverse,
  7. pylint... Well, actually the development of this library is essentially
  8. governed by pylint's needs.
  9. It mimics the class defined in the python's _ast module with some
  10. additional methods and attributes. New nodes instances are not fully
  11. compatible with python's _ast.
  12. Instance attributes are added by a
  13. builder object, which can either generate extended ast (let's call
  14. them astroid ;) by visiting an existent ast tree or by inspecting living
  15. object. Methods are added by monkey patching ast classes.
  16. Main modules are:
  17. * nodes and scoped_nodes for more information about methods and
  18. attributes added to different node classes
  19. * the manager contains a high level object to get astroid trees from
  20. source files and living objects. It maintains a cache of previously
  21. constructed tree for quick access
  22. * builder contains the class responsible to build astroid trees
  23. """
  24. import functools
  25. import tokenize
  26. from importlib import import_module
  27. # isort: off
  28. # We have an isort: off on '__version__' because the packaging need to access
  29. # the version before the dependencies are installed (in particular 'wrapt'
  30. # that is imported in astroid.inference)
  31. from astroid.__pkginfo__ import __version__, version
  32. from astroid.nodes import node_classes, scoped_nodes
  33. # isort: on
  34. from astroid import inference, raw_building
  35. from astroid.astroid_manager import MANAGER
  36. from astroid.bases import BaseInstance, BoundMethod, Instance, UnboundMethod
  37. from astroid.brain.helpers import register_module_extender
  38. from astroid.builder import extract_node, parse
  39. from astroid.const import BRAIN_MODULES_DIRECTORY, PY310_PLUS, Context, Del, Load, Store
  40. from astroid.exceptions import (
  41. AstroidBuildingError,
  42. AstroidBuildingException,
  43. AstroidError,
  44. AstroidImportError,
  45. AstroidIndexError,
  46. AstroidSyntaxError,
  47. AstroidTypeError,
  48. AstroidValueError,
  49. AttributeInferenceError,
  50. BinaryOperationError,
  51. DuplicateBasesError,
  52. InconsistentMroError,
  53. InferenceError,
  54. InferenceOverwriteError,
  55. MroError,
  56. NameInferenceError,
  57. NoDefault,
  58. NotFoundError,
  59. OperationError,
  60. ParentMissingError,
  61. ResolveError,
  62. StatementMissing,
  63. SuperArgumentTypeError,
  64. SuperError,
  65. TooManyLevelsError,
  66. UnaryOperationError,
  67. UnresolvableName,
  68. UseInferenceDefault,
  69. )
  70. from astroid.inference_tip import _inference_tip_cached, inference_tip
  71. from astroid.objects import ExceptionInstance
  72. # isort: off
  73. # It's impossible to import from astroid.nodes with a wildcard, because
  74. # there is a cyclic import that prevent creating an __all__ in astroid/nodes
  75. # and we need astroid/scoped_nodes and astroid/node_classes to work. So
  76. # importing with a wildcard would clash with astroid/nodes/scoped_nodes
  77. # and astroid/nodes/node_classes.
  78. from astroid.nodes import ( # pylint: disable=redefined-builtin (Ellipsis)
  79. CONST_CLS,
  80. AnnAssign,
  81. Arguments,
  82. Assert,
  83. Assign,
  84. AssignAttr,
  85. AssignName,
  86. AsyncFor,
  87. AsyncFunctionDef,
  88. AsyncWith,
  89. Attribute,
  90. AugAssign,
  91. Await,
  92. BinOp,
  93. BoolOp,
  94. Break,
  95. Call,
  96. ClassDef,
  97. Compare,
  98. Comprehension,
  99. ComprehensionScope,
  100. Const,
  101. Continue,
  102. Decorators,
  103. DelAttr,
  104. Delete,
  105. DelName,
  106. Dict,
  107. DictComp,
  108. DictUnpack,
  109. Ellipsis,
  110. EmptyNode,
  111. EvaluatedObject,
  112. ExceptHandler,
  113. Expr,
  114. ExtSlice,
  115. For,
  116. FormattedValue,
  117. FunctionDef,
  118. GeneratorExp,
  119. Global,
  120. If,
  121. IfExp,
  122. Import,
  123. ImportFrom,
  124. Index,
  125. JoinedStr,
  126. Keyword,
  127. Lambda,
  128. List,
  129. ListComp,
  130. Match,
  131. MatchAs,
  132. MatchCase,
  133. MatchClass,
  134. MatchMapping,
  135. MatchOr,
  136. MatchSequence,
  137. MatchSingleton,
  138. MatchStar,
  139. MatchValue,
  140. Module,
  141. Name,
  142. NamedExpr,
  143. NodeNG,
  144. Nonlocal,
  145. Pass,
  146. Raise,
  147. Return,
  148. Set,
  149. SetComp,
  150. Slice,
  151. Starred,
  152. Subscript,
  153. TryExcept,
  154. TryFinally,
  155. TryStar,
  156. Tuple,
  157. UnaryOp,
  158. Unknown,
  159. While,
  160. With,
  161. Yield,
  162. YieldFrom,
  163. are_exclusive,
  164. builtin_lookup,
  165. unpack_infer,
  166. function_to_method,
  167. )
  168. # isort: on
  169. from astroid.util import Uninferable
  170. # Performance hack for tokenize. See https://bugs.python.org/issue43014
  171. # Adapted from https://github.com/PyCQA/pycodestyle/pull/993
  172. if (
  173. not PY310_PLUS
  174. and callable(getattr(tokenize, "_compile", None))
  175. and getattr(tokenize._compile, "__wrapped__", None) is None # type: ignore[attr-defined]
  176. ):
  177. tokenize._compile = functools.lru_cache()(tokenize._compile) # type: ignore[attr-defined]
  178. # load brain plugins
  179. for module in BRAIN_MODULES_DIRECTORY.iterdir():
  180. if module.suffix == ".py":
  181. import_module(f"astroid.brain.{module.stem}")