Skip to content

Inspect detail

inspect_detail

Detail-building helpers for InspectTool.

build_detail(sym, *, file='', abs_path='', source=False)

Build detail dict from a FunctionInfo, ClassInfo, or VariableInfo.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def build_detail(
    sym: FunctionInfo | ClassInfo | VariableInfo,
    *,
    file: str = "",
    abs_path: str = "",
    source: bool = False,
) -> dict[str, Any]:
    """Build detail dict from a FunctionInfo, ClassInfo, or VariableInfo."""
    if isinstance(sym, VariableInfo):
        return variable_detail(sym, file=file)

    if isinstance(sym, FunctionInfo):
        detail = function_detail(sym, file=file)
    else:
        detail = class_detail(sym, file=file)

    if source and abs_path:
        detail["source"] = read_source(abs_path, sym.line_start, sym.line_end)

    return detail

build_module_detail(pkg, mod, name)

Build detail dict for a module.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def build_module_detail(pkg: PackageInfo, mod: ModuleInfo, name: str) -> dict[str, Any]:
    """Build detail dict for a module."""
    return {
        "name": name,
        "kind": "module",
        "file": relative_path(pkg, mod.path),
        "docstring": mod.docstring or "",
        "functions": [f.name for f in mod.functions],
        "classes": [c.name for c in mod.classes],
        "symbol_count": len(mod.functions) + len(mod.classes),
    }

class_detail(sym, *, file='')

Build detail dict from a ClassInfo.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def class_detail(
    sym: ClassInfo,
    *,
    file: str = "",
) -> dict[str, Any]:
    """Build detail dict from a ClassInfo."""
    detail: dict[str, Any] = {
        "name": sym.name,
        "file": file,
        "start_line": sym.line_start,
        "end_line": sym.line_end,
    }
    if sym.docstring is not None:
        detail["docstring"] = sym.docstring
    if sym.bases:
        detail["bases"] = sym.bases
    if sym.methods:
        detail["methods"] = [m.name for m in sym.methods]
    detail["module"] = ""
    return detail

function_detail(sym, *, file='')

Build detail dict from a FunctionInfo.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def function_detail(
    sym: FunctionInfo,
    *,
    file: str = "",
) -> dict[str, Any]:
    """Build detail dict from a FunctionInfo."""
    detail: dict[str, Any] = {
        "name": sym.name,
        "file": file,
        "start_line": sym.line_start,
        "end_line": sym.line_end,
    }
    if sym.docstring is not None:
        detail["docstring"] = sym.docstring
    detail["signature"] = sym.signature
    if sym.return_type is not None:
        detail["return_type"] = sym.return_type
    if sym.params:
        detail["parameters"] = [
            {"name": p.name, "annotation": p.annotation, "default": p.default}
            for p in sym.params
        ]
    detail["module"] = ""
    return detail

read_source(abs_file_path, start, end)

Read source lines from a file (absolute path).

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def read_source(abs_file_path: str, start: int, end: int) -> str:
    """Read source lines from a file (absolute path)."""
    try:
        lines = Path(abs_file_path).read_text().splitlines()
        return "\n".join(lines[start - 1 : end])
    except (OSError, IndexError):
        return ""

relative_path(pkg, mod_path)

Compute relative path from package root.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def relative_path(pkg: PackageInfo, mod_path: Path) -> str:
    """Compute relative path from package root."""
    try:
        return str(mod_path.relative_to(pkg.root.parent))
    except ValueError:
        return str(mod_path)

variable_detail(sym, *, file='')

Build detail dict from a VariableInfo.

Source code in packages/axm-ast/src/axm_ast/tools/inspect_detail.py
Python
def variable_detail(
    sym: VariableInfo,
    *,
    file: str = "",
) -> dict[str, Any]:
    """Build detail dict from a VariableInfo."""
    detail: dict[str, Any] = {
        "name": sym.name,
        "file": file,
        "kind": "variable",
        "start_line": sym.line,
        "end_line": sym.line,
        "module": "",
    }
    if sym.annotation is not None:
        detail["annotation"] = sym.annotation
    if sym.value_repr is not None:
        detail["value_repr"] = sym.value_repr
    return detail