An abstract syntax tree for Python with inference support.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
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.
Foundation classes that all astroid nodes inherit from, providing core functionality for traversal, inference, and analysis.
class NodeNG:
"""Base class for all astroid nodes."""
def infer(self, context: InferenceContext | None = None) -> Iterator[InferenceResult]:
"""
Get possible inferred values for this node.
Parameters:
- context: Inference context for caching and control
Yields:
Possible inferred values (nodes or objects)
Raises:
InferenceError: When inference fails
"""
def as_string(self) -> str:
"""Return string representation of the node."""
def accept(self, visitor) -> Any:
"""Accept a visitor and call appropriate visit method."""
def get_children(self) -> Iterator[NodeNG]:
"""Yield direct child nodes."""
def nodes_of_class(self, klass: type[NodeNG]) -> Iterator[NodeNG]:
"""Yield descendant nodes of specified class."""
def scope(self) -> LocalsDictNodeNG:
"""Return the first parent node defining a local scope."""
def statement(self) -> NodeNG:
"""Return the first parent node that is a statement."""
class LocalsDictNodeNG(NodeNG):
"""Base class for nodes that define local scopes."""
def keys(self) -> Iterator[str]:
"""Yield names defined in this scope."""
def values(self) -> Iterator[list[NodeNG]]:
"""Yield lists of nodes for each name."""
def items(self) -> Iterator[tuple[str, list[NodeNG]]]:
"""Yield (name, nodes) pairs."""
def __getitem__(self, name: str) -> list[NodeNG]:
"""Get nodes for a given name."""
def __contains__(self, name: str) -> bool:
"""Check if name is defined in this scope."""Nodes that define scopes and contain local variable definitions.
class Module(LocalsDictNodeNG):
"""Module/file level scope."""
name: str
doc: str | None
file: str | None
package: bool
def wildcard_import_names(self) -> list[str]:
"""Get names imported by 'from module import *'."""
def public_names(self) -> list[str]:
"""Get public names (not starting with underscore)."""
class ClassDef(LocalsDictNodeNG):
"""Class definition."""
name: str
bases: list[NodeNG]
decorators: Decorators | None
doc: str | None
def mro(self) -> list[ClassDef]:
"""Get method resolution order."""
def ancestors(self, recurs: bool = True, context: InferenceContext | None = None) -> Iterator[ClassDef]:
"""Yield ancestor classes."""
def local_attr(self, name: str, context: InferenceContext | None = None) -> list[NodeNG]:
"""Get local attribute nodes."""
class FunctionDef(LocalsDictNodeNG):
"""Function definition."""
name: str
args: Arguments
returns: NodeNG | None
decorators: Decorators | None
doc: str | None
def argnames(self) -> list[str]:
"""Get argument names."""
def is_method(self) -> bool:
"""Check if this is a method."""
def is_abstract(self) -> bool:
"""Check if this is an abstract method."""
class AsyncFunctionDef(FunctionDef):
"""Async function definition."""
class Lambda(LocalsDictNodeNG):
"""Lambda expression."""
args: Arguments
body: NodeNGNodes representing Python expressions and values.
class Const:
"""Constant values (strings, numbers, None, etc.)."""
value: Any
def itered(self) -> list[NodeNG]:
"""Get iteration elements for iterable constants."""
class Name:
"""Variable/identifier references."""
name: str
ctx: Context # Load, Store, Del
class Attribute:
"""Attribute access (obj.attr)."""
expr: NodeNG
attrname: str
ctx: Context
class Subscript:
"""Subscription (obj[key])."""
value: NodeNG
slice: NodeNG
ctx: Context
class Call:
"""Function/method calls."""
func: NodeNG
args: list[NodeNG]
keywords: list[Keyword]
def infer_call_result(self, context: InferenceContext | None = None) -> Iterator[InferenceResult]:
"""Infer the result of this call."""
class BinOp:
"""Binary operations (+, -, *, etc.)."""
left: NodeNG
op: str
right: NodeNG
class UnaryOp:
"""Unary operations (-, +, ~, not)."""
op: str
operand: NodeNG
class Compare:
"""Comparison operations."""
left: NodeNG
ops: list[tuple[str, NodeNG]]
class BoolOp:
"""Boolean operations (and, or)."""
op: str
values: list[NodeNG]Nodes representing Python containers and data structures.
class List:
"""List literals."""
elts: list[NodeNG]
ctx: Context
class Tuple:
"""Tuple literals."""
elts: list[NodeNG]
ctx: Context
class Set:
"""Set literals."""
elts: list[NodeNG]
class Dict:
"""Dictionary literals."""
keys: list[NodeNG | None]
values: list[NodeNG]
class Slice:
"""Slice objects."""
lower: NodeNG | None
upper: NodeNG | None
step: NodeNG | NoneNodes representing Python statements.
class Assign:
"""Assignment statements."""
targets: list[NodeNG]
value: NodeNG
class AnnAssign:
"""Annotated assignments."""
target: NodeNG
annotation: NodeNG
value: NodeNG | None
class AugAssign:
"""Augmented assignments (+=, -=, etc.)."""
target: NodeNG
op: str
value: NodeNG
class Import:
"""Import statements."""
names: list[tuple[str, str | None]]
class ImportFrom:
"""From-import statements."""
module: str | None
names: list[tuple[str, str | None]]
level: int
class Return:
"""Return statements."""
value: NodeNG | None
class Yield:
"""Yield expressions."""
value: NodeNG | None
class Raise:
"""Raise statements."""
exc: NodeNG | None
cause: NodeNG | None
class Assert:
"""Assert statements."""
test: NodeNG
msg: NodeNG | NoneNodes representing control flow constructs.
class If:
"""If statements."""
test: NodeNG
body: list[NodeNG]
orelse: list[NodeNG]
class For:
"""For loops."""
target: NodeNG
iter: NodeNG
body: list[NodeNG]
orelse: list[NodeNG]
class While:
"""While loops."""
test: NodeNG
body: list[NodeNG]
orelse: list[NodeNG]
class With:
"""With statements (context managers)."""
items: list[NodeNG]
body: list[NodeNG]
class Try:
"""Try/except blocks."""
body: list[NodeNG]
handlers: list[ExceptHandler]
orelse: list[NodeNG]
finalbody: list[NodeNG]
class ExceptHandler:
"""Exception handlers."""
type: NodeNG | None
name: NodeNG | None
body: list[NodeNG]Specialized nodes for specific Python constructs.
class Arguments:
"""Function argument specifications."""
args: list[NodeNG]
posonlyargs: list[NodeNG]
kwonlyargs: list[NodeNG]
defaults: list[NodeNG]
kw_defaults: list[NodeNG | None]
vararg: NodeNG | None
kwarg: NodeNG | None
class Keyword:
"""Keyword arguments."""
arg: str | None
value: NodeNG
class Decorators:
"""Decorator lists."""
nodes: list[NodeNG]
class Comprehension:
"""Comprehension clauses."""
target: NodeNG
iter: NodeNG
ifs: list[NodeNG]
is_async: bool
class Unknown:
"""Unknown/uninferable nodes."""
name: str | None
class EmptyNode:
"""Placeholder nodes."""ALL_NODE_CLASSES: tuple[type[NodeNG], ...]
"""Tuple of all available node classes."""def are_exclusive(node_a: NodeNG, node_b: NodeNG, exceptions: list[type[Exception]] | None = None) -> bool:
"""Check if two nodes are mutually exclusive."""
def unpack_infer(node: NodeNG, context: InferenceContext | None = None) -> list[NodeNG]:
"""Unpack inference results, handling Uninferable."""
def const_factory(value: Any) -> Const:
"""Create appropriate Const node for a value."""Install with Tessl CLI
npx tessl i tessl/pypi-astroid