Skip to content

Python

python

Python language backend.

The Python grammar logic (tree-sitter-python, the _extract_* node mappers, the parse cache) lives in :mod:axm_ast.core.parser and is battle-tested by the existing suite. This backend is a thin adapter over those functions so the multi-language dispatch goes through one uniform interface without moving — and risking — the proven Python extraction code. New languages implement the same interface from scratch in their own module.

PythonBackend

Adapter exposing the existing Python parser through the backend interface.

Implements :class:axm_ast.core.backends.base.LanguageBackend structurally.

Source code in packages/axm-ast/src/axm_ast/core/backends/python.py
Python
class PythonBackend:
    """Adapter exposing the existing Python parser through the backend interface.

    Implements :class:`axm_ast.core.backends.base.LanguageBackend` structurally.
    """

    @property
    def name(self) -> str:
        """Language name."""
        return "python"

    @property
    def suffixes(self) -> tuple[str, ...]:
        """Python source extension."""
        return (".py",)

    def parse_source(self, source: str) -> Tree:
        """Parse Python *source* via the existing tree-sitter parser."""
        return _py_parser.parse_source(source)

    def parse_file(self, path: Path) -> Tree:
        """Parse a Python file via the existing cached parser."""
        return _py_parser.parse_file(path)

    def extract_module(self, path: Path) -> ModuleInfo:
        """Extract module info via the existing Python extractor."""
        return _py_parser.extract_module_info(path)

    def extract_calls(
        self, module: ModuleInfo, module_name: str | None = None
    ) -> list[CallSite]:
        """Extract call-sites via the existing Python caller analysis."""
        from axm_ast.core.callers import extract_calls

        return extract_calls(module, module_name=module_name)
name property

Language name.

suffixes property

Python source extension.

extract_calls(module, module_name=None)

Extract call-sites via the existing Python caller analysis.

Source code in packages/axm-ast/src/axm_ast/core/backends/python.py
Python
def extract_calls(
    self, module: ModuleInfo, module_name: str | None = None
) -> list[CallSite]:
    """Extract call-sites via the existing Python caller analysis."""
    from axm_ast.core.callers import extract_calls

    return extract_calls(module, module_name=module_name)
extract_module(path)

Extract module info via the existing Python extractor.

Source code in packages/axm-ast/src/axm_ast/core/backends/python.py
Python
def extract_module(self, path: Path) -> ModuleInfo:
    """Extract module info via the existing Python extractor."""
    return _py_parser.extract_module_info(path)
parse_file(path)

Parse a Python file via the existing cached parser.

Source code in packages/axm-ast/src/axm_ast/core/backends/python.py
Python
def parse_file(self, path: Path) -> Tree:
    """Parse a Python file via the existing cached parser."""
    return _py_parser.parse_file(path)
parse_source(source)

Parse Python source via the existing tree-sitter parser.

Source code in packages/axm-ast/src/axm_ast/core/backends/python.py
Python
def parse_source(self, source: str) -> Tree:
    """Parse Python *source* via the existing tree-sitter parser."""
    return _py_parser.parse_source(source)