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