Repository navigation
Expand file tree
/
Copy pathtest_python_inheritance.py
More file actions
1071 lines (852 loc) · 31.8 KB
/
Copy pathtest_python_inheritance.py
File metadata and controls
1071 lines (852 loc) · 31.8 KB
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
"""Test class inheritance parsing for Python code."""
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
@pytest.fixture
def inheritance_project(tmp_path: Path) -> Path:
"""Create a temporary project with comprehensive inheritance patterns."""
project_path = tmp_path / "inheritance_test"
project_path.mkdir()
# Create __init__.py
(project_path / "__init__.py").write_text("")
# Create inheritance.py with comprehensive inheritance examples
inheritance_file = project_path / "inheritance.py"
inheritance_file.write_text(
'''"""Module with various inheritance patterns."""
from abc import ABC, abstractmethod
from typing import Protocol
# Base classes
class Animal:
"""Base animal class."""
def __init__(self, name: str) -> None:
self.name = name
def speak(self) -> str:
"""Basic speak method to be overridden."""
return "Some generic animal sound"
def eat(self) -> str:
"""Common method."""
return f"{self.name} is eating"
class Mammal(Animal):
"""Mammal inherits from Animal."""
def __init__(self, name: str, fur_color: str) -> None:
super().__init__(name) # super() call!
self.fur_color = fur_color
def speak(self) -> str:
"""Override parent method."""
return f"{self.name} makes mammal sounds"
def give_birth(self) -> str:
"""Mammal-specific method."""
return f"{self.name} gives birth"
class Flyable(Protocol):
"""Protocol for flying capability."""
def fly(self) -> str:
"""Flying method."""
...
class Bird(Animal):
"""Bird inherits from Animal."""
def __init__(self, name: str, wingspan: float) -> None:
super().__init__(name) # Another super() call
self.wingspan = wingspan
def speak(self) -> str:
"""Override parent method."""
return f"{self.name} chirps"
def fly(self) -> str:
"""Bird-specific method."""
return f"{self.name} flies with {self.wingspan}m wingspan"
# Multiple inheritance
class Bat(Mammal, Flyable):
"""Bat inherits from both Mammal and Flyable (multiple inheritance)."""
def __init__(self, name: str, fur_color: str, wing_membrane: str) -> None:
super().__init__(name, fur_color) # Complex super() call
self.wing_membrane = wing_membrane
def speak(self) -> str:
"""Override method from Mammal."""
return f"{self.name} screeches"
def fly(self) -> str:
"""Implement Flyable protocol."""
return f"{self.name} flies using {self.wing_membrane} wings"
def echolocate(self) -> str:
"""Bat-specific method."""
return f"{self.name} uses echolocation"
# Deep inheritance chain
class Dog(Mammal):
"""Dog inherits from Mammal."""
def speak(self) -> str:
"""Override method."""
return f"{self.name} barks"
def fetch(self) -> str:
"""Dog-specific method."""
return f"{self.name} fetches the ball"
class Poodle(Dog):
"""Poodle inherits from Dog (3-level inheritance)."""
def __init__(self, name: str, fur_color: str, cut_style: str) -> None:
super().__init__(name, fur_color) # Deep super() call
self.cut_style = cut_style
def speak(self) -> str:
"""Override method from Dog."""
return f"{self.name} yips elegantly"
def get_groomed(self) -> str:
"""Poodle-specific method."""
return f"{self.name} gets a {self.cut_style} cut"
# Abstract base class
class Vehicle(ABC):
"""Abstract vehicle class."""
def __init__(self, brand: str) -> None:
self.brand = brand
@abstractmethod
def start_engine(self) -> str:
"""Abstract method must be implemented."""
pass
def honk(self) -> str:
"""Concrete method."""
return f"{self.brand} vehicle honks"
class Car(Vehicle):
"""Car inherits from abstract Vehicle."""
def start_engine(self) -> str:
"""Implement abstract method."""
return f"{self.brand} car engine starts"
def drive(self) -> str:
"""Car-specific method."""
return f"Driving the {self.brand} car"
# Method chaining and super calls
class SmartCar(Car):
"""Smart car with advanced features."""
def __init__(self, brand: str, ai_level: int) -> None:
super().__init__(brand) # Call parent constructor
self.ai_level = ai_level
def start_engine(self) -> str:
"""Override with super() call."""
base_start = super().start_engine() # Call parent method
return f"{base_start} with AI level {self.ai_level}"
def autonomous_drive(self) -> str:
"""Smart car specific method."""
return f"{self.brand} drives itself (AI level {self.ai_level})"
def use_inheritance() -> None:
"""Function that demonstrates inheritance usage."""
# Create instances and call methods
dog = Dog("Buddy", "brown")
print(dog.speak()) # Calls Dog.speak()
print(dog.eat()) # Calls inherited Animal.eat()
poodle = Poodle("Fifi", "white", "poodle cut")
print(poodle.speak()) # Calls Poodle.speak() (override)
print(poodle.give_birth()) # Calls inherited Mammal.give_birth()
bat = Bat("Bruce", "black", "leather")
print(bat.fly()) # Calls Bat.fly() (from Flyable)
print(bat.speak()) # Calls Bat.speak() (override)
smart_car = SmartCar("Tesla", 5)
print(smart_car.start_engine()) # Calls SmartCar.start_engine() with super()
'''
)
return project_path
def test_inheritance_relationships_are_created(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that INHERITS relationships are created between child and parent classes."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Expected INHERITS relationships
expected_inherits = [
# Single inheritance
(
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
("Class", "qualified_name", f"{project_name}.inheritance.Animal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Bird"),
("Class", "qualified_name", f"{project_name}.inheritance.Animal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Dog"),
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Poodle"),
("Class", "qualified_name", f"{project_name}.inheritance.Dog"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Car"),
("Class", "qualified_name", f"{project_name}.inheritance.Vehicle"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.SmartCar"),
("Class", "qualified_name", f"{project_name}.inheritance.Car"),
),
# Multiple inheritance (Bat inherits from Mammal and Flyable)
(
("Class", "qualified_name", f"{project_name}.inheritance.Bat"),
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Bat"),
("Class", "qualified_name", f"{project_name}.inheritance.Flyable"),
),
]
# Verify INHERITS relationships are created
relationship_calls = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "INHERITS"
]
for expected_child, expected_parent in expected_inherits:
found = any(
call[0][0] == expected_child and call[0][2] == expected_parent
for call in relationship_calls
)
assert found, (
f"Missing INHERITS relationship: "
f"{expected_child[2]} INHERITS {expected_parent[2]}"
)
def test_super_calls_are_tracked(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that super() calls are tracked as CALLS relationships."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Expected super() calls to track
expected_super_calls = [
# Constructor super() calls
(
f"{project_name}.inheritance.Mammal.__init__",
f"{project_name}.inheritance.Animal.__init__",
),
(
f"{project_name}.inheritance.Bird.__init__",
f"{project_name}.inheritance.Animal.__init__",
),
(
f"{project_name}.inheritance.Bat.__init__",
f"{project_name}.inheritance.Mammal.__init__",
),
(
f"{project_name}.inheritance.Poodle.__init__",
f"{project_name}.inheritance.Mammal.__init__",
),
(
f"{project_name}.inheritance.SmartCar.__init__",
f"{project_name}.inheritance.Vehicle.__init__",
),
# Method super() calls
(
f"{project_name}.inheritance.SmartCar.start_engine",
f"{project_name}.inheritance.Car.start_engine",
),
]
# Get all CALLS relationships
call_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "CALLS"
]
for caller_qn, callee_qn in expected_super_calls:
found = any(
call[0][0][2] == caller_qn and call[0][2][2] == callee_qn
for call in call_relationships
)
assert found, f"Missing super() call: {caller_qn} CALLS {callee_qn}"
def test_method_overrides_are_detected(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that method overrides are properly detected and tracked."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Expected method overrides (child method overrides parent method)
expected_overrides = [
# speak() method overrides
(
f"{project_name}.inheritance.Mammal.speak",
f"{project_name}.inheritance.Animal.speak",
),
(
f"{project_name}.inheritance.Bird.speak",
f"{project_name}.inheritance.Animal.speak",
),
(
f"{project_name}.inheritance.Dog.speak",
f"{project_name}.inheritance.Mammal.speak",
),
(
f"{project_name}.inheritance.Poodle.speak",
f"{project_name}.inheritance.Dog.speak",
),
(
f"{project_name}.inheritance.Bat.speak",
f"{project_name}.inheritance.Mammal.speak",
),
# start_engine() overrides
(
f"{project_name}.inheritance.Car.start_engine",
f"{project_name}.inheritance.Vehicle.start_engine",
),
(
f"{project_name}.inheritance.SmartCar.start_engine",
f"{project_name}.inheritance.Car.start_engine",
),
]
# Check for OVERRIDES relationships
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
for child_method, parent_method in expected_overrides:
found = any(
call[0][0][2] == child_method and call[0][2][2] == parent_method
for call in override_relationships
)
assert found, (
f"Missing OVERRIDES relationship: {child_method} OVERRIDES {parent_method}"
)
def test_multiple_inheritance_is_handled(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that multiple inheritance is properly handled."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Bat should inherit from both Mammal and Flyable
expected_multiple_inheritance = [
(
("Class", "qualified_name", f"{project_name}.inheritance.Bat"),
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Bat"),
("Class", "qualified_name", f"{project_name}.inheritance.Flyable"),
),
]
relationship_calls = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "INHERITS"
]
for expected_child, expected_parent in expected_multiple_inheritance:
found = any(
call[0][0] == expected_child and call[0][2] == expected_parent
for call in relationship_calls
)
assert found, (
f"Missing multiple inheritance: "
f"{expected_child[1]} INHERITS {expected_parent[1]}"
)
def test_inherited_method_calls_are_resolved(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that calls to inherited methods are properly resolved."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Expected calls to inherited methods
expected_inherited_calls = [
# dog.eat() calls Animal.eat() (inherited through Mammal)
(
f"{project_name}.inheritance.use_inheritance",
f"{project_name}.inheritance.Animal.eat",
),
# poodle.give_birth() calls Mammal.give_birth() (inherited)
(
f"{project_name}.inheritance.use_inheritance",
f"{project_name}.inheritance.Mammal.give_birth",
),
]
call_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "CALLS"
]
for caller_qn, callee_qn in expected_inherited_calls:
found = any(
call[0][0][2] == caller_qn and call[0][2][2] == callee_qn
for call in call_relationships
)
assert found, f"Missing inherited method call: {caller_qn} CALLS {callee_qn}"
def test_deep_inheritance_chain(
inheritance_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that deep inheritance chains are properly handled."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=inheritance_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = inheritance_project.name
# Test 3-level inheritance: Animal -> Mammal -> Dog -> Poodle
expected_chain = [
(
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
("Class", "qualified_name", f"{project_name}.inheritance.Animal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Dog"),
("Class", "qualified_name", f"{project_name}.inheritance.Mammal"),
),
(
("Class", "qualified_name", f"{project_name}.inheritance.Poodle"),
("Class", "qualified_name", f"{project_name}.inheritance.Dog"),
),
]
relationship_calls = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "INHERITS"
]
for expected_child, expected_parent in expected_chain:
found = any(
call[0][0] == expected_child and call[0][2] == expected_parent
for call in relationship_calls
)
assert found, (
f"Missing inheritance in chain: "
f"{expected_child[1]} INHERITS {expected_parent[1]}"
)
@pytest.fixture
def mro_diamond_project(tmp_path: Path) -> Path:
"""Create a project with diamond inheritance patterns for MRO testing."""
project_path = tmp_path / "mro_test"
project_path.mkdir()
# Create __init__.py
(project_path / "__init__.py").write_text("")
# Create diamond inheritance test file
diamond_file = project_path / "diamond_mro.py"
diamond_file.write_text(
'''"""Diamond inheritance patterns for MRO testing."""
# Classic Diamond Inheritance Pattern
class A:
"""Base class."""
def method(self) -> str:
"""Base method to be overridden."""
return "A.method"
def base_only(self) -> str:
"""Method only in base class."""
return "A.base_only"
def deep_method(self) -> str:
"""Base deep method for testing deep chains."""
return "A.deep_method"
def asym_method(self) -> str:
"""Base asymmetric method."""
return "A.asym_method"
class B(A):
"""Left branch of diamond."""
def method(self) -> str:
"""Override in left branch."""
return "B.method"
def left_only(self) -> str:
"""Method only in left branch."""
return "B.left_only"
class C(A):
"""Right branch of diamond."""
def method(self) -> str:
"""Override in right branch."""
return "C.method"
def right_only(self) -> str:
"""Method only in right branch."""
return "C.right_only"
class D(B, C):
"""Diamond point - inherits from both B and C."""
def diamond_method(self) -> str:
"""Method unique to diamond point."""
return "D.diamond_method"
# Complex Diamond with Override at Bottom
class E(B, C):
"""Another diamond point that overrides the conflicted method."""
def method(self) -> str:
"""Override the conflicted method at diamond point."""
return "E.method"
# Multiple Diamonds Pattern
class F:
"""Another base class."""
def f_method(self) -> str:
"""Method from F."""
return "F.f_method"
def shared_method(self) -> str:
"""Method that will be overridden."""
return "F.shared_method"
class G(A, F):
"""Mix of two hierarchies."""
def shared_method(self) -> str:
"""Override shared method."""
return "G.shared_method"
class H(B, F):
"""Another mix."""
def shared_method(self) -> str:
"""Override shared method."""
return "H.shared_method"
class I(G, H):
"""Complex multiple inheritance from mixed hierarchies."""
def complex_method(self) -> str:
"""Method unique to complex class."""
return "I.complex_method"
# Deep Diamond Chain
class J(A):
"""Intermediate class in deep chain."""
def deep_method(self) -> str:
"""Method for deep testing."""
return "J.deep_method"
class K(J):
"""Another intermediate class."""
def deep_method(self) -> str:
"""Override deep method."""
return "K.deep_method"
class L(A):
"""Parallel branch."""
def deep_method(self) -> str:
"""Override deep method in parallel branch."""
return "L.deep_method"
class M(K, L):
"""Deep diamond point."""
def final_method(self) -> str:
"""Final method."""
return "M.final_method"
# Asymmetric Diamond (different depth branches)
class N(A):
"""Shallow branch."""
def asym_method(self) -> str:
"""Asymmetric method."""
return "N.asym_method"
class O(B):
"""Deeper branch (A -> B -> O)."""
def asym_method(self) -> str:
"""Override in deeper branch."""
return "O.asym_method"
class P(N, O):
"""Asymmetric diamond - left is 1 level deep, right is 2 levels deep."""
def asym_final(self) -> str:
"""Final asymmetric method."""
return "P.asym_final"
'''
)
return project_path
def test_diamond_inheritance_mro_basic(
mro_diamond_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test basic diamond inheritance MRO - should follow left-to-right, depth-first."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=mro_diamond_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = mro_diamond_project.name
# In diamond D(B, C) with both B and C overriding A.method():
# D.method should resolve to B.method (left-to-right precedence)
# This tests that our BFS finds the FIRST (nearest) override
# Get all OVERRIDES relationships
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
# D doesn't override method, so no D.method -> * relationship should exist
d_method_overrides = [
call
for call in override_relationships
if call[0][0][2] == f"{project_name}.diamond_mro.D.method"
]
# D class doesn't have its own method() implementation, so should be no overrides
assert len(d_method_overrides) == 0, (
f"D class should not have method() overrides, but found: {d_method_overrides}"
)
# But B.method should override A.method and C.method should override A.method
expected_overrides = [
(
f"{project_name}.diamond_mro.B.method",
f"{project_name}.diamond_mro.A.method",
),
(
f"{project_name}.diamond_mro.C.method",
f"{project_name}.diamond_mro.A.method",
),
(
f"{project_name}.diamond_mro.E.method",
f"{project_name}.diamond_mro.B.method",
), # E should override nearest B
]
for child_method, parent_method in expected_overrides:
found = any(
call[0][0][2] == child_method and call[0][2][2] == parent_method
for call in override_relationships
)
assert found, (
f"Missing OVERRIDES relationship: {child_method} OVERRIDES {parent_method}"
)
def test_diamond_inheritance_mro_override_at_point(
mro_diamond_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test diamond where the diamond point overrides the conflicted method."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=mro_diamond_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = mro_diamond_project.name
# E(B, C) overrides method() itself, so E.method should override B.method
# (the nearest method in the hierarchy)
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
# E.method should override the nearest parent method (B.method due to MRO)
e_override_found = any(
(
call[0][0][2] == f"{project_name}.diamond_mro.E.method"
and call[0][2][2] == f"{project_name}.diamond_mro.B.method"
)
for call in override_relationships
)
assert e_override_found, (
"E.method should override B.method (nearest parent in MRO), "
f"but override relationships are: {[(call[0][0][2], call[0][2][2]) for call in override_relationships if 'E.method' in call[0][0][2]]}"
)
def test_complex_multiple_inheritance_mro(
mro_diamond_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test complex multiple inheritance patterns with mixed hierarchies."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=mro_diamond_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = mro_diamond_project.name
# Test complex inheritance I(G, H) where:
# G(A, F) overrides shared_method
# H(B, F) overrides shared_method
# I should not override shared_method, so no I.shared_method -> * relationship
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
# I doesn't override shared_method, so should be no I.shared_method overrides
i_shared_overrides = [
call
for call in override_relationships
if call[0][0][2] == f"{project_name}.diamond_mro.I.shared_method"
]
assert len(i_shared_overrides) == 0, (
f"I class should not have shared_method overrides, but found: {i_shared_overrides}"
)
# But G and H should each override their respective parents
expected_complex_overrides = [
# G(A, F) - shared_method should override F.shared_method (nearest)
(
f"{project_name}.diamond_mro.G.shared_method",
f"{project_name}.diamond_mro.F.shared_method",
),
# H(B, F) - shared_method should override F.shared_method (nearest)
(
f"{project_name}.diamond_mro.H.shared_method",
f"{project_name}.diamond_mro.F.shared_method",
),
]
for child_method, parent_method in expected_complex_overrides:
found = any(
call[0][0][2] == child_method and call[0][2][2] == parent_method
for call in override_relationships
)
assert found, (
f"Missing complex MRO override: {child_method} OVERRIDES {parent_method}"
)
def test_deep_diamond_chain_mro(
mro_diamond_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test deep diamond chains with multiple levels."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=mro_diamond_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = mro_diamond_project.name
# Test M(K, L) where:
# A defines deep_method
# J(A) overrides deep_method
# K(J) overrides deep_method
# L(A) overrides deep_method
# M(K, L) should not override deep_method
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
# Test the deep chain overrides
expected_deep_overrides = [
# J.deep_method should override A.deep_method
(
f"{project_name}.diamond_mro.J.deep_method",
f"{project_name}.diamond_mro.A.deep_method",
),
# K.deep_method should override J.deep_method (nearest parent)
(
f"{project_name}.diamond_mro.K.deep_method",
f"{project_name}.diamond_mro.J.deep_method",
),
# L.deep_method should override A.deep_method
(
f"{project_name}.diamond_mro.L.deep_method",
f"{project_name}.diamond_mro.A.deep_method",
),
]
for child_method, parent_method in expected_deep_overrides:
found = any(
call[0][0][2] == child_method and call[0][2][2] == parent_method
for call in override_relationships
)
assert found, (
f"Missing deep chain override: {child_method} OVERRIDES {parent_method}"
)
# M doesn't override deep_method, so should be no M.deep_method overrides
m_deep_overrides = [
call
for call in override_relationships
if call[0][0][2] == f"{project_name}.diamond_mro.M.deep_method"
]
assert len(m_deep_overrides) == 0, (
f"M class should not have deep_method overrides, but found: {m_deep_overrides}"
)
def test_asymmetric_diamond_mro(
mro_diamond_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test asymmetric diamond inheritance with different branch depths."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=mro_diamond_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = mro_diamond_project.name
# Test P(N, O) asymmetric diamond where:
# A defines asym_method
# N(A) overrides asym_method (1 level deep)
# B(A) overrides method (not asym_method)
# O(B) overrides asym_method (2 levels deep)
# P(N, O) should not override asym_method
override_relationships = [
call
for call in mock_ingestor.ensure_relationship_batch.call_args_list
if len(call[0]) >= 3 and call[0][1] == "OVERRIDES"
]
# Test asymmetric overrides - our BFS should find the nearest regardless of which branch it's in
expected_asym_overrides = [
# N.asym_method should override A.asym_method (direct parent)
(
f"{project_name}.diamond_mro.N.asym_method",
f"{project_name}.diamond_mro.A.asym_method",
),
# O.asym_method should override A.asym_method (skipping B which doesn't have asym_method)
(
f"{project_name}.diamond_mro.O.asym_method",
f"{project_name}.diamond_mro.A.asym_method",
),
]
for child_method, parent_method in expected_asym_overrides:
found = any(
call[0][0][2] == child_method and call[0][2][2] == parent_method