|
| 1 | +# Copyright (c) 2025-2026 Splunk Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 15 | +# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 16 | +# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 17 | + |
| 18 | +"""Python Abstract Syntax Tree New Generation. |
| 19 | +
|
| 20 | +The aim of this module is to provide a common base representation of |
| 21 | +python source code for projects such as pychecker, pyreverse, |
| 22 | +pylint... Well, actually the development of this library is essentially |
| 23 | +governed by pylint's needs. |
| 24 | +
|
| 25 | +It mimics the class defined in the python's _ast module with some |
| 26 | +additional methods and attributes. New nodes instances are not fully |
| 27 | +compatible with python's _ast. |
| 28 | +
|
| 29 | +Instance attributes are added by a |
| 30 | +builder object, which can either generate extended ast (let's call |
| 31 | +them astroid ;) by visiting an existent ast tree or by inspecting living |
| 32 | +object. |
| 33 | +
|
| 34 | +Main modules are: |
| 35 | +
|
| 36 | +* nodes and scoped_nodes for more information about methods and |
| 37 | + attributes added to different node classes |
| 38 | +
|
| 39 | +* the manager contains a high level object to get astroid trees from |
| 40 | + source files and living objects. It maintains a cache of previously |
| 41 | + constructed tree for quick access |
| 42 | +
|
| 43 | +* builder contains the class responsible to build astroid trees |
| 44 | +""" |
| 45 | + |
| 46 | +# isort: off |
| 47 | +# We have an isort: off on 'astroid.nodes' because of a circular import. |
| 48 | +from astroid.nodes import node_classes, scoped_nodes |
| 49 | + |
| 50 | +# isort: on |
| 51 | + |
| 52 | +from astroid import raw_building |
| 53 | +from astroid.__pkginfo__ import __version__, version |
| 54 | +from astroid.bases import BaseInstance, BoundMethod, Instance, UnboundMethod |
| 55 | +from astroid.brain.helpers import register_module_extender |
| 56 | +from astroid.builder import extract_node, parse |
| 57 | +from astroid.const import Context |
| 58 | +from astroid.exceptions import ( |
| 59 | + AstroidBuildingError, |
| 60 | + AstroidError, |
| 61 | + AstroidImportError, |
| 62 | + AstroidIndexError, |
| 63 | + AstroidSyntaxError, |
| 64 | + AstroidTypeError, |
| 65 | + AstroidValueError, |
| 66 | + AttributeInferenceError, |
| 67 | + DuplicateBasesError, |
| 68 | + InconsistentMroError, |
| 69 | + InferenceError, |
| 70 | + InferenceOverwriteError, |
| 71 | + MroError, |
| 72 | + NameInferenceError, |
| 73 | + NoDefault, |
| 74 | + NotFoundError, |
| 75 | + ParentMissingError, |
| 76 | + ResolveError, |
| 77 | + StatementMissing, |
| 78 | + SuperArgumentTypeError, |
| 79 | + SuperError, |
| 80 | + TooManyLevelsError, |
| 81 | + UnresolvableName, |
| 82 | + UseInferenceDefault, |
| 83 | +) |
| 84 | +from astroid.inference_tip import _inference_tip_cached, inference_tip |
| 85 | +from astroid.objects import ExceptionInstance |
| 86 | + |
| 87 | +# isort: off |
| 88 | +# It's impossible to import from astroid.nodes with a wildcard, because |
| 89 | +# there is a cyclic import that prevent creating an __all__ in astroid/nodes |
| 90 | +# and we need astroid/scoped_nodes and astroid/node_classes to work. So |
| 91 | +# importing with a wildcard would clash with astroid/nodes/scoped_nodes |
| 92 | +# and astroid/nodes/node_classes. |
| 93 | +from astroid.astroid_manager import MANAGER |
| 94 | +from astroid.nodes import ( |
| 95 | + CONST_CLS, |
| 96 | + AnnAssign as _DEPRECATED_AnnAssign, |
| 97 | + Arguments as _DEPRECATED_Arguments, |
| 98 | + Assert as _DEPRECATED_Assert, |
| 99 | + Assign as _DEPRECATED_Assign, |
| 100 | + AssignAttr as _DEPRECATED_AssignAttr, |
| 101 | + AssignName as _DEPRECATED_AssignName, |
| 102 | + AsyncFor as _DEPRECATED_AsyncFor, |
| 103 | + AsyncFunctionDef as _DEPRECATED_AsyncFunctionDef, |
| 104 | + AsyncWith as _DEPRECATED_AsyncWith, |
| 105 | + Attribute as _DEPRECATED_Attribute, |
| 106 | + AugAssign as _DEPRECATED_AugAssign, |
| 107 | + Await as _DEPRECATED_Await, |
| 108 | + BinOp as _DEPRECATED_BinOp, |
| 109 | + BoolOp as _DEPRECATED_BoolOp, |
| 110 | + Break as _DEPRECATED_Break, |
| 111 | + Call as _DEPRECATED_Call, |
| 112 | + ClassDef as _DEPRECATED_ClassDef, |
| 113 | + Compare as _DEPRECATED_Compare, |
| 114 | + Comprehension as _DEPRECATED_Comprehension, |
| 115 | + ComprehensionScope as _DEPRECATED_ComprehensionScope, |
| 116 | + Const as _DEPRECATED_Const, |
| 117 | + Continue as _DEPRECATED_Continue, |
| 118 | + Decorators as _DEPRECATED_Decorators, |
| 119 | + DelAttr as _DEPRECATED_DelAttr, |
| 120 | + Delete as _DEPRECATED_Delete, |
| 121 | + DelName as _DEPRECATED_DelName, |
| 122 | + Dict as _DEPRECATED_Dict, |
| 123 | + DictComp as _DEPRECATED_DictComp, |
| 124 | + DictUnpack as _DEPRECATED_DictUnpack, |
| 125 | + EmptyNode as _DEPRECATED_EmptyNode, |
| 126 | + EvaluatedObject as _DEPRECATED_EvaluatedObject, |
| 127 | + ExceptHandler as _DEPRECATED_ExceptHandler, |
| 128 | + Expr as _DEPRECATED_Expr, |
| 129 | + For as _DEPRECATED_For, |
| 130 | + FormattedValue as _DEPRECATED_FormattedValue, |
| 131 | + FunctionDef as _DEPRECATED_FunctionDef, |
| 132 | + GeneratorExp as _DEPRECATED_GeneratorExp, |
| 133 | + Global as _DEPRECATED_Global, |
| 134 | + If as _DEPRECATED_If, |
| 135 | + IfExp as _DEPRECATED_IfExp, |
| 136 | + Import as _DEPRECATED_Import, |
| 137 | + ImportFrom as _DEPRECATED_ImportFrom, |
| 138 | + Interpolation as _DEPRECATED_Interpolation, |
| 139 | + JoinedStr as _DEPRECATED_JoinedStr, |
| 140 | + Keyword as _DEPRECATED_Keyword, |
| 141 | + Lambda as _DEPRECATED_Lambda, |
| 142 | + List as _DEPRECATED_List, |
| 143 | + ListComp as _DEPRECATED_ListComp, |
| 144 | + Match as _DEPRECATED_Match, |
| 145 | + MatchAs as _DEPRECATED_MatchAs, |
| 146 | + MatchCase as _DEPRECATED_MatchCase, |
| 147 | + MatchClass as _DEPRECATED_MatchClass, |
| 148 | + MatchMapping as _DEPRECATED_MatchMapping, |
| 149 | + MatchOr as _DEPRECATED_MatchOr, |
| 150 | + MatchSequence as _DEPRECATED_MatchSequence, |
| 151 | + MatchSingleton as _DEPRECATED_MatchSingleton, |
| 152 | + MatchStar as _DEPRECATED_MatchStar, |
| 153 | + MatchValue as _DEPRECATED_MatchValue, |
| 154 | + Module as _DEPRECATED_Module, |
| 155 | + Name as _DEPRECATED_Name, |
| 156 | + NamedExpr as _DEPRECATED_NamedExpr, |
| 157 | + NodeNG as _DEPRECATED_NodeNG, |
| 158 | + Nonlocal as _DEPRECATED_Nonlocal, |
| 159 | + ParamSpec as _DEPRECATED_ParamSpec, |
| 160 | + Pass as _DEPRECATED_Pass, |
| 161 | + Raise as _DEPRECATED_Raise, |
| 162 | + Return as _DEPRECATED_Return, |
| 163 | + Set as _DEPRECATED_Set, |
| 164 | + SetComp as _DEPRECATED_SetComp, |
| 165 | + Slice as _DEPRECATED_Slice, |
| 166 | + Starred as _DEPRECATED_Starred, |
| 167 | + Subscript as _DEPRECATED_Subscript, |
| 168 | + TemplateStr as _DEPRECATED_TemplateStr, |
| 169 | + Try as _DEPRECATED_Try, |
| 170 | + TryStar as _DEPRECATED_TryStar, |
| 171 | + Tuple as _DEPRECATED_Tuple, |
| 172 | + TypeAlias as _DEPRECATED_TypeAlias, |
| 173 | + TypeVar as _DEPRECATED_TypeVar, |
| 174 | + TypeVarTuple as _DEPRECATED_TypeVarTuple, |
| 175 | + UnaryOp as _DEPRECATED_UnaryOp, |
| 176 | + Unknown as _DEPRECATED_Unknown, |
| 177 | + While as _DEPRECATED_While, |
| 178 | + With as _DEPRECATED_With, |
| 179 | + Yield as _DEPRECATED_Yield, |
| 180 | + YieldFrom as _DEPRECATED_YieldFrom, |
| 181 | + are_exclusive, |
| 182 | + builtin_lookup, |
| 183 | + unpack_infer, |
| 184 | + function_to_method, |
| 185 | +) |
| 186 | + |
| 187 | +# isort: on |
| 188 | + |
| 189 | +from astroid.util import Uninferable |
| 190 | + |
| 191 | +__all__ = [ |
| 192 | + "CONST_CLS", |
| 193 | + "MANAGER", |
| 194 | + "AstroidBuildingError", |
| 195 | + "AstroidError", |
| 196 | + "AstroidImportError", |
| 197 | + "AstroidIndexError", |
| 198 | + "AstroidSyntaxError", |
| 199 | + "AstroidTypeError", |
| 200 | + "AstroidValueError", |
| 201 | + "AttributeInferenceError", |
| 202 | + "BaseInstance", |
| 203 | + "BoundMethod", |
| 204 | + "Context", |
| 205 | + "DuplicateBasesError", |
| 206 | + "ExceptionInstance", |
| 207 | + "InconsistentMroError", |
| 208 | + "InferenceError", |
| 209 | + "InferenceOverwriteError", |
| 210 | + "Instance", |
| 211 | + "MroError", |
| 212 | + "NameInferenceError", |
| 213 | + "NoDefault", |
| 214 | + "NotFoundError", |
| 215 | + "ParentMissingError", |
| 216 | + "ResolveError", |
| 217 | + "StatementMissing", |
| 218 | + "SuperArgumentTypeError", |
| 219 | + "SuperError", |
| 220 | + "TooManyLevelsError", |
| 221 | + "UnboundMethod", |
| 222 | + "Uninferable", |
| 223 | + "UnresolvableName", |
| 224 | + "UseInferenceDefault", |
| 225 | + "__version__", |
| 226 | + "_inference_tip_cached", |
| 227 | + "are_exclusive", |
| 228 | + "builtin_lookup", |
| 229 | + "extract_node", |
| 230 | + "function_to_method", |
| 231 | + "inference_tip", |
| 232 | + "node_classes", |
| 233 | + "parse", |
| 234 | + "raw_building", |
| 235 | + "register_module_extender", |
| 236 | + "scoped_nodes", |
| 237 | + "unpack_infer", |
| 238 | + "version", |
| 239 | +] |
| 240 | + |
| 241 | + |
| 242 | +def __getattr__(name: str): |
| 243 | + if (val := globals().get(f"_DEPRECATED_{name}")) is None: |
| 244 | + msg = f"module '{__name__}' has no attribute '{name}" |
| 245 | + raise AttributeError(msg) |
| 246 | + |
| 247 | + # pylint: disable-next=import-outside-toplevel |
| 248 | + import warnings |
| 249 | + |
| 250 | + msg = ( |
| 251 | + f"importing '{name}' from 'astroid' is deprecated and will be removed in v5, " |
| 252 | + "import it from 'astroid.nodes' instead" |
| 253 | + ) |
| 254 | + warnings.warn(msg, DeprecationWarning, stacklevel=2) |
| 255 | + return val |
0 commit comments