astatine

Some handy helper functions for Python’s AST module.

Functions:

get_attribute_name(node)

Returns the elements of the dotted attribute name for the given AST node.

get_constants(module)

Returns a name: value mapping of constants in the given module.

get_contextmanagers(with_node)

For the given with block, returns a mapping of the contextmanager names to the individual nodes.

get_docstring_lineno(node)

Returns the line number of the start of the docstring for node.

get_toplevel_comments(source)

Returns a list of comment lines from source which occur before the first line of source code (including before module-level docstrings).

is_type_checking(node)

Returns whether the given if block is if typing.TYPE_CHECKING or equivalent.

kwargs_from_node(node, posarg_names)

Returns a mapping of argument names to the AST nodes representing their values, for the given function call.

mark_text_ranges(node, source)

Recursively add the end_lineno and end_col_offset attributes to each child of node which already has the attributes lineno and col_offset.

get_attribute_name(node)[source]

Returns the elements of the dotted attribute name for the given AST node.

New in version 0.3.1.

Parameters

node (AST)

Raises

NotImplementedError – if the name contains an unknown node (i.e. not ast.Name, ast.Attribute, or ast.Call)

Return type

Iterable[str]

get_constants(module)[source]

Returns a name: value mapping of constants in the given module.

New in version 0.3.1.

Parameters

module (Module)

Return type

Dict[str, Any]

get_contextmanagers(with_node)[source]

For the given with block, returns a mapping of the contextmanager names to the individual nodes.

New in version 0.3.1.

Parameters

with_node (With)

Return type

Dict[Tuple[str, …], withitem]

get_docstring_lineno(node)[source]

Returns the line number of the start of the docstring for node.

Parameters

node (Union[FunctionDef, ClassDef, Module])

Warning

On CPython 3.6 and 3.7 the line number may not be correct, due to https://bugs.python.org/issue16806.

CPython 3.8 and above are unaffected, as are PyPy 3.6 and 3.7

Accurate line numbers on CPython 3.6 and 3.7 may be obtained by using https://github.com/domdfcoding/typed_ast, which contains the backported fix from Python 3.8.

Return type

Optional[int]

get_toplevel_comments(source)[source]

Returns a list of comment lines from source which occur before the first line of source code (including before module-level docstrings).

Parameters

source (str)

Return type

StringList

is_type_checking(node)[source]

Returns whether the given if block is if typing.TYPE_CHECKING or equivalent.

Parameters

node (AST)

Return type

bool

kwargs_from_node(node, posarg_names)[source]

Returns a mapping of argument names to the AST nodes representing their values, for the given function call.

New in version 0.3.1.

Parameters
  • node (Call)

  • posarg_names (Union[Iterable[str], Callable]) – Either a list of positional argument names for the function, or the function object.

Return type

Dict[str, AST]

mark_text_ranges(node, source)[source]

Recursively add the end_lineno and end_col_offset attributes to each child of node which already has the attributes lineno and col_offset.

Parameters
  • node (AST) – An AST node created with ast.parse().

  • source (str) – The corresponding source code for the node.