palgen.loaders.ast_helper
#
Module Contents#
- class palgen.loaders.ast_helper.AST(tree)#
palgen.loaders.ast_helper.AST palgen.loaders.ast_helper.AST palgen.loaders.ast_helper.AST palgen.loaders.ast_helper.AST pathlib.Path pathlib.Path pathlib.Path->palgen.loaders.ast_helper.AST path pathlib.PurePath pathlib.PurePath pathlib.Path->pathlib.PurePath Module level AST parsing utility. Will visit the AST and extract information automatically.
- Currently unsupported but legal tree types:
ast.Interactive
ast.Expression
ast.FunctionType
- Extracts:
constants
imports
import aliases
classes
- Parameters:
tree (
ast.Module
) – Module to inspect
- Info:
This is only constructible from a ast.Module instance. Use the
AST.parse(...)
orAST.load(...)
methods to load AST from text or files.
- __slots__ = ('path', 'tree', 'constants', 'imports', 'classes')#
- classmethod parse(text)#
Parse AST from string
- classmethod load(path, encoding='utf-8')#
Parse AST from file
- Parameters:
path (
Path | str
) – Path to fileencoding (
str, optional
) – Encoding to use. Defaults to ‘utf-8’.
- Returns:
parsed AST
- Return type:
- visit_module(node)#
Module
Module(stmt* body, type_ignore* type_ignores)
- Parameters:
node (ast.Module) –
- visit_assign(node)#
Assignment, ie
foo = 3
Assign(expr* targets, expr value, string? type_comment)
- Parameters:
node (ast.Assign) –
- visit_assign_annotated(node)#
Assignment with type annotations, ie
foo: int = 3
AnnAssign(expr target, expr annotation, expr? value, int simple)
- Parameters:
node (ast.AnnAssign) –
- visit_import(node)#
Simple imports, ie
import foo
Import(alias* names)
- Parameters:
node (ast.Import) –
- visit_import_from(node)#
Import from module, ie from foo import bar
ImportFrom(identifier? module, alias* names, int? level)
- Parameters:
node (ast.ImportFrom) –
- visit_class(node)#
Class definition
ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)
- Parameters:
node (ast.ClassDef) –
- try_constant(target, value)#
- Parameters:
target (ast.expr) –
value (ast.expr) –
- possible_names(base)#
Yields all possible symbols referring to the target type.
- class palgen.loaders.ast_helper.Import(name=None, module=None, alias=None)#
palgen.loaders.ast_helper.Import palgen.loaders.ast_helper.Import palgen.loaders.ast_helper.Import palgen.loaders.ast_helper.Import - property name#
- property real_name#
- __slots__ = ('_name', 'module', 'alias')#
- full_name()#
- class palgen.loaders.ast_helper.Class(name, bases)#
palgen.loaders.ast_helper.Class palgen.loaders.ast_helper.Class palgen.loaders.ast_helper.Class palgen.loaders.ast_helper.Class - __slots__ = ('name', 'bases')#
- static parse(node)#
Visits class definition and extracts bases.
ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)
- Parameters:
node (
ast.ClassDef
) – The class node- Returns:
This object with properly set bases.
- Return type:
- static visit_attribute(node)#
Visits an attribute
Attribute(expr value, identifier attr, expr_context ctx)`
- Returns:
str
- Parameters:
node (ast.Attribute) –
- Return type:
- palgen.loaders.ast_helper.get_import_name(import_)#
Gets import name from type or module.