tldr-core 0.1.2

Core analysis engine for TLDR code analysis tool
Documentation
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
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
//! Python language handler for call graph analysis.
//!
//! This module provides Python-specific call graph support using tree-sitter-python.
//!
//! # Import Patterns Supported
//!
//! | Pattern | ImportDef |
//! |---------|-----------|
//! | `import os` | `{module: "os", is_from: false}` |
//! | `import os as o` | `{module: "os", alias: "o"}` |
//! | `from os import path` | `{module: "os", is_from: true, names: ["path"]}` |
//! | `from os import path as p` | `{module: "os", names: ["path"], aliases: {"p": "path"}}` |
//! | `from . import types` | `{module: "", is_from: true, names: ["types"], level: 1}` |
//! | `from ..utils import helper` | `{module: "utils", names: ["helper"], level: 2}` |
//! | `from pkg import *` | `{module: "pkg", names: ["*"]}` |
//!
//! # Call Extraction
//!
//! - Direct calls: `func()` -> CallType::Direct or CallType::Intra
//! - Attribute calls: `obj.method()` -> CallType::Attr
//! - Function references: `map(func, ...)` -> CallType::Ref
//!
//! # Spec Reference
//!
//! See `migration/spec/callgraph-spec.md` Section 9.1 for Python-specific details.

use std::collections::{HashMap, HashSet};
use std::path::Path;

use tree_sitter::{Node, Parser, Tree};

use super::base::{get_node_text, walk_tree};
use super::{CallGraphLanguageSupport, ParseError};
use crate::callgraph::cross_file_types::{CallSite, CallType, ClassDef, FuncDef, ImportDef};

// =============================================================================
// Python Handler
// =============================================================================

/// Python language handler using tree-sitter-python.
///
/// Supports:
/// - Import parsing (all Python import styles including relative imports)
/// - Call extraction (direct, method, attribute, references)
/// - TYPE_CHECKING block detection
/// - Nested function tracking via parent_function
/// - `<module>` synthetic function for module-level calls
#[derive(Debug, Default)]
pub struct PythonHandler;

impl PythonHandler {
    /// Creates a new PythonHandler.
    pub fn new() -> Self {
        Self
    }

    /// Parse the source code into a tree-sitter Tree.
    fn parse_source(&self, source: &str) -> Result<Tree, ParseError> {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .map_err(|e| ParseError::ParseFailed {
                file: std::path::PathBuf::new(),
                message: format!("Failed to set Python language: {}", e),
            })?;

        parser
            .parse(source, None)
            .ok_or_else(|| ParseError::ParseFailed {
                file: std::path::PathBuf::new(),
                message: "Parser returned None".to_string(),
            })
    }

    /// Check if a node is inside a TYPE_CHECKING block.
    fn is_in_type_checking_block(&self, node: &Node, source: &[u8]) -> bool {
        let mut current = node.parent();
        while let Some(parent) = current {
            if parent.kind() == "if_statement" {
                // Check if the condition is TYPE_CHECKING
                if let Some(condition) = parent.child_by_field_name("condition") {
                    let cond_text = get_node_text(&condition, source);
                    if cond_text == "TYPE_CHECKING"
                        || cond_text == "typing.TYPE_CHECKING"
                        || cond_text.ends_with(".TYPE_CHECKING")
                    {
                        return true;
                    }
                }
            }
            current = parent.parent();
        }
        false
    }

