API Reference#

class NodeWalk(node: AST)[source]#

A class for exploring definitions in an AST.

Parameters:

node – The root AST node.

classmethod from_source(source: str) NodeWalk[source]#

Create a NodeWalk object from python source code. The code is parsed using the built-in ast module.

classmethod from_file(filename: str) NodeWalk[source]#

Create a NodeWalk object from a python file. The file is read as text and parsed using the built-in ast module.

children() Mapping[str, Sequence[NodeWalk]][source]#

Returns a mapping of the names of the named declared in the node to a sequence of NodeWalk objects for each declaration.

__getitem__(name: str) NodeWalk[source]#

Returns a NodeWalk object for the declaration of the given name.

Parameters:

name – The name of the declared object. Can also be a dotted name.

Raises:
  • KeyError – If the name is not declared in the node.

  • ValueError – If the node declares multiple names with the given name.

get_last(name: str) NodeWalk[source]#

Returns a NodeWalk object for the last declaration of the given name.

Parameters:

name – The name of the declared object. Can also be a dotted name.

Raises:

KeyError – If the name is not declared in the node.

get_many(name: str) Sequence[NodeWalk][source]#

Returns a sequence of NodeWalk objects for each declaration of the given name. If the name is not declared in the node, an empty sequence is returned.

property lineno: int[source]#
property col_offset: int[source]#

The line numer and column offset of the node. Note that this may differ from the column offset of the root node

property end_lineno: int | None[source]#
property end_col_offset: int | None[source]#

The line number and column offset of the end of the node. Note that this may differ from the column offset of the end of the root node