pub struct PythonDependencyExtractor { /* private fields */ }Expand description
Extracts Python import dependencies using tree-sitter AST walking.
Uses tree-sitter’s Python grammar to parse import statements without executing the Python code. Thread-safe and reusable across files.
§Architecture
The extractor operates in two phases:
- Parse: Tree-sitter parses the source into an AST
- Walk: Recursive traversal matches
import_statementandimport_from_statementnodes, extracting structured data
Module path resolution (converting "os.path" to a filesystem path)
is a separate concern handled by resolve_module_path.
Implementations§
Source§impl PythonDependencyExtractor
impl PythonDependencyExtractor
Sourcepub fn extract_imports(
&self,
source: &str,
_file_path: &Path,
) -> Result<Vec<ImportInfo>, ExtractionError>
pub fn extract_imports( &self, source: &str, _file_path: &Path, ) -> Result<Vec<ImportInfo>, ExtractionError>
Extracts all import statements from Python source code.
Parses the source with tree-sitter and walks the AST to find both
import_statement and import_from_statement nodes. Imports inside
function bodies, try/except blocks, and other nested scopes are
included.
§Arguments
source- Python source code to analyze._file_path- Path of the source file (reserved for future error context).
§Returns
A vector of ImportInfo records. Bare import os, sys statements
produce one ImportInfo per module.
§Errors
Returns ExtractionError::ParseError if tree-sitter cannot parse
the source.
Sourcepub fn resolve_module_path(
&self,
source_file: &Path,
module_path: &str,
relative_level: usize,
) -> Result<PathBuf, ExtractionError>
pub fn resolve_module_path( &self, source_file: &Path, module_path: &str, relative_level: usize, ) -> Result<PathBuf, ExtractionError>
Resolves a Python module path to a filesystem path.
For absolute imports (relative_level == 0), converts dots to path
separators and appends .py. For relative imports, navigates up from
the source file’s directory according to the dot count.
§Arguments
source_file- The file containing the import statement.module_path- The dotted module path (e.g.,"os.path","utils"), with leading dots already stripped (conveyed viarelative_level).relative_level- The relative import depth (0 for absolute).
§Returns
The resolved filesystem path to the target module.
§Errors
Returns ExtractionError::ResolutionError if the source file has no
parent directory, or relative navigation exceeds the filesystem root.
Sourcepub fn extract_dependency_edges(
&self,
source: &str,
file_path: &Path,
) -> Result<Vec<DependencyEdge>, ExtractionError>
pub fn extract_dependency_edges( &self, source: &str, file_path: &Path, ) -> Result<Vec<DependencyEdge>, ExtractionError>
Extract [DependencyEdge] values from a Python source file.
Combines import extraction with path resolution to produce edges suitable for the incremental dependency graph. Only resolvable relative imports produce edges; absolute imports and unresolvable paths are silently skipped.
§Errors
Returns an error if the source file cannot be parsed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PythonDependencyExtractor
impl RefUnwindSafe for PythonDependencyExtractor
impl Send for PythonDependencyExtractor
impl Sync for PythonDependencyExtractor
impl Unpin for PythonDependencyExtractor
impl UnsafeUnpin for PythonDependencyExtractor
impl UnwindSafe for PythonDependencyExtractor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more