    /// Parse a single import statement node.
    fn parse_import_statement(&self, node: &Node, source: &[u8]) -> Vec<ImportDef> {
        let mut imports = Vec::new();

        match node.kind() {
            "import_statement" => {
                // import X, import X as Y
                for i in 0..node.named_child_count() {
                    if let Some(child) = node.named_child(i) {
                        match child.kind() {
                            "dotted_name" => {
                                let module = get_node_text(&child, source).to_string();
                                imports.push(ImportDef::simple_import(module));
                            }
                            "aliased_import" => {
                                // import X as Y
                                let mut module = String::new();
                                let mut alias = None;
                                for j in 0..child.named_child_count() {
                                    if let Some(gc) = child.named_child(j) {
                                        match gc.kind() {
                                            "dotted_name" => {
                                                module = get_node_text(&gc, source).to_string();
                                            }
                                            "identifier" => {
                                                alias =
                                                    Some(get_node_text(&gc, source).to_string());
                                            }
                                            _ => {}
                                        }
                                    }
                                }
                                if !module.is_empty() {
                                    let mut imp = ImportDef::simple_import(module);
                                    imp.alias = alias;
                                    imports.push(imp);
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
            "import_from_statement" => {
                // from X import Y, from . import Y, from ..X import Y
                let mut module = String::new();
                let mut level: u8 = 0;
                let mut names = Vec::new();
                let mut aliases: HashMap<String, String> = HashMap::new();
                let mut is_wildcard = false;

                // Handle relative imports
                // tree-sitter-python uses a "relative_import" node containing dots and module
                // e.g., "from . import X" has relative_import="."
                // e.g., "from ..utils import X" has relative_import="..utils"
                for i in 0..node.child_count() {
                    if let Some(child) = node.child(i) {
                        if child.kind() == "relative_import" {
                            let text = get_node_text(&child, source);
                            // Count leading dots
                            for c in text.chars() {
                                if c == '.' {
                                    level += 1;
                                } else {
                                    break;
                                }
                            }
                            // Extract module name (part after dots)
                            let module_part: String =
                                text.chars().skip_while(|&c| c == '.').collect();
                            if !module_part.is_empty() {
                                module = module_part;
                            }
                            break;
                        }
                    }
                }

                // For non-relative imports, get module name from module_name field
                if level == 0 {
                    if let Some(module_node) = node.child_by_field_name("module_name") {
                        module = get_node_text(&module_node, source).to_string();
                    }
                }

                // Parse imported names
                for i in 0..node.named_child_count() {
                    if let Some(child) = node.named_child(i) {
                        match child.kind() {
                            "dotted_name" | "identifier" => {
                                // Skip the module name itself
                                let text = get_node_text(&child, source);
                                if text != module && !text.is_empty() {
                                    names.push(text.to_string());
                                }
                            }
                            "aliased_import" => {
                                // from X import Y as Z
                                let mut orig_name = String::new();
                                let mut alias_name = None;
                                for j in 0..child.named_child_count() {
                                    if let Some(gc) = child.named_child(j) {
                                        match gc.kind() {
                                            "dotted_name" | "identifier" => {
                                                if orig_name.is_empty() {
                                                    orig_name =
                                                        get_node_text(&gc, source).to_string();
                                                } else {
                                                    alias_name = Some(
                                                        get_node_text(&gc, source).to_string(),
                                                    );
                                                }
                                            }
                                            _ => {}
                                        }
                                    }
                                }
                                if !orig_name.is_empty() {
                                    names.push(orig_name.clone());
                                    if let Some(alias) = alias_name {
                                        aliases.insert(alias, orig_name);
                                    }
                                }
                            }
                            "wildcard_import" => {
                                is_wildcard = true;
                            }
                            _ => {}
                        }
                    }
                }

                if is_wildcard {
                    names = vec!["*".to_string()];
                }

                // Create the ImportDef
                let mut imp = if level > 0 {
                    ImportDef::relative_import(module, names, level)
                } else {
                    ImportDef::from_import(module, names)
                };

                if !aliases.is_empty() {
                    imp.aliases = Some(aliases);
                }

                // Check if inside TYPE_CHECKING block
                imp.is_type_checking = self.is_in_type_checking_block(node, source);

                imports.push(imp);
            }
            _ => {}
        }

        imports
    }

    /// Collect all function and class names defined in the file.
    fn collect_definitions(
        &self,
        tree: &Tree,
        source: &[u8],
    ) -> (HashSet<String>, HashSet<String>) {
        let mut functions = HashSet::new();
        let mut classes = HashSet::new();

        for node in walk_tree(tree.root_node()) {
            match node.kind() {
                "function_definition" => {
                    if let Some(name_node) = node.child_by_field_name("name") {
                        functions.insert(get_node_text(&name_node, source).to_string());
                    }
                }
                "class_definition" => {
                    if let Some(name_node) = node.child_by_field_name("name") {
                        classes.insert(get_node_text(&name_node, source).to_string());
                    }
                }
                _ => {}
            }
        }

        (functions, classes)
    }

    /// Extract calls from a function body node.
    fn extract_calls_from_node(
        &self,
        node: &Node,
        source: &[u8],
        defined_funcs: &HashSet<String>,
        defined_classes: &HashSet<String>,
        caller: &str,
        line_offset: u32,
    ) -> Vec<CallSite> {
        let mut calls = Vec::new();
        let mut refs = HashSet::new();

        // Walk the node tree
        for child in walk_tree(*node) {
            match child.kind() {
                "call" => {
                    // Get the function being called
                    if let Some(func_node) = child.child_by_field_name("function") {
                        let line = child.start_position().row as u32 + 1 + line_offset;

                        match func_node.kind() {
                            "identifier" => {
                                // Direct call: func()
                                let target = get_node_text(&func_node, source).to_string();
                                let call_type = if defined_funcs.contains(&target)
                                    || defined_classes.contains(&target)
                                {
                                    CallType::Intra
                                } else {
                                    CallType::Direct
                                };
                                calls.push(CallSite::new(
                                    caller.to_string(),
                                    target,
                                    call_type,
                                    Some(line),
                                    None,
                                    None,
                                    None,
                                ));
                            }
                            "attribute" => {
                                // Attribute call: obj.method()
                                let full_target = get_node_text(&func_node, source).to_string();
                                // Extract receiver (obj) from obj.method
                                let receiver = if let Some(obj_node) =
                                    func_node.child_by_field_name("object")
                                {
                                    Some(get_node_text(&obj_node, source).to_string())
                                } else {
                                    // Fallback: split on first dot
                                    full_target.split('.').next().map(|s| s.to_string())
                                };

                                calls.push(CallSite::new(
                                    caller.to_string(),
                                    full_target,
                                    CallType::Attr,
                                    Some(line),
                                    None,
                                    receiver,
                                    None,
                                ));
                            }
                            _ => {
                                // Other call patterns (subscript, etc.)
                                let target = get_node_text(&func_node, source).to_string();
                                calls.push(CallSite::new(
                                    caller.to_string(),
                                    target,
                                    CallType::Direct,
                                    Some(line),
                                    None,
                                    None,
                                    None,
                                ));
                            }
                        }
                    }
                }
                "identifier" => {
                    // Check for function references (not in calls, but used as values)
                    let name = get_node_text(&child, source);
                    if defined_funcs.contains(name) {
                        // Check if this identifier is NOT the function part of a call
                        if let Some(parent) = child.parent() {
                            if parent.kind() != "call"
                                && parent.child_by_field_name("function").as_ref() != Some(&child)
                            {
                                refs.insert(name.to_string());
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        // Add function references
        for ref_name in refs {
            let line = node.start_position().row as u32 + 1;
            calls.push(CallSite::new(
                caller.to_string(),
                ref_name,
                CallType::Ref,
                Some(line),
                None,
                None,
                None,
            ));
        }

        calls
    }
}

impl CallGraphLanguageSupport for PythonHandler {
    fn name(&self) -> &str {
        "python"
    }

    fn extensions(&self) -> &[&str] {
        &[".py", ".pyi"]
    }

    fn parse_imports(&self, source: &str, _path: &Path) -> Result<Vec<ImportDef>, ParseError> {
        let tree = self.parse_source(source)?;
        let source_bytes = source.as_bytes();
        let mut imports = Vec::new();

        for node in walk_tree(tree.root_node()) {
            match node.kind() {
                "import_statement" | "import_from_statement" => {
                    imports.extend(self.parse_import_statement(&node, source_bytes));
                }
                _ => {}
            }
        }

        Ok(imports)
    }

    fn extract_calls(
        &self,
        _path: &Path,
        source: &str,
        tree: &Tree,
    ) -> Result<HashMap<String, Vec<CallSite>>, ParseError> {
        let source_bytes = source.as_bytes();
        let (defined_funcs, defined_classes) = self.collect_definitions(tree, source_bytes);
        let mut calls_by_func: HashMap<String, Vec<CallSite>> = HashMap::new();

        // Extract calls from each function (includes default params and decorators)
        for node in walk_tree(tree.root_node()) {
            if node.kind() == "function_definition" {
                if let Some(name_node) = node.child_by_field_name("name") {
                    let func_name = get_node_text(&name_node, source_bytes).to_string();

                    // FIX: Determine if this function is a method inside a class
                    // by walking up the parent chain to find the enclosing class.
                    // This ensures calls from ClassA.method and ClassB.method are
                    // recorded separately with qualified caller names.
                    let mut caller_name = func_name.clone();
                    let mut current = node.parent();
                    while let Some(parent) = current {
                        if parent.kind() == "block" {
                            if let Some(gp) = parent.parent() {
                                if gp.kind() == "class_definition" {
                                    if let Some(class_name_node) = gp.child_by_field_name("name") {
                                        let class_name =
                                            get_node_text(&class_name_node, source_bytes);
                                        caller_name = format!("{}.{}", class_name, func_name);
                                    }
                                    break;
                                }
                            }
                        }
                        current = parent.parent();
                    }

                    let mut func_calls = Vec::new();

                    // Pattern 9: Extract calls from decorators
                    // In tree-sitter-python, decorated functions are wrapped in
                    // `decorated_definition` which has `decorator` + `function_definition`
                    // as siblings. The decorator is NOT a child of function_definition.
                    if let Some(parent) = node.parent() {
                        if parent.kind() == "decorated_definition" {
                            for i in 0..parent.child_count() {
                                if let Some(child) = parent.child(i) {
                                    if child.kind() == "decorator" {
                                        // Only extract actual calls from decorators
                                        // @app.route("/api") has a call node inside
                                        // @login_required does NOT (just identifier/attribute)
                                        let decorator_calls = self.extract_calls_from_node(
                                            &child,
                                            source_bytes,
                                            &defined_funcs,
                                            &defined_classes,
                                            &caller_name,
                                            0,
                                        );
                                        func_calls.extend(decorator_calls);
                                    }
                                }
                            }
                        }
                    }

                    // Pattern 6/7: Extract calls from default parameter values
                    if let Some(params_node) = node.child_by_field_name("parameters") {
                        let param_calls = self.extract_calls_from_node(
                            &params_node,
                            source_bytes,
                            &defined_funcs,
                            &defined_classes,
                            &caller_name,
                            0,
                        );
                        func_calls.extend(param_calls);
                    }

                    // Extract calls from the function body (existing behavior)
                    if let Some(body_node) = node.child_by_field_name("body") {
                        let calls = self.extract_calls_from_node(
                            &body_node,
                            source_bytes,
                            &defined_funcs,
                            &defined_classes,
                            &caller_name,
                            0,
                        );
                        func_calls.extend(calls);
                    }

                    if !func_calls.is_empty() {
                        calls_by_func
                            .entry(caller_name)
                            .or_default()
                            .extend(func_calls);
                    }
                }
            }
        }

        // Pattern 3/21: Extract calls from class body field initializers
        for node in walk_tree(tree.root_node()) {
            if node.kind() == "class_definition" {
                if let Some(name_node) = node.child_by_field_name("name") {
                    let class_name = get_node_text(&name_node, source_bytes).to_string();

                    if let Some(body) = node.child_by_field_name("body") {
                        let mut class_calls = Vec::new();
                        // Walk direct children of the class body (block node)
                        // Skip function_definition and class_definition (methods/nested classes)
                        for i in 0..body.named_child_count() {
                            if let Some(child) = body.named_child(i) {
                                if matches!(
                                    child.kind(),
                                    "function_definition"
                                        | "class_definition"
                                        | "decorated_definition"
                                ) {
                                    continue;
                                }
                                // Extract calls from class-level statements
                                // e.g., timeout = compute_timeout(), name = Column(String(50))
                                let calls = self.extract_calls_from_node(
                                    &child,
                                    source_bytes,
                                    &defined_funcs,
                                    &defined_classes,
                                    &class_name,
                                    0,
                                );
                                class_calls.extend(calls);
                            }
                        }
                        if !class_calls.is_empty() {
                            calls_by_func
                                .entry(class_name)
                                .or_default()
                                .extend(class_calls);
                        }
                    }
                }
            }
        }

        // Extract module-level calls into synthetic <module> function
        let mut module_calls = Vec::new();
        for node in tree.root_node().children(&mut tree.root_node().walk()) {
            // Skip function and class definitions
            if matches!(node.kind(), "function_definition" | "class_definition") {
                continue;
            }

            // Extract calls from this module-level statement
            let calls = self.extract_calls_from_node(
                &node,
                source_bytes,
                &defined_funcs,
                &defined_classes,
                "<module>",
                0,
            );
            module_calls.extend(calls);
        }

        if !module_calls.is_empty() {
            calls_by_func.insert("<module>".to_string(), module_calls);
        }

        Ok(calls_by_func)
    }

    fn extract_definitions(
        &self,
        source: &str,
        _path: &Path,
        tree: &Tree,
    ) -> Result<(Vec<FuncDef>, Vec<ClassDef>), super::ParseError> {
        let source_bytes = source.as_bytes();
        let mut funcs = Vec::new();
        let mut classes = Vec::new();

        for node in walk_tree(tree.root_node()) {
            match node.kind() {
                "function_definition" => {
                    if let Some(name_node) = node.child_by_field_name("name") {
                        let name = get_node_text(&name_node, source_bytes).to_string();
                        let line = node.start_position().row as u32 + 1;
                        let end_line = node.end_position().row as u32 + 1;

                        // Check if inside a class
                        let mut class_name = None;
                        let mut parent = node.parent();
                        while let Some(p) = parent {
                            if p.kind() == "block" {
                                if let Some(gp) = p.parent() {
                                    if gp.kind() == "class_definition" {
                                        if let Some(cn) = gp.child_by_field_name("name") {
                                            class_name =
                                                Some(get_node_text(&cn, source_bytes).to_string());
                                        }
                                    }
                                }
                                break;
                            }
                            parent = p.parent();
                        }

                        if let Some(cn) = class_name {
                            funcs.push(FuncDef::method(name, cn, line, end_line));
                        } else {
                            funcs.push(FuncDef::function(name, line, end_line));
                        }
                    }
                }
                "class_definition" => {
                    if let Some(name_node) = node.child_by_field_name("name") {
                        let class_name = get_node_text(&name_node, source_bytes).to_string();
                        let line = node.start_position().row as u32 + 1;
                        let end_line = node.end_position().row as u32 + 1;

                        // Collect base classes from argument_list
                        let mut bases = Vec::new();
                        if let Some(args) = node.child_by_field_name("superclasses") {
                            for i in 0..args.child_count() {
                                if let Some(arg) = args.child(i) {
                                    if arg.kind() == "identifier" {
                                        bases.push(get_node_text(&arg, source_bytes).to_string());
                                    }
                                }
                            }
                        }

                        // Collect method names from the body
                        let mut methods = Vec::new();
                        if let Some(body) = node.child_by_field_name("body") {
                            for i in 0..body.named_child_count() {
                                if let Some(child) = body.named_child(i) {
                                    if child.kind() == "function_definition" {
                                        if let Some(fn_name) = child.child_by_field_name("name") {
                                            methods.push(
                                                get_node_text(&fn_name, source_bytes).to_string(),
                                            );
                                        }
                                    }
                                }
                            }
                        }

                        classes.push(ClassDef::new(class_name, line, end_line, methods, bases));
                    }
                }
                _ => {}
            }
        }

        Ok((funcs, classes))
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_imports(source: &str) -> Vec<ImportDef> {
        let handler = PythonHandler::new();
        handler.parse_imports(source, Path::new("test.py")).unwrap()
    }

    fn extract_calls(source: &str) -> HashMap<String, Vec<CallSite>> {
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap()
    }

    // -------------------------------------------------------------------------
    // Import Parsing Tests
    // -------------------------------------------------------------------------

    mod import_tests {
        use super::*;

        #[test]
        fn test_parse_import_simple() {
            let imports = parse_imports("import os");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].module, "os");
            assert!(!imports[0].is_from);
            assert!(imports[0].names.is_empty());
        }

        #[test]
        fn test_parse_import_dotted() {
            let imports = parse_imports("import os.path");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].module, "os.path");
        }

        #[test]
        fn test_parse_import_as() {
            let imports = parse_imports("import numpy as np");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].module, "numpy");
            assert_eq!(imports[0].alias, Some("np".to_string()));
        }

        #[test]
        fn test_parse_from_import() {
            let imports = parse_imports("from os import path");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].module, "os");
            assert!(imports[0].is_from);
            assert_eq!(imports[0].names, vec!["path"]);
        }

        #[test]
        fn test_parse_from_import_multiple() {
            let imports = parse_imports("from os import path, getcwd");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].module, "os");
            assert_eq!(imports[0].names.len(), 2);
            assert!(imports[0].names.contains(&"path".to_string()));
            assert!(imports[0].names.contains(&"getcwd".to_string()));
        }

        #[test]
        fn test_parse_from_import_as() {
            let imports = parse_imports("from os import path as p");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].names, vec!["path"]);
            let aliases = imports[0].aliases.as_ref().unwrap();
            assert_eq!(aliases.get("p"), Some(&"path".to_string()));
        }

        #[test]
        fn test_parse_relative_import_level1() {
            let imports = parse_imports("from . import types");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].level, 1);
            assert!(imports[0].is_relative());
            assert_eq!(imports[0].names, vec!["types"]);
        }

