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(...) or AST.load(...) methods to load AST from text or files.

__slots__ = ('path', 'tree', 'constants', 'imports', 'classes')#
classmethod parse(text)#

Parse AST from string

Parameters:

text (str) – String containing valid Python code.

Returns:

parsed AST

Return type:

AST

classmethod load(path, encoding='utf-8')#

Parse AST from file

Parameters:
  • path (Path | str) – Path to file

  • encoding (str, optional) – Encoding to use. Defaults to ‘utf-8’.

Returns:

parsed AST

Return type:

AST

visit(node)#

Generic node visitor

Parameters:

node (ast.AST) –

Return type:

None

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.

Parameters:

base (type) – The type of the target

Yields:

str – for every possible symbol referring to the target type

Return type:

Iterable[str]

get_subclasses(base)#

Gets all subclasses of given base type.

Parameters:

base (type) – the base type.

Yields:

Class – Every subclass of base.

Return type:

Iterable[Class]

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
Parameters:
  • name (Optional[str]) –

  • module (Optional[list[str]]) –

  • alias (Optional[str]) –

property name#
property real_name#
__slots__ = ('_name', 'module', 'alias')#
full_name()#
__str__()#

Return str(self).

Return type:

str

__repr__()#

Return repr(self).

Return type:

str

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
Parameters:
__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:

Class

static visit(node)#
Parameters:

node (ast.AST) –

Return type:

str

static visit_attribute(node)#

Visits an attribute

Attribute(expr value,
          identifier attr,
          expr_context ctx)`
Returns:

str

Parameters:

node (ast.Attribute) –

Return type:

str

static visit_name(node)#

Visits a variable name

Name(identifier id, expr_context ctx)
Parameters:

node (ast.Name) – A variable name

Returns:

Variable name as a string.

Return type:

str

palgen.loaders.ast_helper.get_import_name(import_)#

Gets import name from type or module.

Parameters:
Raises:

TypeError – Unsupported target type

Returns:

Package and module

Return type:

tuple[list[str], Optional[str]]