0
# AST Node Classes
1
2
Comprehensive set of node classes representing all Python language constructs. These classes extend Python's built-in AST nodes with additional methods for static analysis, inference, and code introspection.
3
4
## Capabilities
5
6
### Base Node Classes
7
8
Foundation classes that all astroid nodes inherit from, providing core functionality for traversal, inference, and analysis.
9
10
```python { .api }
11
class NodeNG:
12
"""Base class for all astroid nodes."""
13
14
def infer(self, context: InferenceContext | None = None) -> Iterator[InferenceResult]:
15
"""
16
Get possible inferred values for this node.
17
18
Parameters:
19
- context: Inference context for caching and control
20
21
Yields:
22
Possible inferred values (nodes or objects)
23
24
Raises:
25
InferenceError: When inference fails
26
"""
27
28
def as_string(self) -> str:
29
"""Return string representation of the node."""
30
31
def accept(self, visitor) -> Any:
32
"""Accept a visitor and call appropriate visit method."""
33
34
def get_children(self) -> Iterator[NodeNG]:
35
"""Yield direct child nodes."""
36
37
def nodes_of_class(self, klass: type[NodeNG]) -> Iterator[NodeNG]:
38
"""Yield descendant nodes of specified class."""
39
40
def scope(self) -> LocalsDictNodeNG:
41
"""Return the first parent node defining a local scope."""
42
43
def statement(self) -> NodeNG:
44
"""Return the first parent node that is a statement."""
45
46
class LocalsDictNodeNG(NodeNG):
47
"""Base class for nodes that define local scopes."""
48
49
def keys(self) -> Iterator[str]:
50
"""Yield names defined in this scope."""
51
52
def values(self) -> Iterator[list[NodeNG]]:
53
"""Yield lists of nodes for each name."""
54
55
def items(self) -> Iterator[tuple[str, list[NodeNG]]]:
56
"""Yield (name, nodes) pairs."""
57
58
def __getitem__(self, name: str) -> list[NodeNG]:
59
"""Get nodes for a given name."""
60
61
def __contains__(self, name: str) -> bool:
62
"""Check if name is defined in this scope."""
63
```
64
65
### Scoped Nodes
66
67
Nodes that define scopes and contain local variable definitions.
68
69
```python { .api }
70
class Module(LocalsDictNodeNG):
71
"""Module/file level scope."""
72
73
name: str
74
doc: str | None
75
file: str | None
76
package: bool
77
78
def wildcard_import_names(self) -> list[str]:
79
"""Get names imported by 'from module import *'."""
80
81
def public_names(self) -> list[str]:
82
"""Get public names (not starting with underscore)."""
83
84
class ClassDef(LocalsDictNodeNG):
85
"""Class definition."""
86
87
name: str
88
bases: list[NodeNG]
89
decorators: Decorators | None
90
doc: str | None
91
92
def mro(self) -> list[ClassDef]:
93
"""Get method resolution order."""
94
95
def ancestors(self, recurs: bool = True, context: InferenceContext | None = None) -> Iterator[ClassDef]:
96
"""Yield ancestor classes."""
97
98
def local_attr(self, name: str, context: InferenceContext | None = None) -> list[NodeNG]:
99
"""Get local attribute nodes."""
100
101
class FunctionDef(LocalsDictNodeNG):
102
"""Function definition."""
103
104
name: str
105
args: Arguments
106
returns: NodeNG | None
107
decorators: Decorators | None
108
doc: str | None
109
110
def argnames(self) -> list[str]:
111
"""Get argument names."""
112
113
def is_method(self) -> bool:
114
"""Check if this is a method."""
115
116
def is_abstract(self) -> bool:
117
"""Check if this is an abstract method."""
118
119
class AsyncFunctionDef(FunctionDef):
120
"""Async function definition."""
121
122
class Lambda(LocalsDictNodeNG):
123
"""Lambda expression."""
124
125
args: Arguments
126
body: NodeNG
127
```
128
129
### Expression Nodes
130
131
Nodes representing Python expressions and values.
132
133
```python { .api }
134
class Const:
135
"""Constant values (strings, numbers, None, etc.)."""
136
137
value: Any
138
139
def itered(self) -> list[NodeNG]:
140
"""Get iteration elements for iterable constants."""
141
142
class Name:
143
"""Variable/identifier references."""
144
145
name: str
146
ctx: Context # Load, Store, Del
147
148
class Attribute:
149
"""Attribute access (obj.attr)."""
150
151
expr: NodeNG
152
attrname: str
153
ctx: Context
154
155
class Subscript:
156
"""Subscription (obj[key])."""
157
158
value: NodeNG
159
slice: NodeNG
160
ctx: Context
161
162
class Call:
163
"""Function/method calls."""
164
165
func: NodeNG
166
args: list[NodeNG]
167
keywords: list[Keyword]
168
169
def infer_call_result(self, context: InferenceContext | None = None) -> Iterator[InferenceResult]:
170
"""Infer the result of this call."""
171
172
class BinOp:
173
"""Binary operations (+, -, *, etc.)."""
174
175
left: NodeNG
176
op: str
177
right: NodeNG
178
179
class UnaryOp:
180
"""Unary operations (-, +, ~, not)."""
181
182
op: str
183
operand: NodeNG
184
185
class Compare:
186
"""Comparison operations."""
187
188
left: NodeNG
189
ops: list[tuple[str, NodeNG]]
190
191
class BoolOp:
192
"""Boolean operations (and, or)."""
193
194
op: str
195
values: list[NodeNG]
196
```
197
198
### Container Nodes
199
200
Nodes representing Python containers and data structures.
201
202
```python { .api }
203
class List:
204
"""List literals."""
205
206
elts: list[NodeNG]
207
ctx: Context
208
209
class Tuple:
210
"""Tuple literals."""
211
212
elts: list[NodeNG]
213
ctx: Context
214
215
class Set:
216
"""Set literals."""
217
218
elts: list[NodeNG]
219
220
class Dict:
221
"""Dictionary literals."""
222
223
keys: list[NodeNG | None]
224
values: list[NodeNG]
225
226
class Slice:
227
"""Slice objects."""
228
229
lower: NodeNG | None
230
upper: NodeNG | None
231
step: NodeNG | None
232
```
233
234
### Statement Nodes
235
236
Nodes representing Python statements.
237
238
```python { .api }
239
class Assign:
240
"""Assignment statements."""
241
242
targets: list[NodeNG]
243
value: NodeNG
244
245
class AnnAssign:
246
"""Annotated assignments."""
247
248
target: NodeNG
249
annotation: NodeNG
250
value: NodeNG | None
251
252
class AugAssign:
253
"""Augmented assignments (+=, -=, etc.)."""
254
255
target: NodeNG
256
op: str
257
value: NodeNG
258
259
class Import:
260
"""Import statements."""
261
262
names: list[tuple[str, str | None]]
263
264
class ImportFrom:
265
"""From-import statements."""
266
267
module: str | None
268
names: list[tuple[str, str | None]]
269
level: int
270
271
class Return:
272
"""Return statements."""
273
274
value: NodeNG | None
275
276
class Yield:
277
"""Yield expressions."""
278
279
value: NodeNG | None
280
281
class Raise:
282
"""Raise statements."""
283
284
exc: NodeNG | None
285
cause: NodeNG | None
286
287
class Assert:
288
"""Assert statements."""
289
290
test: NodeNG
291
msg: NodeNG | None
292
```
293
294
### Control Flow Nodes
295
296
Nodes representing control flow constructs.
297
298
```python { .api }
299
class If:
300
"""If statements."""
301
302
test: NodeNG
303
body: list[NodeNG]
304
orelse: list[NodeNG]
305
306
class For:
307
"""For loops."""
308
309
target: NodeNG
310
iter: NodeNG
311
body: list[NodeNG]
312
orelse: list[NodeNG]
313
314
class While:
315
"""While loops."""
316
317
test: NodeNG
318
body: list[NodeNG]
319
orelse: list[NodeNG]
320
321
class With:
322
"""With statements (context managers)."""
323
324
items: list[NodeNG]
325
body: list[NodeNG]
326
327
class Try:
328
"""Try/except blocks."""
329
330
body: list[NodeNG]
331
handlers: list[ExceptHandler]
332
orelse: list[NodeNG]
333
finalbody: list[NodeNG]
334
335
class ExceptHandler:
336
"""Exception handlers."""
337
338
type: NodeNG | None
339
name: NodeNG | None
340
body: list[NodeNG]
341
```
342
343
### Special Nodes
344
345
Specialized nodes for specific Python constructs.
346
347
```python { .api }
348
class Arguments:
349
"""Function argument specifications."""
350
351
args: list[NodeNG]
352
posonlyargs: list[NodeNG]
353
kwonlyargs: list[NodeNG]
354
defaults: list[NodeNG]
355
kw_defaults: list[NodeNG | None]
356
vararg: NodeNG | None
357
kwarg: NodeNG | None
358
359
class Keyword:
360
"""Keyword arguments."""
361
362
arg: str | None
363
value: NodeNG
364
365
class Decorators:
366
"""Decorator lists."""
367
368
nodes: list[NodeNG]
369
370
class Comprehension:
371
"""Comprehension clauses."""
372
373
target: NodeNG
374
iter: NodeNG
375
ifs: list[NodeNG]
376
is_async: bool
377
378
class Unknown:
379
"""Unknown/uninferable nodes."""
380
381
name: str | None
382
383
class EmptyNode:
384
"""Placeholder nodes."""
385
```
386
387
## Node Collections
388
389
```python { .api }
390
ALL_NODE_CLASSES: tuple[type[NodeNG], ...]
391
"""Tuple of all available node classes."""
392
```
393
394
## Utility Functions
395
396
```python { .api }
397
def are_exclusive(node_a: NodeNG, node_b: NodeNG, exceptions: list[type[Exception]] | None = None) -> bool:
398
"""Check if two nodes are mutually exclusive."""
399
400
def unpack_infer(node: NodeNG, context: InferenceContext | None = None) -> list[NodeNG]:
401
"""Unpack inference results, handling Uninferable."""
402
403
def const_factory(value: Any) -> Const:
404
"""Create appropriate Const node for a value."""
405
```