        #[test]
        fn test_parse_relative_import_level2() {
            let imports = parse_imports("from ..utils import helper");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].level, 2);
            assert_eq!(imports[0].module, "utils");
            assert_eq!(imports[0].names, vec!["helper"]);
        }

        #[test]
        fn test_parse_relative_import_level1_with_module() {
            let imports = parse_imports("from .core import MyClass");
            assert_eq!(imports.len(), 1);
            assert_eq!(imports[0].level, 1);
            assert_eq!(imports[0].module, "core");
            assert_eq!(imports[0].names, vec!["MyClass"]);
        }

        #[test]
        fn test_parse_wildcard_import() {
            let imports = parse_imports("from pkg import *");
            assert_eq!(imports.len(), 1);
            assert!(imports[0].is_wildcard());
            assert_eq!(imports[0].names, vec!["*"]);
        }

        #[test]
        fn test_parse_type_checking_import() {
            let source = r#"
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mymodule import MyClass
"#;
            let imports = parse_imports(source);
            // Find the MyClass import
            let myclass_import = imports
                .iter()
                .find(|i| i.names.contains(&"MyClass".to_string()));
            assert!(myclass_import.is_some());
            assert!(myclass_import.unwrap().is_type_checking);
        }

        #[test]
        fn test_parse_multiple_imports() {
            let source = r#"
import os
import sys
from pathlib import Path
from collections import defaultdict, Counter
"#;
            let imports = parse_imports(source);
            assert_eq!(imports.len(), 4);
        }
    }

    // -------------------------------------------------------------------------
    // Call Extraction Tests
    // -------------------------------------------------------------------------

    mod call_tests {
        use super::*;

        #[test]
        fn test_extract_calls_direct() {
            let source = r#"
def main():
    print("hello")
    some_external_func()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();
            assert!(main_calls.iter().any(|c| c.target == "print"));
            assert!(main_calls.iter().any(|c| c.target == "some_external_func"));
        }

        #[test]
        fn test_extract_calls_intra_file() {
            let source = r#"
def helper():
    pass

def main():
    helper()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();
            let helper_call = main_calls.iter().find(|c| c.target == "helper").unwrap();
            assert_eq!(helper_call.call_type, CallType::Intra);
        }

        #[test]
        fn test_extract_calls_method() {
            let source = r#"
def main():
    obj.method()
    self.internal()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();

            let obj_method = main_calls
                .iter()
                .find(|c| c.target == "obj.method")
                .unwrap();
            assert_eq!(obj_method.call_type, CallType::Attr);
            assert_eq!(obj_method.receiver, Some("obj".to_string()));

            let self_method = main_calls
                .iter()
                .find(|c| c.target == "self.internal")
                .unwrap();
            assert_eq!(self_method.call_type, CallType::Attr);
            assert_eq!(self_method.receiver, Some("self".to_string()));
        }

        #[test]
        fn test_extract_calls_chained() {
            let source = r#"
def main():
    a.b.c.d()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();
            assert!(main_calls.iter().any(|c| c.target == "a.b.c.d"));
        }

        #[test]
        fn test_extract_calls_class_instantiation() {
            let source = r#"
class MyClass:
    pass

def main():
    obj = MyClass()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();
            let instantiation = main_calls.iter().find(|c| c.target == "MyClass").unwrap();
            assert_eq!(instantiation.call_type, CallType::Intra);
        }

        #[test]
        fn test_extract_calls_module_level() {
            let source = r#"
def helper():
    pass

# Module-level call
result = helper()
"#;
            let calls = extract_calls(source);
            assert!(calls.contains_key("<module>"));
            let module_calls = calls.get("<module>").unwrap();
            assert!(module_calls.iter().any(|c| c.target == "helper"));
        }

        #[test]
        fn test_extract_calls_with_line_numbers() {
            let source = r#"def main():
    first_call()
    second_call()
"#;
            let calls = extract_calls(source);
            let main_calls = calls.get("main").unwrap();

            let first = main_calls
                .iter()
                .find(|c| c.target == "first_call")
                .unwrap();
            let second = main_calls
                .iter()
                .find(|c| c.target == "second_call")
                .unwrap();

            // Line numbers should be 1-indexed and different
            assert!(first.line.is_some());
            assert!(second.line.is_some());
            assert!(second.line.unwrap() > first.line.unwrap());
        }
    }

    // -------------------------------------------------------------------------
    // New Pattern Tests: Default Params, Decorators, Class Body Calls
    // -------------------------------------------------------------------------

    mod new_pattern_tests {
        use super::*;

        // --- Pattern 6/7: Default parameter calls ---

        #[test]
        fn test_default_param_direct_call() {
            let source = r#"
def greet(default_name=get_default()):
    pass
"#;
            let calls = extract_calls(source);
            let greet_calls = calls.get("greet").expect("greet should have calls");
            assert!(
                greet_calls.iter().any(|c| c.target == "get_default"),
                "get_default() in default param should be extracted. Got: {:?}",
                greet_calls
            );
        }

        #[test]
        fn test_default_param_method_call() {
            let _source = r#"
def greet(formatter=str.upper):
    pass
"#;
            // str.upper is NOT a call (no parens), so no calls expected from default params
            // But if it were str.upper(), it would be a call. Let's test the actual call case:
            let source_with_call = r#"
def greet(formatter=str.upper()):
    pass
"#;
            let calls = extract_calls(source_with_call);
            let greet_calls = calls.get("greet").expect("greet should have calls");
            assert!(
                greet_calls.iter().any(|c| c.target == "str.upper"),
                "str.upper() in default param should be extracted. Got: {:?}",
                greet_calls
            );
        }

        #[test]
        fn test_default_param_in_class_method() {
            let source = r#"
class Service:
    def __init__(self, db=connect_db(), timeout=compute_timeout()):
        pass
"#;
            let calls = extract_calls(source);
            // After fix, caller name is qualified with class name
            let init_calls = calls
                .get("Service.__init__")
                .expect("Service.__init__ should have calls");
            assert!(
                init_calls.iter().any(|c| c.target == "connect_db"),
                "connect_db() in default param should be extracted. Got: {:?}",
                init_calls
            );
            assert!(
                init_calls.iter().any(|c| c.target == "compute_timeout"),
                "compute_timeout() in default param should be extracted. Got: {:?}",
                init_calls
            );
        }

        #[test]
        fn test_default_param_multiple() {
            let source = r#"
def configure(a=make_a(), b=make_b(), c="static"):
    pass
"#;
            let calls = extract_calls(source);
            let conf_calls = calls.get("configure").expect("configure should have calls");
            assert!(
                conf_calls.iter().any(|c| c.target == "make_a"),
                "make_a() should be extracted. Got: {:?}",
                conf_calls
            );
            assert!(
                conf_calls.iter().any(|c| c.target == "make_b"),
                "make_b() should be extracted. Got: {:?}",
                conf_calls
            );
            // "static" is not a call
            assert!(
                !conf_calls.iter().any(|c| c.target == "static"),
                "static string should not be a call"
            );
        }

        // --- Pattern 9: Decorator calls ---

        #[test]
        fn test_decorator_call_with_args() {
            let source = r#"
@app.route("/api")
def handle_api():
    pass
"#;
            let calls = extract_calls(source);
            let api_calls = calls
                .get("handle_api")
                .expect("handle_api should have calls");
            assert!(
                api_calls.iter().any(|c| c.target == "app.route"),
                "app.route(\"/api\") decorator should be extracted. Got: {:?}",
                api_calls
            );
        }

        #[test]
        fn test_decorator_without_args_not_call() {
            let source = r#"
@login_required
def dashboard():
    pass
"#;
            let calls = extract_calls(source);
            // @login_required without () is NOT a call, just a reference
            // dashboard should have no calls (or only non-decorator calls)
            let dashboard_calls = calls.get("dashboard");
            if let Some(dc) = dashboard_calls {
                assert!(
                    !dc.iter().any(|c| c.target == "login_required"),
                    "login_required without () should NOT be extracted as a call. Got: {:?}",
                    dc
                );
            }
        }

        #[test]
        fn test_decorator_nested_call() {
            let source = r#"
@pytest.mark.parametrize("x", [1, 2])
def test_something():
    pass
"#;
            let calls = extract_calls(source);
            let test_calls = calls
                .get("test_something")
                .expect("test_something should have calls");
            assert!(
                test_calls
                    .iter()
                    .any(|c| c.target == "pytest.mark.parametrize"),
                "pytest.mark.parametrize() decorator should be extracted. Got: {:?}",
                test_calls
            );
        }

        #[test]
        fn test_decorator_simple_call() {
            let source = r#"
@my_decorator()
def my_func():
    pass
"#;
            let calls = extract_calls(source);
            let func_calls = calls.get("my_func").expect("my_func should have calls");
            assert!(
                func_calls.iter().any(|c| c.target == "my_decorator"),
                "my_decorator() decorator should be extracted. Got: {:?}",
                func_calls
            );
        }

        // --- Pattern 3/21: Class body field initializer calls ---

        #[test]
        fn test_class_body_field_call() {
            let source = r#"
class Config:
    timeout = compute_timeout()
    handler = create_handler()
    CONSTANT = "no call here"
"#;
            let calls = extract_calls(source);
            let config_calls = calls.get("Config").expect("Config should have calls");
            assert!(
                config_calls.iter().any(|c| c.target == "compute_timeout"),
                "compute_timeout() in class body should be extracted. Got: {:?}",
                config_calls
            );
            assert!(
                config_calls.iter().any(|c| c.target == "create_handler"),
                "create_handler() in class body should be extracted. Got: {:?}",
                config_calls
            );
        }

        #[test]
        fn test_class_body_dsl_nested_calls() {
            let source = r#"
class User(Base):
    name = Column(String(50))
    age = Column(Integer())
"#;
            let calls = extract_calls(source);
            let user_calls = calls.get("User").expect("User should have calls");
            assert!(
                user_calls.iter().any(|c| c.target == "Column"),
                "Column() in class body should be extracted. Got: {:?}",
                user_calls
            );
            assert!(
                user_calls.iter().any(|c| c.target == "String"),
                "String() in class body should be extracted. Got: {:?}",
                user_calls
            );
            assert!(
                user_calls.iter().any(|c| c.target == "Integer"),
                "Integer() in class body should be extracted. Got: {:?}",
                user_calls
            );
        }

        #[test]
        fn test_class_body_no_false_positives() {
            let source = r#"
class Config:
    NAME = "static"
    VALUE = 42
    ITEMS = [1, 2, 3]
"#;
            let calls = extract_calls(source);
            // No calls in class body (all static assignments)
            assert!(
                !calls.contains_key("Config"),
                "Config should have no calls for static assignments. Got: {:?}",
                calls.get("Config")
            );
        }

        #[test]
        fn test_class_body_method_call() {
            let source = r#"
class MyModel(Model):
    objects = Manager()
    db = get_connection()
"#;
            let calls = extract_calls(source);
            let model_calls = calls.get("MyModel").expect("MyModel should have calls");
            assert!(
                model_calls.iter().any(|c| c.target == "Manager"),
                "Manager() should be extracted. Got: {:?}",
                model_calls
            );
            assert!(
                model_calls.iter().any(|c| c.target == "get_connection"),
                "get_connection() should be extracted. Got: {:?}",
                model_calls
            );
        }
    }

    // -------------------------------------------------------------------------
    // Handler Trait Tests
    // -------------------------------------------------------------------------

    mod trait_tests {
        use super::*;

        #[test]
        fn test_handler_name() {
            let handler = PythonHandler::new();
            assert_eq!(handler.name(), "python");
        }

        #[test]
        fn test_handler_extensions() {
            let handler = PythonHandler::new();
            let exts = handler.extensions();
            assert!(exts.contains(&".py"));
            assert!(exts.contains(&".pyi"));
        }

        #[test]
        fn test_handler_supports() {
            let handler = PythonHandler::new();
            assert!(handler.supports("python"));
            assert!(handler.supports("Python"));
            assert!(handler.supports("PYTHON"));
            assert!(!handler.supports("javascript"));
        }

        #[test]
        fn test_handler_supports_extension() {
            let handler = PythonHandler::new();
            assert!(handler.supports_extension(".py"));
            assert!(handler.supports_extension(".pyi"));
            assert!(handler.supports_extension(".PY"));
            assert!(!handler.supports_extension(".js"));
        }
    }

    // Debug test for cross-scope method extraction
    #[test]
    fn test_extract_group_shell_complete() {
        // Simplified version of the click/core.py Group.shell_complete structure
        let source = r#"
class Command:
    def shell_complete(self, ctx, incomplete):
        return []

class Group(Command):
    def get_command(self, ctx, cmd_name):
        pass
    
    def shell_complete(self, ctx, incomplete):
        results = []
        results.extend(super().shell_complete(ctx, incomplete))
        return results
"#;
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        let (funcs, _classes) = handler
            .extract_definitions(source, Path::new("test.py"), &tree)
            .unwrap();

        // Check that both shell_complete methods are found
        let command_shell_complete = funcs.iter().find(|f| {
            f.name == "shell_complete" && f.class_name.as_ref().is_some_and(|n| n == "Command")
        });
        let group_shell_complete = funcs.iter().find(|f| {
            f.name == "shell_complete" && f.class_name.as_ref().is_some_and(|n| n == "Group")
        });

        assert!(
            command_shell_complete.is_some(),
            "Command.shell_complete should be found"
        );
        assert!(
            group_shell_complete.is_some(),
            "Group.shell_complete should be found"
        );

        // Now check extract_calls
        let calls = handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap();

        // Group.shell_complete should have calls
        // After fix, caller name is qualified with class name
        let group_method_calls = calls.get("Group.shell_complete");
        println!("Calls from Group.shell_complete: {:?}", group_method_calls);

        // It should call super().shell_complete()
        assert!(
            group_method_calls.is_some(),
            "Group.shell_complete should have calls recorded"
        );
    }

    // Test for the actual bug: generator expression calls
    #[test]
    fn test_extract_calls_in_generator_expression() {
        let source = r#"
def _complete_visible_commands(ctx, incomplete):
    return []

class Command:
    def shell_complete(self, ctx, incomplete):
        results = []
        results.extend(
            (name, cmd)
            for name, cmd in _complete_visible_commands(ctx, incomplete)
            if name
        )
        return results
"#;
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        let calls = handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap();

        println!("All calls: {:?}", calls);

        // shell_complete should have a call to _complete_visible_commands
        // After fix, caller name is qualified with class name
        let shell_calls = calls
            .get("Command.shell_complete")
            .expect("Command.shell_complete should have calls");
        let helper_call = shell_calls
            .iter()
            .find(|c| c.target == "_complete_visible_commands");

        assert!(
            helper_call.is_some(),
            "Should find call to _complete_visible_commands in generator expression. Got: {:?}",
            shell_calls
        );

        // And it should be Intra since it's defined in the same file
        let call = helper_call.unwrap();
        assert_eq!(
            call.call_type,
            CallType::Intra,
            "Call to same-file function should be Intra"
        );
    }

    // Test with the actual click/core.py file structure
    #[test]
    #[ignore] // Requires external fixture: /tmp/purity-realworld/python-click/src/click/core.py
    fn test_extract_from_real_click_core() {
        use std::fs;

        let source =
            fs::read_to_string("/tmp/purity-realworld/python-click/src/click/core.py").unwrap();
        let handler = PythonHandler::new();
        let tree = handler.parse_source(&source).unwrap();

        let (funcs, _classes) = handler
            .extract_definitions(&source, Path::new("core.py"), &tree)
            .unwrap();

        // Check that _complete_visible_commands is found
        let helper_func = funcs
            .iter()
            .find(|f| f.name == "_complete_visible_commands");
        println!(
            "_complete_visible_commands found: {:?}",
            helper_func.is_some()
        );

        // Check that Command.shell_complete and Group.shell_complete are found
        let command_methods: Vec<_> = funcs
            .iter()
            .filter(|f| {
                f.name == "shell_complete"
                    && f.class_name.as_ref().is_some_and(|n| n == "Command")
            })
            .collect();
        let group_methods: Vec<_> = funcs
            .iter()
            .filter(|f| {
                f.name == "shell_complete" && f.class_name.as_ref().is_some_and(|n| n == "Group")
            })
            .collect();

        println!(
            "Command.shell_complete found: {} times",
            command_methods.len()
        );
        println!("Group.shell_complete found: {} times", group_methods.len());

        for m in &command_methods {
            println!("  Command.shell_complete at line {}-{}", m.line, m.end_line);
        }
        for m in &group_methods {
            println!("  Group.shell_complete at line {}-{}", m.line, m.end_line);
        }

        // Now check calls
        let calls = handler
            .extract_calls(Path::new("core.py"), &source, &tree)
            .unwrap();

        // Find calls to _complete_visible_commands
        for (caller, call_sites) in &calls {
            for call in call_sites {
                if call.target == "_complete_visible_commands" {
                    println!(
                        "Found call from '{}' to _complete_visible_commands (type: {:?})",
                        caller, call.call_type
                    );
                }
            }
        }

        // The bug: Group.shell_complete is NOT in the nodes, but Command.shell_complete is
        // Let's verify this
        let group_shell_complete_node = funcs.iter().find(|f| {
            f.name == "shell_complete" && f.class_name.as_ref().is_some_and(|n| n == "Group")
        });
        let command_shell_complete_node = funcs.iter().find(|f| {
            f.name == "shell_complete" && f.class_name.as_ref().is_some_and(|n| n == "Command")
        });

        println!("\nIn extract_definitions:");
        println!(
            "  Group.shell_complete: {:?}",
            group_shell_complete_node.is_some()
        );
        println!(
            "  Command.shell_complete: {:?}",
            command_shell_complete_node.is_some()
        );

        // These should both be Some
        assert!(
            group_shell_complete_node.is_some(),
            "Group.shell_complete should be found by extract_definitions"
        );
        assert!(
            command_shell_complete_node.is_some(),
            "Command.shell_complete should be found by extract_definitions"
        );
    }

    // Test for the actual bug: cross-scope intra-file calls from methods to top-level functions
    #[test]
    fn test_extract_calls_method_to_toplevel() {
        let source = r#"
def helper_func():
    pass

class MyClass:
    def method(self):
        helper_func()
"#;
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        let calls = handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap();

        // The method should have a call to helper_func marked as Intra
        // After the fix, the caller name is qualified as "MyClass.method"
        let method_calls = calls
            .get("MyClass.method")
            .expect("MyClass.method should have calls");
        let helper_call = method_calls.iter().find(|c| c.target == "helper_func");

        assert!(
            helper_call.is_some(),
            "Should find call from method to top-level helper_func. Got: {:?}",
            method_calls
        );

        let call = helper_call.unwrap();
        assert_eq!(
            call.call_type,
            CallType::Intra,
            "Call to same-file top-level function should be Intra"
        );
    }

    // Test that demonstrates the fix: multiple methods with same name in different classes
    // should now have their calls recorded separately with qualified caller names
    #[test]
    fn test_multiple_methods_same_name() {
        let source = r#"
def _helper():
    pass

class Command:
    def shell_complete(self):
        _helper()

class Group(Command):
    def shell_complete(self):
        _helper()
"#;
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        let calls = handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap();

        println!("Calls: {:?}", calls);

        // After the fix, calls should be recorded with qualified names
        // Command.shell_complete and Group.shell_complete are separate entries
        let command_calls = calls.get("Command.shell_complete");
        let group_calls = calls.get("Group.shell_complete");

        assert!(
            command_calls.is_some(),
            "Should have calls from Command.shell_complete"
        );
        assert!(
            group_calls.is_some(),
            "Should have calls from Group.shell_complete"
        );

        // Each should have 1 call to _helper
        let cmd_helper_calls: Vec<_> = command_calls
            .unwrap()
            .iter()
            .filter(|c| c.target == "_helper")
            .collect();
        let group_helper_calls: Vec<_> = group_calls
            .unwrap()
            .iter()
            .filter(|c| c.target == "_helper")
            .collect();

        assert_eq!(
            cmd_helper_calls.len(),
            1,
            "Command.shell_complete should have 1 call to _helper"
        );
        assert_eq!(
            group_helper_calls.len(),
            1,
            "Group.shell_complete should have 1 call to _helper"
        );

        // The old unqualified name should NOT exist (or be empty)
        let old_shell_calls = calls.get("shell_complete");
        assert!(
            old_shell_calls.is_none() || old_shell_calls.unwrap().is_empty(),
            "Unqualified 'shell_complete' should not have calls after fix"
        );
    }

    // Test the exact scenario from the bug report: two classes with same method name
    #[test]
    fn test_two_classes_same_method_name() {
        let source = r#"
def helper():
    pass

class A:
    def method(self):
        helper()  # Line 7

class B:
    def method(self):
        helper()  # Line 11
"#;
        let handler = PythonHandler::new();
        let tree = handler.parse_source(source).unwrap();
        let calls = handler
            .extract_calls(Path::new("test.py"), source, &tree)
            .unwrap();

        println!("Calls: {:?}", calls);

        // Both A.method and B.method should be separate entries
        let a_method_calls = calls.get("A.method");
        let b_method_calls = calls.get("B.method");

        assert!(a_method_calls.is_some(), "Should have calls from A.method");
        assert!(b_method_calls.is_some(), "Should have calls from B.method");

        // Each should have 1 call to helper
        let a_helper: Vec<_> = a_method_calls
            .unwrap()
            .iter()
            .filter(|c| c.target == "helper")
            .collect();
        let b_helper: Vec<_> = b_method_calls
            .unwrap()
            .iter()
            .filter(|c| c.target == "helper")
            .collect();

        assert_eq!(a_helper.len(), 1, "A.method should have 1 call to helper");
        assert_eq!(b_helper.len(), 1, "B.method should have 1 call to helper");

        // Both should be Intra calls
        assert_eq!(a_helper[0].call_type, CallType::Intra);
        assert_eq!(b_helper[0].call_type, CallType::Intra);
    }

    // Test with the actual click/core.py to verify extraction works
    #[test]
    #[ignore] // Requires external fixture: /tmp/purity-realworld/python-click/src/click/core.py
    fn test_click_core_extraction() {
        use std::fs;

        let source =
            fs::read_to_string("/tmp/purity-realworld/python-click/src/click/core.py").unwrap();
        let handler = PythonHandler::new();
        let tree = handler.parse_source(&source).unwrap();
        let calls = handler
            .extract_calls(Path::new("core.py"), &source, &tree)
            .unwrap();

        // Count calls to _complete_visible_commands
        let mut total_calls = 0;
        let mut callers = Vec::new();
        for (caller, call_sites) in &calls {
            for call in call_sites {
                if call.target == "_complete_visible_commands" {
                    total_calls += 1;
                    callers.push(caller.clone());
                }
            }
        }

        println!("Total calls to _complete_visible_commands: {}", total_calls);
        println!("Callers: {:?}", callers);

        // Should have at least 2 calls (from Command.shell_complete and Group.shell_complete)
        assert!(
            total_calls >= 2,
            "Should have at least 2 calls to _complete_visible_commands, found {}",
            total_calls
        );

        // Should have Command.shell_complete and Group.shell_complete as callers
        assert!(
            callers.iter().any(|c| c.contains("Command.shell_complete")),
            "Should have Command.shell_complete as caller"
        );
        assert!(
            callers.iter().any(|c| c.contains("Group.shell_complete")),
            "Should have Group.shell_complete as caller"
        );
    }
}