1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Python language support.
use crate::external_packages::ResolvedPackage;
use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use tree_sitter::Node;
// ============================================================================
// Python path cache (filesystem-based detection, no subprocess calls)
// ============================================================================
static PYTHON_CACHE: Mutex<Option<PythonPathCache>> = Mutex::new(None);
/// Cached Python paths detected from filesystem structure.
#[derive(Clone)]
struct PythonPathCache {
/// Canonical project root used as cache key
root: PathBuf,
/// Python version (e.g., "3.13")
version: Option<String>,
/// Stdlib path (e.g., /usr/.../lib/python3.13/)
stdlib: Option<PathBuf>,
/// Site-packages path
site_packages: Option<PathBuf>,
}
impl PythonPathCache {
fn new(root: &Path) -> Self {
let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
// Try to find Python from venv or PATH
let python_bin = if root.join(".venv/bin/python").exists() {
Some(root.join(".venv/bin/python"))
} else if root.join("venv/bin/python").exists() {
Some(root.join("venv/bin/python"))
} else {
// Look in PATH
std::env::var("PATH").ok().and_then(|path| {
for dir in path.split(':') {
let python = PathBuf::from(dir).join("python3");
if python.exists() {
return Some(python);
}
let python = PathBuf::from(dir).join("python");
if python.exists() {
return Some(python);
}
}
None
})
};
let Some(python_bin) = python_bin else {
return Self {
root,
version: None,
stdlib: None,
site_packages: None,
};
};
// Resolve symlinks to find the actual Python installation
let python_real = std::fs::canonicalize(&python_bin).unwrap_or(python_bin.clone());
// Python binary is typically at /prefix/bin/python3
// Stdlib is at /prefix/lib/pythonX.Y/
// Site-packages is at /prefix/lib/pythonX.Y/site-packages/ (system)
// Or for venv: venv/lib/pythonX.Y/site-packages/
let prefix = python_real.parent().and_then(|bin| bin.parent());
// Look for lib/pythonX.Y directories to detect version
let (version, stdlib, site_packages) = if let Some(prefix) = prefix {
let lib = prefix.join("lib");
if lib.exists() {
// Find pythonX.Y directories
let mut best_version: Option<(String, PathBuf)> = None;
if let Ok(entries) = std::fs::read_dir(&lib) {
for entry in entries.flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
if name.starts_with("python") && entry.path().is_dir() {
let ver = name.trim_start_matches("python");
// Check it looks like a version (X.Y)
if ver.contains('.')
&& ver.chars().next().is_some_and(|c| c.is_ascii_digit())
{
// Prefer higher versions
if best_version.as_ref().is_none_or(|(v, _)| ver > v.as_str()) {
best_version = Some((ver.to_string(), entry.path()));
}
}
}
}
}
if let Some((ver, stdlib_path)) = best_version {
// For venv, site-packages is in the venv
let site = if root.join(".venv").exists() || root.join("venv").exists() {
let venv = if root.join(".venv").exists() {
root.join(".venv")
} else {
root.join("venv")
};
let venv_site = venv
.join("lib")
.join(format!("python{}", ver))
.join("site-packages");
if venv_site.exists() {
Some(venv_site)
} else {
// Fall back to system site-packages
let sys_site = stdlib_path.join("site-packages");
if sys_site.exists() {
Some(sys_site)
} else {
None
}
}
} else {
let sys_site = stdlib_path.join("site-packages");
if sys_site.exists() {
Some(sys_site)
} else {
None
}
};
(Some(ver), Some(stdlib_path), site)
} else {
(None, None, None)
}
} else {
(None, None, None)
}
} else {
(None, None, None)
};
Self {
root,
version,
stdlib,
site_packages,
}
}
}
/// Get cached Python paths for a project.
fn get_python_cache(project_root: &Path) -> PythonPathCache {
let canonical = project_root
.canonicalize()
.unwrap_or_else(|_| project_root.to_path_buf());
let mut cache_guard = PYTHON_CACHE.lock().unwrap();
if let Some(ref cache) = *cache_guard {
if cache.root == canonical {
return cache.clone();
}
}
let new_cache = PythonPathCache::new(project_root);
*cache_guard = Some(new_cache.clone());
new_cache
}
// ============================================================================
// Python stdlib and site-packages resolution
// ============================================================================
/// Get Python version from filesystem structure (no subprocess).
pub fn get_python_version(project_root: &Path) -> Option<String> {
get_python_cache(project_root).version
}
/// Find Python stdlib directory from filesystem structure (no subprocess).
pub fn find_python_stdlib(project_root: &Path) -> Option<PathBuf> {
get_python_cache(project_root).stdlib
}
/// Check if a module name is a Python stdlib module.
fn is_python_stdlib_module(module_name: &str, stdlib_path: &Path) -> bool {
let top_level = module_name.split('.').next().unwrap_or(module_name);
// Check for package
let pkg_dir = stdlib_path.join(top_level);
if pkg_dir.is_dir() {
return true;
}
// Check for module
let py_file = stdlib_path.join(format!("{}.py", top_level));
py_file.is_file()
}
/// Resolve a Python stdlib import to its source location.
fn resolve_python_stdlib_import(import_name: &str, stdlib_path: &Path) -> Option<ResolvedPackage> {
let parts: Vec<&str> = import_name.split('.').collect();
let top_level = parts[0];
// Check for package (directory)
let pkg_dir = stdlib_path.join(top_level);
if pkg_dir.is_dir() {
if parts.len() == 1 {
let init = pkg_dir.join("__init__.py");
if init.is_file() {
return Some(ResolvedPackage {
path: pkg_dir,
name: import_name.to_string(),
is_namespace: false,
});
}
// Some stdlib packages don't have __init__.py in newer Python
return Some(ResolvedPackage {
path: pkg_dir,
name: import_name.to_string(),
is_namespace: true,
});
} else {
// Submodule
let mut path = pkg_dir.clone();
for part in &parts[1..] {
path = path.join(part);
}
if path.is_dir() {
let init = path.join("__init__.py");
return Some(ResolvedPackage {
path: path.clone(),
name: import_name.to_string(),
is_namespace: !init.is_file(),
});
}
let py_file = path.with_extension("py");
if py_file.is_file() {
return Some(ResolvedPackage {
path: py_file,
name: import_name.to_string(),
is_namespace: false,
});
}
return None;
}
}
// Check for single-file module
let py_file = stdlib_path.join(format!("{}.py", top_level));
if py_file.is_file() {
return Some(ResolvedPackage {
path: py_file,
name: import_name.to_string(),
is_namespace: false,
});
}
None
}
/// Find Python site-packages directory for a project.
///
/// Search order:
/// 1. .venv/lib/pythonX.Y/site-packages/ (uv, poetry, standard venv)
/// 2. Walk up looking for venv directories
pub fn find_python_site_packages(project_root: &Path) -> Option<PathBuf> {
// Use cached result from filesystem detection
if let Some(site) = get_python_cache(project_root).site_packages {
return Some(site);
}
// Fall back to scanning parent directories for venvs
let mut current = project_root.to_path_buf();
while let Some(parent) = current.parent() {
let venv_dir = parent.join(".venv");
if venv_dir.is_dir() {
if let Some(site_packages) = find_site_packages_in_venv(&venv_dir) {
return Some(site_packages);
}
}
current = parent.to_path_buf();
}
None
}
/// Find site-packages within a venv directory.
fn find_site_packages_in_venv(venv: &Path) -> Option<PathBuf> {
// Unix: lib/pythonX.Y/site-packages
let lib_dir = venv.join("lib");
if lib_dir.is_dir() {
if let Ok(entries) = std::fs::read_dir(&lib_dir) {
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with("python") {
let site_packages = entry.path().join("site-packages");
if site_packages.is_dir() {
return Some(site_packages);
}
}
}
}
}
// Windows: Lib/site-packages
let lib_dir = venv.join("Lib").join("site-packages");
if lib_dir.is_dir() {
return Some(lib_dir);
}
None
}
/// Resolve a Python import to its source location.
///
/// Handles:
/// - Package imports (requests -> requests/__init__.py)
/// - Module imports (six -> six.py)
/// - Submodule imports (requests.api -> requests/api.py)
/// - Namespace packages (no __init__.py)
fn resolve_python_import(import_name: &str, site_packages: &Path) -> Option<ResolvedPackage> {
// Split on dots for submodule resolution
let parts: Vec<&str> = import_name.split('.').collect();
let top_level = parts[0];
// Check for package (directory)
let pkg_dir = site_packages.join(top_level);
if pkg_dir.is_dir() {
if parts.len() == 1 {
// Just the package - look for __init__.py
let init = pkg_dir.join("__init__.py");
if init.is_file() {
return Some(ResolvedPackage {
path: pkg_dir,
name: import_name.to_string(),
is_namespace: false,
});
}
// Namespace package (no __init__.py)
return Some(ResolvedPackage {
path: pkg_dir,
name: import_name.to_string(),
is_namespace: true,
});
} else {
// Submodule - build path
let mut path = pkg_dir.clone();
for part in &parts[1..] {
path = path.join(part);
}
// Try as package first
if path.is_dir() {
let init = path.join("__init__.py");
return Some(ResolvedPackage {
path: path.clone(),
name: import_name.to_string(),
is_namespace: !init.is_file(),
});
}
// Try as module
let py_file = path.with_extension("py");
if py_file.is_file() {
return Some(ResolvedPackage {
path: py_file,
name: import_name.to_string(),
is_namespace: false,
});
}
return None;
}
}
// Check for single-file module
let py_file = site_packages.join(format!("{}.py", top_level));
if py_file.is_file() {
return Some(ResolvedPackage {
path: py_file,
name: import_name.to_string(),
is_namespace: false,
});
}
None
}
// ============================================================================
// Python language support
// ============================================================================
/// Python language support.
pub struct Python;
impl Language for Python {
fn name(&self) -> &'static str {
"Python"
}
fn extensions(&self) -> &'static [&'static str] {
&["py", "pyi", "pyw"]
}
fn grammar_name(&self) -> &'static str {
"python"
}
fn has_symbols(&self) -> bool {
true
}
fn container_kinds(&self) -> &'static [&'static str] {
&["class_definition"]
}
fn function_kinds(&self) -> &'static [&'static str] {
&["function_definition"]
}
fn type_kinds(&self) -> &'static [&'static str] {
&["class_definition"]
}
fn import_kinds(&self) -> &'static [&'static str] {
&["import_statement", "import_from_statement"]
}
fn public_symbol_kinds(&self) -> &'static [&'static str] {
&["function_definition", "class_definition"]
}
fn visibility_mechanism(&self) -> VisibilityMechanism {
VisibilityMechanism::NamingConvention
}
fn complexity_nodes(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"while_statement",
"try_statement",
"except_clause",
"with_statement",
"match_statement",
"case_clause",
"and",
"or",
"conditional_expression",
"list_comprehension",
"dictionary_comprehension",
"set_comprehension",
"generator_expression",
]
}
fn nesting_nodes(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"while_statement",
"try_statement",
"with_statement",
"match_statement",
"function_definition",
"class_definition",
]
}
fn signature_suffix(&self) -> &'static str {
""
}
fn scope_creating_kinds(&self) -> &'static [&'static str] {
// Additional scope-creating nodes beyond functions and containers
&[
"for_statement",
"with_statement",
"list_comprehension",
"set_comprehension",
"dictionary_comprehension",
"generator_expression",
"lambda",
]
}
fn control_flow_kinds(&self) -> &'static [&'static str] {
&[
"if_statement",
"for_statement",
"while_statement",
"try_statement",
"with_statement",
"match_statement",
"return_statement",
"break_statement",
"continue_statement",
"raise_statement",
"assert_statement",
]
}
fn extract_function(&self, node: &Node, content: &str, in_container: bool) -> Option<Symbol> {
let name = self.node_name(node, content)?;
// Skip private methods unless they're dunder methods
// (visibility filtering can be done by caller)
// Check for async keyword as first child token
let is_async = node
.child(0)
.map(|c| &content[c.byte_range()] == "async")
.unwrap_or(false);
let prefix = if is_async { "async def" } else { "def" };
let params = node
.child_by_field_name("parameters")
.map(|p| &content[p.byte_range()])
.unwrap_or("()");
let return_type = node
.child_by_field_name("return_type")
.map(|r| format!(" -> {}", &content[r.byte_range()]))
.unwrap_or_default();
let signature = format!("{} {}{}{}", prefix, name, params, return_type);
let visibility = self.get_visibility(node, content);
Some(Symbol {
name: name.to_string(),
kind: if in_container {
SymbolKind::Method
} else {
SymbolKind::Function
},
signature,
docstring: self.extract_docstring(node, content),
attributes: Vec::new(),
start_line: node.start_position().row + 1,
end_line: node.end_position().row + 1,
visibility,
children: Vec::new(),
is_interface_impl: false,
implements: Vec::new(),
})
}
fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
let name = self.node_name(node, content)?;
let bases = node
.child_by_field_name("superclasses")
.map(|b| &content[b.byte_range()])
.unwrap_or("");
let signature = if bases.is_empty() {
format!("class {}", name)
} else {
format!("class {}{}", name, bases)
};
Some(Symbol {
name: name.to_string(),
kind: SymbolKind::Class,
signature,
docstring: self.extract_docstring(node, content),
attributes: Vec::new(),
start_line: node.start_position().row + 1,
end_line: node.end_position().row + 1,
visibility: self.get_visibility(node, content),
children: Vec::new(), // Caller fills this in
is_interface_impl: false,
implements: Vec::new(),
})
}
fn extract_type(&self, node: &Node, content: &str) -> Option<Symbol> {
// Python classes are both containers and types
self.extract_container(node, content)
}
fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
let body = node.child_by_field_name("body")?;
let first = body.child(0)?;
// Handle both grammar versions:
// - Old: expression_statement > string
// - New (arborium): string directly, with string_content child
let string_node = match first.kind() {
"string" => Some(first),
"expression_statement" => first.child(0).filter(|n| n.kind() == "string"),
_ => None,
}?;
// Try string_content child (arborium style)
let mut cursor = string_node.walk();
for child in string_node.children(&mut cursor) {
if child.kind() == "string_content" {
let doc = content[child.byte_range()].trim();
if !doc.is_empty() {
return Some(doc.to_string());
}
}
}
// Fallback: extract from full string text (old style)
let text = &content[string_node.byte_range()];
let doc = text
.trim_start_matches("\"\"\"")
.trim_start_matches("'''")
.trim_start_matches('"')
.trim_start_matches('\'')
.trim_end_matches("\"\"\"")
.trim_end_matches("'''")
.trim_end_matches('"')
.trim_end_matches('\'')
.trim();
if !doc.is_empty() {
Some(doc.to_string())
} else {
None
}
}
fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
Vec::new()
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
let line = node.start_position().row + 1;
match node.kind() {
"import_statement" => {
// import foo, import foo as bar
let mut imports = Vec::new();
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "dotted_name" {
let module = content[child.byte_range()].to_string();
imports.push(Import {
module,
names: Vec::new(),
alias: None,
is_wildcard: false,
is_relative: false,
line,
});
} else if child.kind() == "aliased_import" {
if let Some(name) = child.child_by_field_name("name") {
let module = content[name.byte_range()].to_string();
let alias = child
.child_by_field_name("alias")
.map(|a| content[a.byte_range()].to_string());
imports.push(Import {
module,
names: Vec::new(),
alias,
is_wildcard: false,
is_relative: false,
line,
});
}
}
}
imports
}
"import_from_statement" => {
// from foo import bar, baz
let module = node
.child_by_field_name("module_name")
.map(|m| content[m.byte_range()].to_string())
.unwrap_or_default();
// Check for relative import (from . or from .. or from .foo)
let text = &content[node.byte_range()];
let is_relative = text.starts_with("from .");
let mut names = Vec::new();
let mut is_wildcard = false;
let module_end = node
.child_by_field_name("module_name")
.map(|m| m.end_byte())
.unwrap_or(0);
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"dotted_name" | "identifier" => {
// Skip the module name itself
if child.start_byte() > module_end {
names.push(content[child.byte_range()].to_string());
}
}
"aliased_import" => {
if let Some(name) = child.child_by_field_name("name") {
names.push(content[name.byte_range()].to_string());
}
}
"wildcard_import" => {
is_wildcard = true;
}
_ => {}
}
}
vec![Import {
module,
names,
alias: None,
is_wildcard,
is_relative,
line,
}]
}
_ => Vec::new(),
}
}
fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
let names_to_use: Vec<&str> = names
.map(|n| n.to_vec())
.unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
if import.is_wildcard {
format!("from {} import *", import.module)
} else if names_to_use.is_empty() {
if let Some(ref alias) = import.alias {
format!("import {} as {}", import.module, alias)
} else {
format!("import {}", import.module)
}
} else {
format!("from {} import {}", import.module, names_to_use.join(", "))
}
}
fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
let line = node.start_position().row + 1;
match node.kind() {
"function_definition" => {
if let Some(name) = self.node_name(node, content) {
if !name.starts_with('_') {
return vec![Export {
name: name.to_string(),
kind: SymbolKind::Function,
line,
}];
}
}
Vec::new()
}
"class_definition" => {
if let Some(name) = self.node_name(node, content) {
if !name.starts_with('_') {
return vec![Export {
name: name.to_string(),
kind: SymbolKind::Class,
line,
}];
}
}
Vec::new()
}
_ => Vec::new(),
}
}
fn is_public(&self, node: &Node, content: &str) -> bool {
if let Some(name) = self.node_name(node, content) {
// Public if doesn't start with _ or is dunder method
!name.starts_with('_') || name.starts_with("__")
} else {
true
}
}
fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
if let Some(name) = self.node_name(node, content) {
if name.starts_with("__") && name.ends_with("__") {
Visibility::Public // dunder methods
} else if name.starts_with("__") {
Visibility::Private // name mangled
} else if name.starts_with('_') {
Visibility::Protected // convention private
} else {
Visibility::Public
}
} else {
Visibility::Public
}
}
fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
let name = symbol.name.as_str();
match symbol.kind {
crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
crate::SymbolKind::Class => name.starts_with("Test") && name.len() > 4,
crate::SymbolKind::Module => name == "tests" || name == "test" || name == "__tests__",
_ => false,
}
}
fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
None
}
fn body_has_docstring(&self, body: &Node, content: &str) -> bool {
let _ = content;
body.child(0)
.map(|c| {
c.kind() == "string"
|| (c.kind() == "expression_statement"
&& c.child(0).map(|n| n.kind() == "string").unwrap_or(false))
})
.unwrap_or(false)
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
node.child_by_field_name("body")
}
fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
let name_node = node.child_by_field_name("name")?;
Some(&content[name_node.byte_range()])
}
// === Import Resolution ===
fn lang_key(&self) -> &'static str {
"python"
}
fn resolve_local_import(
&self,
import_name: &str,
current_file: &Path,
project_root: &Path,
) -> Option<PathBuf> {
// Handle relative imports (starting with .)
if import_name.starts_with('.') {
let current_dir = current_file.parent()?;
let dots = import_name.chars().take_while(|c| *c == '.').count();
let module_part = &import_name[dots..];
// Go up (dots-1) directories from current file's directory
let mut base = current_dir.to_path_buf();
for _ in 1..dots {
base = base.parent()?.to_path_buf();
}
// Convert module.path to module/path.py
let module_path = if module_part.is_empty() {
base.join("__init__.py")
} else {
let path_part = module_part.replace('.', "/");
// Try module/submodule.py first, then module/submodule/__init__.py
let direct = base.join(format!("{}.py", path_part));
if direct.exists() {
return Some(direct);
}
base.join(path_part).join("__init__.py")
};
if module_path.exists() {
return Some(module_path);
}
}
// Handle absolute imports - try to find in src/ or as top-level package
let module_path = import_name.replace('.', "/");
// Try src/<module>.py
let src_path = project_root.join("src").join(format!("{}.py", module_path));
if src_path.exists() {
return Some(src_path);
}
// Try src/<module>/__init__.py
let src_pkg_path = project_root
.join("src")
.join(&module_path)
.join("__init__.py");
if src_pkg_path.exists() {
return Some(src_pkg_path);
}
// Try <module>.py directly
let direct_path = project_root.join(format!("{}.py", module_path));
if direct_path.exists() {
return Some(direct_path);
}
// Try <module>/__init__.py
let pkg_path = project_root.join(&module_path).join("__init__.py");
if pkg_path.exists() {
return Some(pkg_path);
}
None
}
fn resolve_external_import(
&self,
import_name: &str,
project_root: &Path,
) -> Option<ResolvedPackage> {
// Check stdlib first
if let Some(stdlib) = find_python_stdlib(project_root)
&& let Some(pkg) = resolve_python_stdlib_import(import_name, &stdlib)
{
return Some(pkg);
}
// Then site-packages
if let Some(site_packages) = find_python_site_packages(project_root) {
return resolve_python_import(import_name, &site_packages);
}
None
}
fn is_stdlib_import(&self, import_name: &str, project_root: &Path) -> bool {
if let Some(stdlib) = find_python_stdlib(project_root) {
is_python_stdlib_module(import_name, &stdlib)
} else {
false
}
}
fn get_version(&self, project_root: &Path) -> Option<String> {
get_python_version(project_root)
}
fn find_package_cache(&self, project_root: &Path) -> Option<PathBuf> {
find_python_site_packages(project_root)
}
fn indexable_extensions(&self) -> &'static [&'static str] {
&["py"]
}
fn find_stdlib(&self, project_root: &Path) -> Option<PathBuf> {
find_python_stdlib(project_root)
}
fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
// Skip private modules
if name.starts_with('_') {
return true;
}
// Skip __pycache__, dist-info, egg-info
if name == "__pycache__" || name.ends_with(".dist-info") || name.ends_with(".egg-info") {
return true;
}
// Skip non-Python files
if !is_dir && !name.ends_with(".py") {
return true;
}
false
}
fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
if path.is_file() {
return Some(path.to_path_buf());
}
// Python packages use __init__.py as entry point
let init_py = path.join("__init__.py");
if init_py.is_file() {
return Some(init_py);
}
None
}
fn package_module_name(&self, entry_name: &str) -> String {
// Strip .py extension
entry_name
.strip_suffix(".py")
.unwrap_or(entry_name)
.to_string()
}
fn package_sources(&self, project_root: &Path) -> Vec<crate::PackageSource> {
let mut sources = Vec::new();
if let Some(stdlib) = self.find_stdlib(project_root) {
sources.push(crate::PackageSource {
name: "stdlib",
path: stdlib,
kind: crate::PackageSourceKind::Flat,
version_specific: true,
});
}
if let Some(cache) = self.find_package_cache(project_root) {
sources.push(crate::PackageSource {
name: "site-packages",
path: cache,
kind: crate::PackageSourceKind::Flat,
version_specific: false,
});
}
sources
}
fn discover_packages(&self, source: &crate::PackageSource) -> Vec<(String, PathBuf)> {
self.discover_flat_packages(&source.path)
}
fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
// Only Python files
if path.extension()?.to_str()? != "py" {
return None;
}
// Remove extension
let stem = path.with_extension("");
let stem_str = stem.to_str()?;
// Strip common source directory prefixes
let module_path = stem_str
.strip_prefix("src/")
.or_else(|| stem_str.strip_prefix("lib/"))
.unwrap_or(stem_str);
// Handle __init__.py - use parent directory as module
let module_path = if module_path.ends_with("/__init__") {
module_path.strip_suffix("/__init__")?
} else {
module_path
};
// Convert path separators to dots
Some(module_path.replace('/', "."))
}
fn module_name_to_paths(&self, module: &str) -> Vec<String> {
// Convert dots to path separators
let rel_path = module.replace('.', "/");
// Try common source directories and both .py and __init__.py
let mut candidates = Vec::with_capacity(4);
for prefix in &["src/", ""] {
candidates.push(format!("{}{}.py", prefix, rel_path));
candidates.push(format!("{}{}/__init__.py", prefix, rel_path));
}
candidates
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::GrammarLoader;
use tree_sitter::Parser;
struct ParseResult {
tree: tree_sitter::Tree,
#[allow(dead_code)]
loader: GrammarLoader,
}
fn parse_python(content: &str) -> ParseResult {
let loader = GrammarLoader::new();
let language = loader.get("python").unwrap();
let mut parser = Parser::new();
parser.set_language(&language).unwrap();
ParseResult {
tree: parser.parse(content, None).unwrap(),
loader,
}
}
#[test]
fn test_python_function_kinds() {
let support = Python;
assert!(support.function_kinds().contains(&"function_definition"));
// async functions are function_definition with "async" keyword as first child
}
#[test]
fn test_python_extract_function() {
let support = Python;
let content = r#"def foo(x: int) -> str:
"""Convert to string."""
return str(x)
"#;
let result = parse_python(content);
let root = result.tree.root_node();
// Find function node
let mut cursor = root.walk();
let func = root
.children(&mut cursor)
.find(|n| n.kind() == "function_definition")
.unwrap();
let sym = support.extract_function(&func, content, false).unwrap();
assert_eq!(sym.name, "foo");
assert_eq!(sym.kind, SymbolKind::Function);
assert!(sym.signature.contains("def foo(x: int) -> str"));
assert_eq!(sym.docstring, Some("Convert to string.".to_string()));
}
#[test]
fn test_python_extract_class() {
let support = Python;
let content = r#"class Foo(Bar):
"""A foo class."""
pass
"#;
let result = parse_python(content);
let root = result.tree.root_node();
let mut cursor = root.walk();
let class = root
.children(&mut cursor)
.find(|n| n.kind() == "class_definition")
.unwrap();
let sym = support.extract_container(&class, content).unwrap();
assert_eq!(sym.name, "Foo");
assert_eq!(sym.kind, SymbolKind::Class);
assert!(sym.signature.contains("class Foo(Bar)"));
assert_eq!(sym.docstring, Some("A foo class.".to_string()));
}
#[test]
fn test_python_visibility() {
let support = Python;
let content = r#"def public(): pass
def _protected(): pass
def __private(): pass
def __dunder__(): pass
"#;
let result = parse_python(content);
let root = result.tree.root_node();
let mut cursor = root.walk();
let funcs: Vec<_> = root
.children(&mut cursor)
.filter(|n| n.kind() == "function_definition")
.collect();
assert_eq!(
support.get_visibility(&funcs[0], content),
Visibility::Public
);
assert_eq!(
support.get_visibility(&funcs[1], content),
Visibility::Protected
);
assert_eq!(
support.get_visibility(&funcs[2], content),
Visibility::Private
);
assert_eq!(
support.get_visibility(&funcs[3], content),
Visibility::Public
); // dunder
}
/// Documents node kinds that exist in the Python grammar but aren't used in trait methods.
/// Each exclusion has a reason. Review periodically as features expand.
///
/// Run `cross_check_node_kinds` in registry.rs to see all potentially useful kinds.
#[test]
fn unused_node_kinds_audit() {
use crate::validate_unused_kinds_audit;
// Categories:
// - STRUCTURAL: Internal/wrapper nodes, not semantically meaningful on their own
// - CLAUSE: Sub-parts of statements, handled via parent (e.g., else_clause in if_statement)
// - EXPRESSION: Expressions don't create control flow/scope, we track statements
// - TYPE: Type annotation nodes, not relevant for current analysis
// - LEGACY: Python 2 compatibility, not worth supporting
// - OPERATOR: Operators within expressions, too granular
// - TODO: Potentially useful, to be added when needed
#[rustfmt::skip]
let documented_unused: &[&str] = &[
// STRUCTURAL
"aliased_import", // used internally by extract_imports
"block", // generic block wrapper (duplicate in grammar)
"expression_list", // comma-separated expressions
"identifier", // too common, used everywhere
"import_prefix", // dots in relative imports
"lambda_parameters", // internal to lambda
"module", // root node of file
"parenthesized_expression",// grouping only
"relative_import", // handled in extract_imports
"tuple_expression", // comma-separated values
"wildcard_import", // handled in extract_imports
// CLAUSE (sub-parts of statements)
"case_pattern", // internal to case_clause
"class_pattern", // pattern in match/case
"elif_clause", // part of if_statement
"else_clause", // part of if/for/while/try
"finally_clause", // part of try_statement
"for_in_clause", // internal to comprehensions
"if_clause", // internal to comprehensions
"with_clause", // internal to with_statement
"with_item", // internal to with_statement
// EXPRESSION (don't affect control flow structure)
"await", // await keyword, not a statement
"format_expression", // f-string interpolation
"format_specifier", // f-string format spec
"named_expression", // walrus operator :=
"yield", // yield keyword form
// TYPE (type annotations)
"constrained_type", // type constraints
"generic_type", // parameterized types
"member_type", // attribute access in types
"splat_type", // *args/**kwargs types
"type", // generic type node
"type_alias_statement", // could track as symbol
"type_conversion", // !r/!s/!a in f-strings
"type_parameter", // generic type params
"typed_default_parameter", // param with type and default
"typed_parameter", // param with type annotation
"union_type", // X | Y union syntax
// OPERATOR
"binary_operator", // +, -, *, /, etc.
"boolean_operator", // and/or - handled in complexity_nodes as keywords
"comparison_operator", // ==, <, >, etc.
"not_operator", // not keyword
"unary_operator", // -, +, ~
// LEGACY (Python 2)
"exec_statement", // Python 2 exec
"print_statement", // Python 2 print
// TODO: Potentially useful
"decorated_definition", // wrapper for @decorator
"delete_statement", // del statement
"future_import_statement", // from __future__
"global_statement", // scope modifier
"nonlocal_statement", // scope modifier
"pass_statement", // no-op, detect empty bodies
];
validate_unused_kinds_audit(&Python, documented_unused)
.expect("Python unused node kinds audit failed");
}
}