astatine¶
Some handy helper functions for Python’s AST module.
Functions:
|
Returns the elements of the dotted attribute name for the given AST node. |
|
Returns a |
|
For the given |
|
Returns the line number of the start of the docstring for |
|
Returns a list of comment lines from |
|
Returns whether the given |
|
Returns a mapping of argument names to the AST nodes representing their values, for the given function call. |
|
Recursively add the |
-
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, orast.Call)- Return type
-
get_constants(module)[source]¶ Returns a
name: valuemapping of constants in the given module.New in version 0.3.1.
-
get_contextmanagers(with_node)[source]¶ For the given
withblock, returns a mapping of the contextmanager names to the individual nodes.New in version 0.3.1.
-
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.
-
get_toplevel_comments(source)[source]¶ Returns a list of comment lines from
sourcewhich occur before the first line of source code (including before module-level docstrings).- Parameters
source (
str)- Return type
-
is_type_checking(node)[source]¶ Returns whether the given
ifblock isif typing.TYPE_CHECKINGor equivalent.
-
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.
-
mark_text_ranges(node, source)[source]¶ Recursively add the
end_linenoandend_col_offsetattributes to each child ofnodewhich already has the attributeslinenoandcol_offset.- Parameters
node (
AST) – An AST node created withast.parse().source (
str) – The corresponding source code for the node.