Skip to main content

reflex/parsers/
python.rs

1//! Python language parser using Tree-sitter
2//!
3//! Extracts symbols from Python source code:
4//! - Functions (def, async def)
5//! - Classes (regular, abstract)
6//! - Methods (regular, async, static, class methods, properties via @property)
7//! - Decorators (tracked in scope)
8//! - Lambda expressions assigned to variables
9//! - Local variables (inside functions)
10//! - Global variables (module-level non-uppercase variables)
11//! - Constants (module-level uppercase variables)
12//! - Imports/Exports
13
14use crate::models::{Language, SearchResult, Span, SymbolKind};
15use anyhow::{Context, Result};
16use streaming_iterator::StreamingIterator;
17use tree_sitter::{Parser, QueryCursor};
18
19/// Parse Python source code and extract symbols
20pub fn parse(path: &str, source: &str) -> Result<Vec<SearchResult>> {
21    let mut parser = Parser::new();
22    let language = tree_sitter_python::LANGUAGE;
23
24    parser
25        .set_language(&language.into())
26        .context("Failed to set Python language")?;
27
28    let tree = parser
29        .parse(source, None)
30        .context("Failed to parse Python source")?;
31
32    let root_node = tree.root_node();
33    let table = SYMBOL_QUERIES.run(&language.into(), &root_node, source)?;
34
35    let mut symbols = Vec::new();
36
37    // Extract different types of symbols using Tree-sitter queries
38    symbols.extend(extract_functions(source, &table)?);
39    symbols.extend(extract_classes(source, &table)?);
40    symbols.extend(extract_methods(source, &table)?);
41    symbols.extend(extract_constants(source, &table)?);
42    symbols.extend(extract_global_variables(source, &table)?);
43    symbols.extend(extract_local_variables(source, &table)?);
44    symbols.extend(extract_lambdas(source, &table)?);
45
46    // Add file path to all symbols
47    for symbol in &mut symbols {
48        symbol.path = path.to_string();
49        symbol.lang = Language::Python;
50    }
51
52    Ok(symbols)
53}
54
55/// Extract function definitions (including async functions)
56fn extract_functions(
57    source: &str,
58    table: &crate::parsers::MatchTable<'_>,
59) -> Result<Vec<SearchResult>> {
60    extract_symbols(source, table, 0, SymbolKind::Function, None)
61}
62
63/// Extract class definitions
64fn extract_classes(
65    source: &str,
66    table: &crate::parsers::MatchTable<'_>,
67) -> Result<Vec<SearchResult>> {
68    extract_symbols(source, table, 1, SymbolKind::Class, None)
69}
70
71/// Extract method definitions from classes
72fn extract_methods(
73    source: &str,
74    table: &crate::parsers::MatchTable<'_>,
75) -> Result<Vec<SearchResult>> {
76    let query = table.query();
77    let matches = table.sub(2);
78
79    let mut symbols = Vec::new();
80
81    for match_ in matches {
82        let mut class_name = None;
83        let mut method_name = None;
84        let mut method_node = None;
85
86        for capture in &match_.captures {
87            let capture_name: &str = query.capture_names()[capture.index as usize];
88            match capture_name {
89                "class_name" => {
90                    class_name = Some(
91                        capture
92                            .node
93                            .utf8_text(source.as_bytes())
94                            .unwrap_or("")
95                            .to_string(),
96                    );
97                }
98                "method_name" => {
99                    method_name = Some(
100                        capture
101                            .node
102                            .utf8_text(source.as_bytes())
103                            .unwrap_or("")
104                            .to_string(),
105                    );
106                    // Find the parent function_definition node
107                    let mut current = capture.node;
108                    while let Some(parent) = current.parent() {
109                        if parent.kind() == "function_definition" {
110                            method_node = Some(parent);
111                            break;
112                        }
113                        current = parent;
114                    }
115                }
116                _ => {}
117            }
118        }
119
120        if let (Some(class_name), Some(method_name), Some(node)) =
121            (class_name, method_name, method_node)
122        {
123            let scope = format!("class {}", class_name);
124            let span = node_to_span(&node);
125            let preview = extract_preview(source, &node);
126
127            symbols.push(SearchResult::new(
128                String::new(),
129                Language::Python,
130                SymbolKind::Method,
131                Some(method_name),
132                span,
133                Some(scope),
134                preview,
135            ));
136        }
137    }
138
139    Ok(symbols)
140}
141
142/// Extract module-level constants (uppercase variable assignments)
143fn extract_constants(
144    source: &str,
145    table: &crate::parsers::MatchTable<'_>,
146) -> Result<Vec<SearchResult>> {
147    let query = table.query();
148    let matches = table.sub(3);
149
150    let mut symbols = Vec::new();
151
152    for match_ in matches {
153        let mut name = None;
154        let mut const_node = None;
155
156        for capture in &match_.captures {
157            let capture_name: &str = query.capture_names()[capture.index as usize];
158            if capture_name == "name" {
159                let name_text = capture.node.utf8_text(source.as_bytes()).unwrap_or("");
160                // Only include if it's all uppercase (Python constant convention)
161                if name_text
162                    .chars()
163                    .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
164                {
165                    name = Some(name_text.to_string());
166                    // Get the assignment node
167                    let mut current = capture.node;
168                    while let Some(parent) = current.parent() {
169                        if parent.kind() == "assignment" {
170                            const_node = Some(parent);
171                            break;
172                        }
173                        current = parent;
174                    }
175                }
176            }
177        }
178
179        if let (Some(name), Some(node)) = (name, const_node) {
180            let span = node_to_span(&node);
181            let preview = extract_preview(source, &node);
182
183            symbols.push(SearchResult::new(
184                String::new(),
185                Language::Python,
186                SymbolKind::Constant,
187                Some(name),
188                span,
189                None,
190                preview,
191            ));
192        }
193    }
194
195    Ok(symbols)
196}
197
198/// Extract module-level global variables (non-uppercase variable assignments)
199fn extract_global_variables(
200    source: &str,
201    table: &crate::parsers::MatchTable<'_>,
202) -> Result<Vec<SearchResult>> {
203    let query = table.query();
204    let matches = table.sub(4);
205
206    let mut symbols = Vec::new();
207
208    for match_ in matches {
209        let mut name = None;
210        let mut var_node = None;
211
212        for capture in &match_.captures {
213            let capture_name: &str = query.capture_names()[capture.index as usize];
214            if capture_name == "name" {
215                let name_text = capture.node.utf8_text(source.as_bytes()).unwrap_or("");
216                // Only include if it's NOT all uppercase (constants are handled separately)
217                if !name_text
218                    .chars()
219                    .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
220                {
221                    name = Some(name_text.to_string());
222                    // Get the assignment node
223                    let mut current = capture.node;
224                    while let Some(parent) = current.parent() {
225                        if parent.kind() == "assignment" {
226                            var_node = Some(parent);
227                            break;
228                        }
229                        current = parent;
230                    }
231                }
232            }
233        }
234
235        if let (Some(name), Some(node)) = (name, var_node) {
236            let span = node_to_span(&node);
237            let preview = extract_preview(source, &node);
238
239            symbols.push(SearchResult::new(
240                String::new(),
241                Language::Python,
242                SymbolKind::Variable,
243                Some(name),
244                span,
245                None,
246                preview,
247            ));
248        }
249    }
250
251    Ok(symbols)
252}
253
254/// Extract local variable assignments inside functions
255fn extract_local_variables(
256    source: &str,
257    table: &crate::parsers::MatchTable<'_>,
258) -> Result<Vec<SearchResult>> {
259    let query = table.query();
260    let matches = table.sub(5);
261
262    let mut symbols = Vec::new();
263
264    for match_ in matches {
265        let mut name = None;
266        let mut assignment_node = None;
267
268        for capture in &match_.captures {
269            let capture_name: &str = query.capture_names()[capture.index as usize];
270            match capture_name {
271                "name" => {
272                    let name_text = capture.node.utf8_text(source.as_bytes()).unwrap_or("");
273                    // Skip uppercase constants (handled by extract_constants)
274                    if !name_text
275                        .chars()
276                        .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
277                    {
278                        name = Some(name_text.to_string());
279                    }
280                }
281                "assignment" => {
282                    assignment_node = Some(capture.node);
283                }
284                _ => {}
285            }
286        }
287
288        // Check if this assignment is inside a function definition
289        if let (Some(name), Some(node)) = (name, assignment_node) {
290            let mut is_in_function = false;
291            let mut current = node;
292
293            while let Some(parent) = current.parent() {
294                if parent.kind() == "function_definition" {
295                    is_in_function = true;
296                    break;
297                }
298                // Stop if we hit module level
299                if parent.kind() == "module" {
300                    break;
301                }
302                current = parent;
303            }
304
305            if is_in_function {
306                let span = node_to_span(&node);
307                let preview = extract_preview(source, &node);
308
309                symbols.push(SearchResult::new(
310                    String::new(),
311                    Language::Python,
312                    SymbolKind::Variable,
313                    Some(name),
314                    span,
315                    None, // No scope for local variables
316                    preview,
317                ));
318            }
319        }
320    }
321
322    Ok(symbols)
323}
324
325/// Extract lambda expressions assigned to variables
326fn extract_lambdas(
327    source: &str,
328    table: &crate::parsers::MatchTable<'_>,
329) -> Result<Vec<SearchResult>> {
330    extract_symbols(source, table, 6, SymbolKind::Function, None)
331}
332
333/// Generic symbol extraction helper
334fn extract_symbols(
335    source: &str,
336    table: &crate::parsers::MatchTable<'_>,
337    sub: usize,
338    kind: SymbolKind,
339    scope: Option<String>,
340) -> Result<Vec<SearchResult>> {
341    let query = table.query();
342    let matches = table.sub(sub);
343
344    let mut symbols = Vec::new();
345
346    for match_ in matches {
347        // Find the name capture and the full node
348        let mut name = None;
349        let mut full_node = None;
350
351        for capture in &match_.captures {
352            let capture_name: &str = query.capture_names()[capture.index as usize];
353            if capture_name == "name" {
354                name = Some(
355                    capture
356                        .node
357                        .utf8_text(source.as_bytes())
358                        .unwrap_or("")
359                        .to_string(),
360                );
361            } else {
362                // Assume any other capture is the full node
363                full_node = Some(capture.node);
364            }
365        }
366
367        if let (Some(name), Some(node)) = (name, full_node) {
368            let span = node_to_span(&node);
369            let preview = extract_preview(source, &node);
370
371            symbols.push(SearchResult::new(
372                String::new(),
373                Language::Python,
374                kind.clone(),
375                Some(name),
376                span,
377                scope.clone(),
378                preview,
379            ));
380        }
381    }
382
383    Ok(symbols)
384}
385
386/// Convert a Tree-sitter node to a Span
387fn node_to_span(node: &tree_sitter::Node) -> Span {
388    let start = node.start_position();
389    let end = node.end_position();
390
391    Span::new(
392        start.row + 1, // Convert 0-indexed to 1-indexed
393        start.column,
394        end.row + 1,
395        end.column,
396    )
397}
398
399/// Extract a preview (7 lines) around the symbol
400fn extract_preview(source: &str, node: &tree_sitter::Node) -> String {
401    // Starts at the node, not at byte 0: see `crate::parsers::preview`.
402    crate::parsers::preview::extract_preview_for_node(source, node)
403}
404
405// ============================================================================
406// Dependency Extraction
407// ============================================================================
408
409use crate::models::ImportType;
410use crate::parsers::{DependencyExtractor, ImportInfo};
411
412const SYMQ_0: &str = r#"
413        (function_definition
414            name: (identifier) @name) @function
415    "#;
416const SYMQ_1: &str = r#"
417        (class_definition
418            name: (identifier) @name) @class
419    "#;
420const SYMQ_2: &str = r#"
421        (class_definition
422            name: (identifier) @class_name
423            body: (block
424                (function_definition
425                    name: (identifier) @method_name))) @class
426
427        (class_definition
428            name: (identifier) @class_name
429            body: (block
430                (decorated_definition
431                    (function_definition
432                        name: (identifier) @method_name)))) @class
433    "#;
434const SYMQ_3: &str = r#"
435        (module
436            (expression_statement
437                (assignment
438                    left: (identifier) @name))) @const
439    "#;
440const SYMQ_4: &str = r#"
441        (module
442            (expression_statement
443                (assignment
444                    left: (identifier) @name))) @var
445    "#;
446const SYMQ_5: &str = r#"
447        (assignment
448            left: (identifier) @name) @assignment
449    "#;
450const SYMQ_6: &str = r#"
451        (assignment
452            left: (identifier) @name
453            right: (lambda)) @lambda
454    "#;
455
456/// Every symbol query of this module, run as ONE query per file (see
457/// `crate::parsers::LanguageQueries`).
458static SYMBOL_QUERIES: crate::parsers::LanguageQueries =
459    crate::parsers::LanguageQueries::new(&[SYMQ_0, SYMQ_1, SYMQ_2, SYMQ_3, SYMQ_4, SYMQ_5, SYMQ_6]);
460
461/// Python dependency extractor
462pub struct PythonDependencyExtractor;
463
464impl DependencyExtractor for PythonDependencyExtractor {
465    fn extract_dependencies(source: &str) -> Result<Vec<ImportInfo>> {
466        let mut parser = Parser::new();
467        let language = tree_sitter_python::LANGUAGE;
468
469        parser
470            .set_language(&language.into())
471            .context("Failed to set Python language")?;
472
473        let tree = parser
474            .parse(source, None)
475            .context("Failed to parse Python source")?;
476
477        let root_node = tree.root_node();
478
479        let mut imports = Vec::new();
480
481        // Extract import statements (import os, sys)
482        imports.extend(extract_import_statements(source, &root_node)?);
483
484        // Extract from-import statements (from os import path)
485        imports.extend(extract_from_imports(source, &root_node)?);
486
487        Ok(imports)
488    }
489}
490
491/// Extract regular import statements: import os, import sys
492fn extract_import_statements(source: &str, root: &tree_sitter::Node) -> Result<Vec<ImportInfo>> {
493    let language = tree_sitter_python::LANGUAGE;
494
495    let query_str = r#"
496        (import_statement
497            name: (dotted_name) @import_path) @import
498    "#;
499
500    static QUERY_1: crate::parsers::CachedQuery = crate::parsers::CachedQuery::new();
501    let query = crate::parsers::cached_query(&QUERY_1, language, query_str)
502        .context("Failed to create import statement query")?;
503
504    let mut cursor = QueryCursor::new();
505    let mut matches = cursor.matches(query, *root, source.as_bytes());
506
507    let mut imports = Vec::new();
508
509    while let Some(match_) = matches.next() {
510        let mut import_path = None;
511        let mut import_node = None;
512
513        for capture in match_.captures {
514            let capture_name: &str = query.capture_names()[capture.index as usize];
515            match capture_name {
516                "import_path" => {
517                    import_path = Some(
518                        capture
519                            .node
520                            .utf8_text(source.as_bytes())
521                            .unwrap_or("")
522                            .to_string(),
523                    );
524                }
525                "import" => {
526                    import_node = Some(capture.node);
527                }
528                _ => {}
529            }
530        }
531
532        if let (Some(path), Some(node)) = (import_path, import_node) {
533            let import_type = classify_python_import(&path);
534            let line_number = node.start_position().row + 1;
535
536            imports.push(ImportInfo {
537                imported_path: path,
538                import_type,
539                line_number,
540                imported_symbols: None,
541            });
542        }
543    }
544
545    Ok(imports)
546}
547
548/// Extract from-import statements: from os import path, from . import module
549fn extract_from_imports(source: &str, root: &tree_sitter::Node) -> Result<Vec<ImportInfo>> {
550    let language = tree_sitter_python::LANGUAGE;
551
552    let query_str = r#"
553        (import_from_statement
554            module_name: (dotted_name) @module_path) @import
555
556        (import_from_statement
557            module_name: (relative_import) @module_path) @import
558    "#;
559
560    static QUERY_2: crate::parsers::CachedQuery = crate::parsers::CachedQuery::new();
561    let query = crate::parsers::cached_query(&QUERY_2, language, query_str)
562        .context("Failed to create from-import query")?;
563
564    let mut cursor = QueryCursor::new();
565    let mut matches = cursor.matches(query, *root, source.as_bytes());
566
567    let mut imports = Vec::new();
568
569    while let Some(match_) = matches.next() {
570        let mut module_path = None;
571        let mut import_node = None;
572
573        for capture in match_.captures {
574            let capture_name: &str = query.capture_names()[capture.index as usize];
575            match capture_name {
576                "module_path" => {
577                    module_path = Some(
578                        capture
579                            .node
580                            .utf8_text(source.as_bytes())
581                            .unwrap_or("")
582                            .to_string(),
583                    );
584                }
585                "import" => {
586                    import_node = Some(capture.node);
587                }
588                _ => {}
589            }
590        }
591
592        if let (Some(path), Some(node)) = (module_path, import_node) {
593            let import_type = classify_python_import(&path);
594            let line_number = node.start_position().row + 1;
595
596            // Extract imported symbols if present
597            let imported_symbols = extract_imported_symbols(source, &node);
598
599            imports.push(ImportInfo {
600                imported_path: path,
601                import_type,
602                line_number,
603                imported_symbols,
604            });
605        }
606    }
607
608    Ok(imports)
609}
610
611/// Extract the list of imported symbols from a from-import statement
612fn extract_imported_symbols(source: &str, import_node: &tree_sitter::Node) -> Option<Vec<String>> {
613    let mut symbols = Vec::new();
614
615    // Walk children to find aliased_import or dotted_name nodes (not the
616    // `module_name`, which is a dotted_name too: `django` of `from django.x import y`)
617    let module = import_node
618        .child_by_field_name("module_name")
619        .map(|m| m.id());
620    let mut cursor = import_node.walk();
621    for child in import_node.children(&mut cursor) {
622        if Some(child.id()) == module {
623            continue;
624        }
625        match child.kind() {
626            "aliased_import" | "dotted_name" => {
627                // Get the first identifier
628                let mut child_cursor = child.walk();
629                for grandchild in child.children(&mut child_cursor) {
630                    if (grandchild.kind() == "identifier" || grandchild.kind() == "dotted_name")
631                        && let Ok(text) = grandchild.utf8_text(source.as_bytes())
632                    {
633                        symbols.push(text.to_string());
634                        break; // Only get the first one for aliased imports
635                    }
636                }
637            }
638            _ => {}
639        }
640    }
641
642    if symbols.is_empty() {
643        None
644    } else {
645        Some(symbols)
646    }
647}
648
649/// Find the Python package name from pyproject.toml, setup.py, or setup.cfg
650/// This is used to determine which imports are internal vs external
651pub fn find_python_package_name(root: &std::path::Path) -> Option<String> {
652    // Try pyproject.toml first (modern standard)
653    if let Some(name) = find_pyproject_package(root) {
654        return Some(name);
655    }
656
657    // Try setup.py second
658    if let Some(name) = find_setup_py_package(root) {
659        return Some(name);
660    }
661
662    // Try setup.cfg third
663    if let Some(name) = find_setup_cfg_package(root) {
664        return Some(name);
665    }
666
667    None
668}
669
670/// Parse pyproject.toml to extract package name
671fn find_pyproject_package(root: &std::path::Path) -> Option<String> {
672    let pyproject_path = root.join("pyproject.toml");
673    let content = std::fs::read_to_string(pyproject_path).ok()?;
674
675    // Look for [project] section and name field
676    // Example: name = "Django"
677    let mut in_project_section = false;
678
679    for line in content.lines() {
680        let trimmed = line.trim();
681
682        // Detect [project] section
683        if trimmed == "[project]" {
684            in_project_section = true;
685            continue;
686        }
687
688        // Stop if we hit another section
689        if trimmed.starts_with('[') && trimmed != "[project]" {
690            in_project_section = false;
691            continue;
692        }
693
694        // Parse name field if we're in [project] section
695        if in_project_section
696            && trimmed.starts_with("name")
697            && trimmed.contains('=')
698            && let Some(equals_pos) = trimmed.find('=')
699        {
700            let after_equals = trimmed[equals_pos + 1..].trim();
701
702            // Handle both "name" and 'name'
703            for quote in ['"', '\''] {
704                if let Some(start) = after_equals.find(quote)
705                    && let Some(end) = after_equals[start + 1..].find(quote)
706                {
707                    let name = &after_equals[start + 1..start + 1 + end];
708                    // Convert to lowercase for matching (Django → django)
709                    return Some(name.to_lowercase());
710                }
711            }
712        }
713    }
714
715    None
716}
717
718/// Parse setup.py to extract package name
719fn find_setup_py_package(root: &std::path::Path) -> Option<String> {
720    let setup_path = root.join("setup.py");
721    let content = std::fs::read_to_string(setup_path).ok()?;
722
723    // Look for: setup(name="package_name", ...) or setup(name='package_name', ...)
724    // Simple regex-like parsing
725    for line in content.lines() {
726        let trimmed = line.trim();
727
728        if trimmed.contains("name") && trimmed.contains('=') {
729            // Extract quoted value after name=
730            if let Some(name_pos) = trimmed.find("name") {
731                let after_name = &trimmed[name_pos + 4..]; // Skip "name"
732
733                if let Some(equals_pos) = after_name.find('=') {
734                    let after_equals = after_name[equals_pos + 1..].trim();
735
736                    // Handle both "name" and 'name'
737                    for quote in ['"', '\''] {
738                        if let Some(start) = after_equals.find(quote)
739                            && let Some(end) = after_equals[start + 1..].find(quote)
740                        {
741                            let name = &after_equals[start + 1..start + 1 + end];
742                            return Some(name.to_lowercase());
743                        }
744                    }
745                }
746            }
747        }
748    }
749
750    None
751}
752
753/// Parse setup.cfg to extract package name
754fn find_setup_cfg_package(root: &std::path::Path) -> Option<String> {
755    let setup_cfg_path = root.join("setup.cfg");
756    let content = std::fs::read_to_string(setup_cfg_path).ok()?;
757
758    // Look for [metadata] section and name field
759    let mut in_metadata_section = false;
760
761    for line in content.lines() {
762        let trimmed = line.trim();
763
764        // Detect [metadata] section
765        if trimmed == "[metadata]" {
766            in_metadata_section = true;
767            continue;
768        }
769
770        // Stop if we hit another section
771        if trimmed.starts_with('[') && trimmed != "[metadata]" {
772            in_metadata_section = false;
773            continue;
774        }
775
776        // Parse name field if we're in [metadata] section
777        if in_metadata_section
778            && trimmed.starts_with("name")
779            && trimmed.contains('=')
780            && let Some(equals_pos) = trimmed.find('=')
781        {
782            let name = trimmed[equals_pos + 1..].trim();
783            return Some(name.to_lowercase());
784        }
785    }
786
787    None
788}
789
790/// Reclassify a Python import using the project's package name
791/// Similar to reclassify_go_import() and reclassify_java_import()
792pub fn reclassify_python_import(import_path: &str, package_prefix: Option<&str>) -> ImportType {
793    // First check if this is an internal import (matches project package)
794    if let Some(prefix) = package_prefix {
795        // Extract first component: "django.conf.settings" → "django"
796        let first_component = import_path.split('.').next().unwrap_or(import_path);
797
798        if first_component == prefix {
799            return ImportType::Internal;
800        }
801    }
802
803    // Then check if it's relative (always internal)
804    if import_path.starts_with('.') {
805        return ImportType::Internal;
806    }
807
808    // Check stdlib
809    if is_python_stdlib(import_path) {
810        return ImportType::Stdlib;
811    }
812
813    // Default to external
814    ImportType::External
815}
816
817/// Check if a Python import path is from the standard library
818fn is_python_stdlib(path: &str) -> bool {
819    const STDLIB_MODULES: &[&str] = &[
820        "os",
821        "sys",
822        "io",
823        "re",
824        "json",
825        "csv",
826        "xml",
827        "html",
828        "http",
829        "urllib",
830        "collections",
831        "itertools",
832        "functools",
833        "operator",
834        "pathlib",
835        "glob",
836        "tempfile",
837        "shutil",
838        "pickle",
839        "shelve",
840        "sqlite3",
841        "zlib",
842        "gzip",
843        "time",
844        "datetime",
845        "calendar",
846        "logging",
847        "argparse",
848        "configparser",
849        "typing",
850        "dataclasses",
851        "enum",
852        "abc",
853        "contextlib",
854        "weakref",
855        "threading",
856        "multiprocessing",
857        "subprocess",
858        "queue",
859        "asyncio",
860        "socket",
861        "email",
862        "base64",
863        "hashlib",
864        "hmac",
865        "secrets",
866        "uuid",
867        "math",
868        "random",
869        "statistics",
870        "decimal",
871        "fractions",
872        "unittest",
873        "doctest",
874        "pdb",
875        "trace",
876        "timeit",
877    ];
878
879    // Extract first component of the path
880    let first_component = path.split('.').next().unwrap_or("");
881
882    STDLIB_MODULES.contains(&first_component)
883}
884
885/// Classify a Python import as internal, external, or stdlib
886fn classify_python_import(import_path: &str) -> ImportType {
887    // Relative imports (. or ..)
888    if import_path.starts_with('.') {
889        return ImportType::Internal;
890    }
891
892    // Python standard library (common modules)
893    const STDLIB_MODULES: &[&str] = &[
894        "os",
895        "sys",
896        "io",
897        "re",
898        "json",
899        "csv",
900        "xml",
901        "html",
902        "http",
903        "urllib",
904        "collections",
905        "itertools",
906        "functools",
907        "operator",
908        "pathlib",
909        "glob",
910        "tempfile",
911        "shutil",
912        "pickle",
913        "shelve",
914        "sqlite3",
915        "zlib",
916        "gzip",
917        "time",
918        "datetime",
919        "calendar",
920        "logging",
921        "argparse",
922        "configparser",
923        "typing",
924        "dataclasses",
925        "enum",
926        "abc",
927        "contextlib",
928        "weakref",
929        "threading",
930        "multiprocessing",
931        "subprocess",
932        "queue",
933        "asyncio",
934        "socket",
935        "email",
936        "base64",
937        "hashlib",
938        "hmac",
939        "secrets",
940        "uuid",
941        "math",
942        "random",
943        "statistics",
944        "decimal",
945        "fractions",
946        "unittest",
947        "doctest",
948        "pdb",
949        "trace",
950        "timeit",
951    ];
952
953    // Extract first component of the path
954    let first_component = import_path.split('.').next().unwrap_or("");
955
956    if STDLIB_MODULES.contains(&first_component) {
957        ImportType::Stdlib
958    } else {
959        // Everything else is external (third-party packages)
960        ImportType::External
961    }
962}
963
964// ============================================================================
965// Monorepo Support & Path Resolution
966// ============================================================================
967
968/// Represents a Python package configuration with its location
969#[derive(Debug, Clone)]
970pub struct PythonPackage {
971    /// Package name (e.g., "django", "myapp")
972    pub name: String,
973    /// Project root relative to index root (e.g., "packages/backend")
974    pub project_root: String,
975    /// Absolute path to project root
976    pub abs_project_root: std::path::PathBuf,
977}
978
979/// Recursively find all Python configuration files (pyproject.toml, setup.py, setup.cfg)
980/// in the repository, respecting .gitignore
981pub fn find_all_python_configs(index_root: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
982    use ignore::WalkBuilder;
983
984    let mut config_files = Vec::new();
985
986    let walker = WalkBuilder::new(index_root)
987        .follow_links(false)
988        .git_ignore(true)
989        .build();
990
991    for entry in walker {
992        let entry = entry?;
993        let path = entry.path();
994
995        if !path.is_file() {
996            continue;
997        }
998
999        let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
1000
1001        // Look for Python config files
1002        if filename == "pyproject.toml" || filename == "setup.py" || filename == "setup.cfg" {
1003            // Skip virtual environments and build directories. Normalize
1004            // separators so these filters fire on Windows (`\venv\`) too.
1005            let path_str = path.to_string_lossy().replace('\\', "/");
1006            if path_str.contains("/venv/")
1007                || path_str.contains("/.venv/")
1008                || path_str.contains("/site-packages/")
1009                || path_str.contains("/dist/")
1010                || path_str.contains("/build/")
1011                || path_str.contains("/__pycache__/")
1012            {
1013                log::trace!(
1014                    "Skipping Python config in vendor/build directory: {:?}",
1015                    path
1016                );
1017                continue;
1018            }
1019
1020            config_files.push(path.to_path_buf());
1021        }
1022    }
1023
1024    log::debug!("Found {} Python config files", config_files.len());
1025    Ok(config_files)
1026}
1027
1028/// Parse all Python packages in a monorepo and track their project roots
1029pub fn parse_all_python_packages(index_root: &std::path::Path) -> Result<Vec<PythonPackage>> {
1030    let config_files = find_all_python_configs(index_root)?;
1031    parse_python_packages_from(index_root, &config_files)
1032}
1033
1034/// [`parse_all_python_packages`] for config files already found (in walk order).
1035pub fn parse_python_packages_from(
1036    index_root: &std::path::Path,
1037    config_files: &[std::path::PathBuf],
1038) -> Result<Vec<PythonPackage>> {
1039    if config_files.is_empty() {
1040        log::debug!("No Python config files found in {:?}", index_root);
1041        return Ok(Vec::new());
1042    }
1043
1044    let mut packages = Vec::new();
1045    let config_count = config_files.len();
1046
1047    for config_path in config_files {
1048        let project_root = config_path
1049            .parent()
1050            .ok_or_else(|| anyhow::anyhow!("Config file has no parent directory"))?;
1051
1052        // Try to extract package name from this config
1053        if let Some(package_name) = find_python_package_name(project_root) {
1054            // Normalize to forward slashes so `starts_with("services/")`
1055            // style assertions and downstream import resolution behave the
1056            // same on every OS.
1057            let relative_project_root = project_root
1058                .strip_prefix(index_root)
1059                .unwrap_or(project_root)
1060                .to_string_lossy()
1061                .replace('\\', "/");
1062
1063            log::debug!(
1064                "Found Python package '{}' at {:?}",
1065                package_name,
1066                relative_project_root
1067            );
1068
1069            packages.push(PythonPackage {
1070                name: package_name,
1071                project_root: relative_project_root,
1072                abs_project_root: project_root.to_path_buf(),
1073            });
1074        }
1075    }
1076
1077    log::info!(
1078        "Loaded {} Python packages from {} config files",
1079        packages.len(),
1080        config_count
1081    );
1082
1083    Ok(packages)
1084}
1085
1086/// The paths a Python import can name, most specific first: `a/b.py`, then
1087/// `a/b/__init__.py` (a package). Absolute imports need a package of the
1088/// workspace whose name is the first component; relative ones (`..x`) are taken
1089/// from `current_file_path`. No candidate starts with `/`: a package at the index
1090/// root has an empty project root.
1091pub fn python_import_candidates(
1092    import_path: &str,
1093    packages: &[PythonPackage],
1094    current_file_path: Option<&str>,
1095) -> Vec<String> {
1096    let (base, module) = if import_path.starts_with('.') {
1097        let Some(dir) = relative_python_base(import_path, current_file_path) else {
1098            return Vec::new();
1099        };
1100        (dir, import_path.trim_start_matches('.'))
1101    } else {
1102        // "django.conf.settings" → package "django"
1103        let first_component = import_path.split('.').next().unwrap_or(import_path);
1104        let Some(package) = packages.iter().find(|p| p.name == first_component) else {
1105            return Vec::new();
1106        };
1107        (package.project_root.clone(), import_path)
1108    };
1109    let join = |rel: String| {
1110        if base.is_empty() {
1111            rel
1112        } else {
1113            format!("{base}/{rel}")
1114        }
1115    };
1116    if module.is_empty() {
1117        // `from . import x`: the current package
1118        return vec![join("__init__.py".to_string())];
1119    }
1120    let module_path = module.replace('.', "/");
1121    vec![
1122        join(format!("{module_path}.py")),
1123        join(format!("{module_path}/__init__.py")),
1124    ]
1125}
1126
1127/// The directory a relative import starts from: the importing file's directory,
1128/// one level up per dot after the first.
1129fn relative_python_base(import_path: &str, current_file_path: Option<&str>) -> Option<String> {
1130    let dots = import_path.chars().take_while(|&c| c == '.').count();
1131    let mut dir = std::path::Path::new(current_file_path?).parent()?;
1132    for _ in 1..dots {
1133        dir = dir.parent()?;
1134    }
1135    Some(dir.to_string_lossy().replace('\\', "/"))
1136}
1137
1138#[cfg(test)]
1139mod tests {
1140    use super::*;
1141
1142    #[test]
1143    fn test_parse_function() {
1144        let source = r#"
1145def hello_world():
1146    print("Hello, world!")
1147    return True
1148        "#;
1149
1150        let symbols = parse("test.py", source).unwrap();
1151        assert_eq!(symbols.len(), 1);
1152        assert_eq!(symbols[0].symbol.as_deref(), Some("hello_world"));
1153        assert!(matches!(symbols[0].kind, SymbolKind::Function));
1154    }
1155
1156    #[test]
1157    fn test_parse_async_function() {
1158        let source = r#"
1159async def fetch_data(url):
1160    async with aiohttp.ClientSession() as session:
1161        async with session.get(url) as response:
1162            return await response.text()
1163        "#;
1164
1165        let symbols = parse("test.py", source).unwrap();
1166        assert_eq!(symbols.len(), 1);
1167        assert_eq!(symbols[0].symbol.as_deref(), Some("fetch_data"));
1168        assert!(matches!(symbols[0].kind, SymbolKind::Function));
1169    }
1170
1171    #[test]
1172    fn test_parse_class() {
1173        let source = r#"
1174class User:
1175    def __init__(self, name, age):
1176        self.name = name
1177        self.age = age
1178        "#;
1179
1180        let symbols = parse("test.py", source).unwrap();
1181
1182        let class_symbols: Vec<_> = symbols
1183            .iter()
1184            .filter(|s| matches!(s.kind, SymbolKind::Class))
1185            .collect();
1186
1187        assert_eq!(class_symbols.len(), 1);
1188        assert_eq!(class_symbols[0].symbol.as_deref(), Some("User"));
1189    }
1190
1191    #[test]
1192    fn test_parse_class_with_methods() {
1193        let source = r#"
1194class Calculator:
1195    def add(self, a, b):
1196        return a + b
1197
1198    def subtract(self, a, b):
1199        return a - b
1200
1201    @staticmethod
1202    def multiply(a, b):
1203        return a * b
1204        "#;
1205
1206        let symbols = parse("test.py", source).unwrap();
1207
1208        let method_symbols: Vec<_> = symbols
1209            .iter()
1210            .filter(|s| matches!(s.kind, SymbolKind::Method))
1211            .collect();
1212
1213        assert_eq!(method_symbols.len(), 3);
1214        assert!(
1215            method_symbols
1216                .iter()
1217                .any(|s| s.symbol.as_deref() == Some("add"))
1218        );
1219        assert!(
1220            method_symbols
1221                .iter()
1222                .any(|s| s.symbol.as_deref() == Some("subtract"))
1223        );
1224        assert!(
1225            method_symbols
1226                .iter()
1227                .any(|s| s.symbol.as_deref() == Some("multiply"))
1228        );
1229
1230        // Check scope
1231        for _method in method_symbols {
1232            // Removed: scope field no longer exists: assert_eq!(method.scope.as_ref().unwrap(), "class Calculator");
1233        }
1234    }
1235
1236    #[test]
1237    fn test_parse_async_method() {
1238        let source = r#"
1239class DataFetcher:
1240    async def get_user(self, user_id):
1241        return await fetch(f"/users/{user_id}")
1242
1243    async def get_all_users(self):
1244        return await fetch("/users")
1245        "#;
1246
1247        let symbols = parse("test.py", source).unwrap();
1248
1249        let method_symbols: Vec<_> = symbols
1250            .iter()
1251            .filter(|s| matches!(s.kind, SymbolKind::Method))
1252            .collect();
1253
1254        assert_eq!(method_symbols.len(), 2);
1255        assert!(
1256            method_symbols
1257                .iter()
1258                .any(|s| s.symbol.as_deref() == Some("get_user"))
1259        );
1260        assert!(
1261            method_symbols
1262                .iter()
1263                .any(|s| s.symbol.as_deref() == Some("get_all_users"))
1264        );
1265    }
1266
1267    #[test]
1268    fn test_parse_constants() {
1269        let source = r#"
1270MAX_SIZE = 100
1271DEFAULT_TIMEOUT = 30
1272API_URL = "https://api.example.com"
1273        "#;
1274
1275        let symbols = parse("test.py", source).unwrap();
1276
1277        let const_symbols: Vec<_> = symbols
1278            .iter()
1279            .filter(|s| matches!(s.kind, SymbolKind::Constant))
1280            .collect();
1281
1282        assert_eq!(const_symbols.len(), 3);
1283        assert!(
1284            const_symbols
1285                .iter()
1286                .any(|s| s.symbol.as_deref() == Some("MAX_SIZE"))
1287        );
1288        assert!(
1289            const_symbols
1290                .iter()
1291                .any(|s| s.symbol.as_deref() == Some("DEFAULT_TIMEOUT"))
1292        );
1293        assert!(
1294            const_symbols
1295                .iter()
1296                .any(|s| s.symbol.as_deref() == Some("API_URL"))
1297        );
1298    }
1299
1300    #[test]
1301    fn test_parse_lambda() {
1302        let source = r#"
1303square = lambda x: x * x
1304add = lambda a, b: a + b
1305        "#;
1306
1307        let symbols = parse("test.py", source).unwrap();
1308
1309        let lambda_symbols: Vec<_> = symbols
1310            .iter()
1311            .filter(|s| matches!(s.kind, SymbolKind::Function))
1312            .collect();
1313
1314        assert!(lambda_symbols.len() >= 2);
1315        assert!(
1316            lambda_symbols
1317                .iter()
1318                .any(|s| s.symbol.as_deref() == Some("square"))
1319        );
1320        assert!(
1321            lambda_symbols
1322                .iter()
1323                .any(|s| s.symbol.as_deref() == Some("add"))
1324        );
1325    }
1326
1327    #[test]
1328    fn test_parse_decorated_method() {
1329        let source = r#"
1330class WebService:
1331    @property
1332    def url(self):
1333        return self._url
1334
1335    @classmethod
1336    def from_config(cls, config):
1337        return cls(config['url'])
1338
1339    @staticmethod
1340    def validate_url(url):
1341        return url.startswith('http')
1342        "#;
1343
1344        let symbols = parse("test.py", source).unwrap();
1345
1346        let method_symbols: Vec<_> = symbols
1347            .iter()
1348            .filter(|s| matches!(s.kind, SymbolKind::Method))
1349            .collect();
1350
1351        assert_eq!(method_symbols.len(), 3);
1352        assert!(
1353            method_symbols
1354                .iter()
1355                .any(|s| s.symbol.as_deref() == Some("url"))
1356        );
1357        assert!(
1358            method_symbols
1359                .iter()
1360                .any(|s| s.symbol.as_deref() == Some("from_config"))
1361        );
1362        assert!(
1363            method_symbols
1364                .iter()
1365                .any(|s| s.symbol.as_deref() == Some("validate_url"))
1366        );
1367    }
1368
1369    #[test]
1370    fn test_parse_mixed_symbols() {
1371        let source = r#"
1372API_KEY = "secret123"
1373MAX_RETRIES = 3
1374
1375class APIClient:
1376    def __init__(self, api_key):
1377        self.api_key = api_key
1378
1379    async def request(self, endpoint):
1380        return await self._fetch(endpoint)
1381
1382    @staticmethod
1383    def build_url(endpoint):
1384        return f"https://api.example.com/{endpoint}"
1385
1386def create_client():
1387    return APIClient(API_KEY)
1388
1389process = lambda data: data.strip().lower()
1390        "#;
1391
1392        let symbols = parse("test.py", source).unwrap();
1393
1394        // Should find: 2 constants, 1 class, 3 methods, 1 function, 1 lambda
1395        assert!(symbols.len() >= 8);
1396
1397        let kinds: Vec<&SymbolKind> = symbols.iter().map(|s| &s.kind).collect();
1398        assert!(kinds.contains(&&SymbolKind::Constant));
1399        assert!(kinds.contains(&&SymbolKind::Class));
1400        assert!(kinds.contains(&&SymbolKind::Method));
1401        assert!(kinds.contains(&&SymbolKind::Function));
1402    }
1403
1404    #[test]
1405    fn test_parse_nested_class() {
1406        let source = r#"
1407class Outer:
1408    class Inner:
1409        def inner_method(self):
1410            pass
1411
1412    def outer_method(self):
1413        pass
1414        "#;
1415
1416        let symbols = parse("test.py", source).unwrap();
1417
1418        let class_symbols: Vec<_> = symbols
1419            .iter()
1420            .filter(|s| matches!(s.kind, SymbolKind::Class))
1421            .collect();
1422
1423        // Should find both Outer and Inner classes
1424        assert_eq!(class_symbols.len(), 2);
1425        assert!(
1426            class_symbols
1427                .iter()
1428                .any(|s| s.symbol.as_deref() == Some("Outer"))
1429        );
1430        assert!(
1431            class_symbols
1432                .iter()
1433                .any(|s| s.symbol.as_deref() == Some("Inner"))
1434        );
1435    }
1436
1437    #[test]
1438    fn test_local_variables_included() {
1439        let source = r#"
1440def calculate(input):
1441    local_var = input * 2
1442    result = local_var + 10
1443    return result
1444
1445class Calculator:
1446    def compute(self, value):
1447        temp = value * 3
1448        final = temp + 5
1449        return final
1450        "#;
1451
1452        let symbols = parse("test.py", source).unwrap();
1453
1454        // Filter to just variables
1455        let variables: Vec<_> = symbols
1456            .iter()
1457            .filter(|s| matches!(s.kind, SymbolKind::Variable))
1458            .collect();
1459
1460        // Check that local variables are captured
1461        assert!(
1462            variables
1463                .iter()
1464                .any(|v| v.symbol.as_deref() == Some("local_var"))
1465        );
1466        assert!(
1467            variables
1468                .iter()
1469                .any(|v| v.symbol.as_deref() == Some("result"))
1470        );
1471        assert!(
1472            variables
1473                .iter()
1474                .any(|v| v.symbol.as_deref() == Some("temp"))
1475        );
1476        assert!(
1477            variables
1478                .iter()
1479                .any(|v| v.symbol.as_deref() == Some("final"))
1480        );
1481
1482        // Verify that local variables have no scope
1483        for _var in variables {
1484            // Removed: scope field no longer exists: assert_eq!(var.scope, None);
1485        }
1486    }
1487
1488    #[test]
1489    fn test_global_variables() {
1490        let source = r#"
1491# Global constants (uppercase)
1492MAX_SIZE = 100
1493DEFAULT_TIMEOUT = 30
1494
1495# Global variables (non-uppercase)
1496database_url = "postgresql://localhost/mydb"
1497config = {"debug": True}
1498current_user = None
1499
1500def get_config():
1501    return config
1502        "#;
1503
1504        let symbols = parse("test.py", source).unwrap();
1505
1506        // Filter to constants and variables
1507        let constants: Vec<_> = symbols
1508            .iter()
1509            .filter(|s| matches!(s.kind, SymbolKind::Constant))
1510            .collect();
1511
1512        let variables: Vec<_> = symbols
1513            .iter()
1514            .filter(|s| matches!(s.kind, SymbolKind::Variable))
1515            .collect();
1516
1517        // Check that constants are captured (uppercase)
1518        assert!(
1519            constants
1520                .iter()
1521                .any(|c| c.symbol.as_deref() == Some("MAX_SIZE"))
1522        );
1523        assert!(
1524            constants
1525                .iter()
1526                .any(|c| c.symbol.as_deref() == Some("DEFAULT_TIMEOUT"))
1527        );
1528
1529        // Check that global variables are captured (non-uppercase)
1530        assert!(
1531            variables
1532                .iter()
1533                .any(|v| v.symbol.as_deref() == Some("database_url"))
1534        );
1535        assert!(
1536            variables
1537                .iter()
1538                .any(|v| v.symbol.as_deref() == Some("config"))
1539        );
1540        assert!(
1541            variables
1542                .iter()
1543                .any(|v| v.symbol.as_deref() == Some("current_user"))
1544        );
1545
1546        // Verify no scope for both
1547        for _constant in constants {
1548            // Removed: scope field no longer exists: assert_eq!(constant.scope, None);
1549        }
1550        for _var in variables {
1551            // Removed: scope field no longer exists: assert_eq!(var.scope, None);
1552        }
1553    }
1554
1555    #[test]
1556    fn test_find_all_python_configs() {
1557        use std::fs;
1558        use tempfile::TempDir;
1559
1560        let temp = TempDir::new().unwrap();
1561        let root = temp.path();
1562
1563        // Create multiple Python projects
1564        let project1 = root.join("backend");
1565        fs::create_dir_all(&project1).unwrap();
1566        fs::write(
1567            project1.join("pyproject.toml"),
1568            "[project]\nname = \"backend\"",
1569        )
1570        .unwrap();
1571
1572        let project2 = root.join("frontend/api");
1573        fs::create_dir_all(&project2).unwrap();
1574        fs::write(project2.join("setup.py"), "setup(name='api')").unwrap();
1575
1576        // Create venv directory that should be skipped
1577        let venv = root.join("venv");
1578        fs::create_dir_all(&venv).unwrap();
1579        fs::write(venv.join("setup.py"), "setup(name='should_skip')").unwrap();
1580
1581        let configs = find_all_python_configs(root).unwrap();
1582
1583        // Should find 2 configs (skipping venv)
1584        assert_eq!(configs.len(), 2);
1585        assert!(
1586            configs
1587                .iter()
1588                .any(|p| p.ends_with("backend/pyproject.toml"))
1589        );
1590        assert!(configs.iter().any(|p| p.ends_with("frontend/api/setup.py")));
1591    }
1592
1593    #[test]
1594    fn test_parse_all_python_packages() {
1595        use std::fs;
1596        use tempfile::TempDir;
1597
1598        let temp = TempDir::new().unwrap();
1599        let root = temp.path();
1600
1601        // Create multiple Python projects with different config types
1602        let project1 = root.join("services/auth");
1603        fs::create_dir_all(&project1).unwrap();
1604        fs::write(
1605            project1.join("pyproject.toml"),
1606            "[project]\nname = \"auth-service\"\n",
1607        )
1608        .unwrap();
1609
1610        let project2 = root.join("services/api");
1611        fs::create_dir_all(&project2).unwrap();
1612        fs::write(project2.join("setup.py"), "setup(name=\"api-service\")").unwrap();
1613
1614        let packages = parse_all_python_packages(root).unwrap();
1615
1616        // Should find 2 packages
1617        assert_eq!(packages.len(), 2);
1618
1619        // Check package names (normalized to lowercase)
1620        let names: Vec<_> = packages.iter().map(|p| p.name.as_str()).collect();
1621        assert!(names.contains(&"auth-service"));
1622        assert!(names.contains(&"api-service"));
1623
1624        // Check project roots
1625        for package in &packages {
1626            assert!(package.project_root.starts_with("services/"));
1627            assert!(package.abs_project_root.ends_with(&package.project_root));
1628        }
1629    }
1630
1631    #[test]
1632    fn test_resolve_python_import_absolute() {
1633        use std::fs;
1634        use tempfile::TempDir;
1635
1636        let temp = TempDir::new().unwrap();
1637        let root = temp.path();
1638
1639        // Create a Python package structure
1640        let myapp = root.join("myapp");
1641        fs::create_dir_all(myapp.join("models")).unwrap();
1642        fs::write(
1643            myapp.join("pyproject.toml"),
1644            "[project]\nname = \"myapp\"\n",
1645        )
1646        .unwrap();
1647
1648        let packages = parse_all_python_packages(root).unwrap();
1649        assert_eq!(packages.len(), 1);
1650
1651        // Test absolute import resolution
1652        // "myapp.models.user" → "myapp/models/user.py"
1653        let resolved = python_import_candidates("myapp.models.user", &packages, None)
1654            .into_iter()
1655            .next();
1656
1657        assert!(resolved.is_some());
1658        let path = resolved.unwrap();
1659        assert!(
1660            path.contains("myapp/models/user.py") || path.contains("myapp/models/user/__init__.py")
1661        );
1662    }
1663
1664    #[test]
1665    fn test_resolve_python_import_relative() {
1666        // Test relative imports: from .models import User
1667        let current_file = "myapp/views/admin.py";
1668
1669        // Test single dot (current package)
1670        let resolved = python_import_candidates(
1671            ".models",
1672            &[], // Empty packages array - relative imports don't need it
1673            Some(current_file),
1674        )
1675        .into_iter()
1676        .next();
1677
1678        assert!(resolved.is_some());
1679        let path = resolved.unwrap();
1680        // from .models → myapp/views/models.py or myapp/views/models/__init__.py
1681        assert!(path.contains("myapp/views/models"));
1682
1683        // Test double dot (parent package)
1684        let resolved = python_import_candidates("..utils", &[], Some(current_file))
1685            .into_iter()
1686            .next();
1687
1688        assert!(resolved.is_some());
1689        let path = resolved.unwrap();
1690        // from ..utils → myapp/utils.py or myapp/utils/__init__.py
1691        assert!(path.contains("myapp/utils"));
1692    }
1693
1694    #[test]
1695    fn test_resolve_python_import_relative_with_module() {
1696        // Test relative imports with module path: from ..models.user import User
1697        let current_file = "myapp/views/dashboard/index.py";
1698
1699        let resolved = python_import_candidates("..models.user", &[], Some(current_file))
1700            .into_iter()
1701            .next();
1702
1703        assert!(resolved.is_some());
1704        let path = resolved.unwrap();
1705        // from ..models.user → myapp/views/models/user.py
1706        assert!(path.contains("models/user"));
1707    }
1708
1709    #[test]
1710    fn test_resolve_python_import_not_found() {
1711        use std::fs;
1712        use tempfile::TempDir;
1713
1714        let temp = TempDir::new().unwrap();
1715        let root = temp.path();
1716
1717        let myapp = root.join("myapp");
1718        fs::create_dir_all(&myapp).unwrap();
1719        fs::write(
1720            myapp.join("pyproject.toml"),
1721            "[project]\nname = \"myapp\"\n",
1722        )
1723        .unwrap();
1724
1725        let packages = parse_all_python_packages(root).unwrap();
1726
1727        // Try to resolve an import for a different package
1728        let resolved = python_import_candidates("other_package.module", &packages, None)
1729            .into_iter()
1730            .next();
1731
1732        // Should return None for packages not in the monorepo
1733        assert!(resolved.is_none());
1734    }
1735
1736    #[test]
1737    fn test_dynamic_imports_filtered() {
1738        let source = r#"
1739import os
1740import sys
1741from json import loads
1742from .models import User
1743
1744# Dynamic imports - should be filtered out
1745import importlib
1746mod = importlib.import_module("some_module")
1747pkg = __import__("package")
1748exec("import dynamic")
1749        "#;
1750
1751        let deps = PythonDependencyExtractor::extract_dependencies(source).unwrap();
1752
1753        // Should only find static imports (os, sys, json, .models, importlib)
1754        // importlib.import_module(), __import__(), and exec() are NOT import statements
1755        assert_eq!(deps.len(), 5, "Should extract 5 static imports only");
1756
1757        assert!(deps.iter().any(|d| d.imported_path == "os"));
1758        assert!(deps.iter().any(|d| d.imported_path == "sys"));
1759        assert!(deps.iter().any(|d| d.imported_path == "json"));
1760        assert!(deps.iter().any(|d| d.imported_path == ".models"));
1761        assert!(deps.iter().any(|d| d.imported_path == "importlib"));
1762
1763        // Verify dynamic imports are NOT captured
1764        assert!(!deps.iter().any(|d| d.imported_path.contains("some_module")));
1765        assert!(
1766            !deps
1767                .iter()
1768                .any(|d| d.imported_path.contains("package") && d.imported_path != "json")
1769        );
1770        assert!(!deps.iter().any(|d| d.imported_path.contains("dynamic")));
1771    }
1772
1773    fn package(name: &str, root: &str) -> PythonPackage {
1774        PythonPackage {
1775            name: name.to_string(),
1776            project_root: root.to_string(),
1777            abs_project_root: std::path::PathBuf::from("/ws").join(root),
1778        }
1779    }
1780
1781    #[test]
1782    fn empty_root_no_leading_slash() {
1783        assert_eq!(
1784            python_import_candidates("django.db", &[package("django", "")], None),
1785            ["django/db.py", "django/db/__init__.py"]
1786        );
1787        assert_eq!(
1788            python_import_candidates("acme.views", &[package("acme", "backend")], None),
1789            ["backend/acme/views.py", "backend/acme/views/__init__.py"]
1790        );
1791        assert!(python_import_candidates("other.x", &[package("acme", "")], None).is_empty());
1792    }
1793
1794    #[test]
1795    fn relative_candidates_try_module_then_package() {
1796        assert_eq!(
1797            python_import_candidates("..db.models", &[], Some("acme/web/views.py")),
1798            ["acme/db/models.py", "acme/db/models/__init__.py"]
1799        );
1800        assert_eq!(
1801            python_import_candidates(".", &[], Some("acme/views.py")),
1802            ["acme/__init__.py"]
1803        );
1804    }
1805
1806    #[test]
1807    fn from_import_symbols_exclude_the_module() {
1808        let deps = PythonDependencyExtractor::extract_dependencies(
1809            "from django.core.management import BaseCommand, call_command as cc\n",
1810        )
1811        .unwrap();
1812        assert_eq!(
1813            deps[0].imported_symbols.as_deref(),
1814            Some(&["BaseCommand".to_string(), "call_command".to_string()][..])
1815        );
1816    }
1817}