Classes
PythonExtractor
Extract Python API documentation via AST parsing.
Features:
- No imports (AST-only) - fast and rel…
PythonExtractor
Extract Python API documentation via AST parsing.
Features:
- No imports (AST-only) - fast and reliable
- Extracts modules, classes, functions, methods
- Type hint support
- Docstring extraction
- Signature building
- Alias detection
- Inherited member synthesis
Performance:
- ~0.1-0.5s per file
- No dependencies loaded
- No side effects
ExtractorMethods 2
extract
Extract documentation from Python source.
extract
def extract(self, source: Path) -> list[DocElement]
Extract documentation from Python source.
Parameters 1
source |
Path |
Directory or file path |
Returns
List of DocElement objectslist[DocElement]
—
get_output_path
Get output path for element.
Packages (modules from __init__.py) generate _ind…
get_output_path
def get_output_path(self, element: DocElement) -> Path | None
Get output path for element.
Packages (modules from init.py) generate _index.md files to act as section indexes. With grouping enabled, modules are organized under group directories based on package hierarchy or explicit configuration.
Examples (without grouping): bengal (package) → bengal/_index.md bengal.core (package) → bengal/core/_index.md bengal.core.site (module) → bengal/core/site.md
Examples (with grouping, strip_prefix="bengal."): bengal.core (package) → core/_index.md bengal.cli.templates.blog (module) → templates/blog.md
Parameters 1
element |
DocElement |
Returns
Path object for output location, or None if element should be skippedPath | None
—
Internal Methods 21
__init__
Initialize extractor.
__init__
def __init__(self, exclude_patterns: list[str] | None = None, config: dict[str, Any] | None = None)
Initialize extractor.
Parameters 2
exclude_patterns |
list[str] | None |
Glob patterns to exclude (e.g., "/tests/") |
config |
dict[str, Any] | None |
Configuration dict with include_inherited, exclude_patterns, etc. |
_init_grouping
Initialize grouping configuration.
_init_grouping
def _init_grouping(self) -> dict[str, Any]
Initialize grouping configuration.
Returns
Grouping config dict with mode and prefix_mapdict[str, Any]
—
_extract_directory
Extract from all Python files in directory.
_extract_directory
def _extract_directory(self, directory: Path) -> list[DocElement]
Extract from all Python files in directory.
Parameters 1
directory |
Path |
Returns
list[DocElement]
_should_skip
Check if file should be skipped.
Handles common exclusion patterns:
- Hidden d…
_should_skip
def _should_skip(self, path: Path) -> bool
Check if file should be skipped.
Handles common exclusion patterns:
- Hidden directories (starting with .)
- Virtual environments (.venv, venv, env, .env)
- Site-packages (dependencies)
- Build artifacts (pycache, build, dist)
- Test files and directories
Parameters 1
path |
Path |
Returns
bool
_extract_file
Extract documentation from a single Python file.
_extract_file
def _extract_file(self, file_path: Path) -> list[DocElement]
Extract documentation from a single Python file.
Parameters 1
file_path |
Path |
Returns
list[DocElement]
_extract_module
Extract module documentation.
_extract_module
def _extract_module(self, tree: ast.Module, file_path: Path, source: str) -> DocElement | None
Extract module documentation.
Parameters 3
tree |
ast.Module |
|
file_path |
Path |
|
source |
str |
Returns
DocElement | None
_extract_class
Extract class documentation.
_extract_class
def _extract_class(self, node: ast.ClassDef, file_path: Path, parent_name: str = '') -> DocElement | None
Extract class documentation.
Parameters 3
node |
ast.ClassDef |
|
file_path |
Path |
|
parent_name |
str |
Returns
DocElement | None
_extract_function
Extract function/method documentation.
_extract_function
def _extract_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef, file_path: Path, parent_name: str = '') -> DocElement | None
Extract function/method documentation.
Parameters 3
node |
ast.FunctionDef | ast.AsyncFunctionDef |
|
file_path |
Path |
|
parent_name |
str |
Returns
DocElement | None
_build_signature
Build function signature string.
_build_signature
def _build_signature(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> str
Build function signature string.
Parameters 1
node |
ast.FunctionDef | ast.AsyncFunctionDef |
Returns
str
_extract_arguments
Extract argument information.
_extract_arguments
def _extract_arguments(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> list[dict[str, Any]]
Extract argument information.
Parameters 1
node |
ast.FunctionDef | ast.AsyncFunctionDef |
Returns
list[dict[str, Any]]
_to_parsed_docstring
Convert ParsedDoc to frozen ParsedDocstring.
_to_parsed_docstring
def _to_parsed_docstring(self, parsed: Any) -> ParsedDocstring | None
Convert ParsedDoc to frozen ParsedDocstring.
Parameters 1
parsed |
Any |
ParsedDoc from docstring_parser |
Returns
ParsedDocstring dataclass or NoneParsedDocstring | None
—
_to_parameter_info
Convert arg dict to ParameterInfo.
_to_parameter_info
def _to_parameter_info(self, arg: dict[str, Any]) -> ParameterInfo
Convert arg dict to ParameterInfo.
Parameters 1
arg |
dict[str, Any] |
Dict with name, annotation, default, docstring |
Returns
ParameterInfo dataclassParameterInfo
—
_has_yield
Check if function contains yield statement.
_has_yield
def _has_yield(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool
Check if function contains yield statement.
Parameters 1
node |
ast.FunctionDef | ast.AsyncFunctionDef |
Function AST node |
Returns
True if function is a generatorbool
—
_annotation_to_string
Convert AST annotation to string.
_annotation_to_string
def _annotation_to_string(self, annotation: ast.expr | None) -> str | None
Convert AST annotation to string.
Parameters 1
annotation |
ast.expr | None |
Returns
str | None
_expr_to_string
Convert AST expression to string.
_expr_to_string
def _expr_to_string(self, expr: ast.expr) -> str
Convert AST expression to string.
Parameters 1
expr |
ast.expr |
Returns
str
_infer_module_name
Infer module name from file path relative to source root.
_infer_module_name
def _infer_module_name(self, file_path: Path) -> str
Infer module name from file path relative to source root.
Parameters 1
file_path |
Path |
Returns
str
_get_relative_source_path
Get source path relative to source root for GitHub links.
_get_relative_source_path
def _get_relative_source_path(self, file_path: Path) -> Path
Get source path relative to source root for GitHub links.
Parameters 1
file_path |
Path |
Absolute file path |
Returns
Path relative to source root (e.g., "bengal/core/page.py")Path
—
_should_include_inherited
Check if inherited members should be included for element type.
_should_include_inherited
def _should_include_inherited(self, element_type: str = 'class') -> bool
Check if inherited members should be included for element type.
Parameters 1
element_type |
str |
Type of element ("class" or "exception") |
Returns
True if inherited members should be includedbool
—
_synthesize_inherited_members
Add inherited members to a class element.
_synthesize_inherited_members
def _synthesize_inherited_members(self, class_elem: DocElement) -> None
Add inherited members to a class element.
Parameters 1
class_elem |
DocElement |
Class DocElement to augment with inherited members |
_detect_aliases
Detect simple assignment aliases at module level.
Patterns detected:
- alias =…
_detect_aliases
def _detect_aliases(self, tree: ast.Module, module_name: str, defined_names: set[str]) -> dict[str, str]
Detect simple assignment aliases at module level.
Patterns detected:
- alias = original (ast.Name)
- alias = module.original (ast.Attribute)
Parameters 3
tree |
ast.Module |
Module AST |
module_name |
str |
Current module qualified name |
defined_names |
set[str] |
Set of names defined in this module |
Returns
Dict mapping alias_name -> qualified_originaldict[str, str]
—
_extract_all_exports
Extract __all__ exports if present.
_extract_all_exports
def _extract_all_exports(self, tree: ast.Module) -> list[str] | None
Extract all exports if present.
Parameters 1
tree |
ast.Module |
Returns
list[str] | None