/
er77
/
code-graph-rag-mcp
Обзор
Документация
Войти
/
er77
/
code-graph-rag-mcp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/python-test-files/layer3_relationship_mapping.py
631 строка
22 KB
Evgeniy Rasyuk
release
16 сен 2025, 20:59
16 сен 2025, 20:59
86f187d
Код
Авторство
О чём код?
""" TASK-003B Layer 3: Relationship Mapping Test File This file tests relationship mapping capabilities including: - Class inheritance hierarchy mapping - Method resolution order (MRO) tracking - Import dependency graph construction - Cross-file reference resolution - Method override detection in inheritance chains Test Coverage: - Single and multiple inheritance hierarchies - Method resolution order complexity - Import patterns and dependencies - Cross-module references - Method overriding and super() calls """ from typing import Protocol, TypeVar, Generic, Union, Dict, List, Any, Optional from abc import ABC, abstractmethod import sys import os import importlib from collections.abc import Mapping, Sequence from datetime import datetime, timezone from pathlib import Path import json # Import from other modules (for dependency tracking) try: from .layer1_basic_parsing import AdvancedClass, ConcreteClass from .layer2_advanced_features import MagicMethodsClass, AsyncClass except ImportError: # Fallback for when running standalone pass # ============================================================================= # 1. COMPLEX INHERITANCE HIERARCHIES # ============================================================================= T = TypeVar('T') U = TypeVar('U') class BaseProtocol(Protocol): """Protocol defining base interface.""" def base_method(self) -> str: """Base protocol method.""" ... @property def base_property(self) -> int: """Base protocol property.""" ... class ExtendedProtocol(BaseProtocol, Protocol): """Extended protocol inheriting from BaseProtocol.""" def extended_method(self, value: Any) -> bool: """Extended protocol method.""" ... class AbstractBaseClass(ABC): """Abstract base class for inheritance hierarchy.""" def __init__(self, name: str): self.name = name self._protected_value = 0 self.__private_value = "base_private" @abstractmethod def abstract_method(self) -> str: """Abstract method to be implemented by subclasses.""" pass @abstractmethod def abstract_property(self) -> int: """Abstract property to be implemented by subclasses.""" pass def concrete_method(self) -> str: """Concrete method that can be overridden.""" return f"base_concrete_{self.name}" def template_method(self) -> str: """Template method using abstract methods.""" return f"template_{self.abstract_method()}_{self.abstract_property()}" @classmethod def base_class_method(cls) -> str: """Base class method.""" return f"base_class_{cls.__name__}" class MiddleClass(AbstractBaseClass): """Middle class in inheritance hierarchy.""" def __init__(self, name: str, middle_value: int): super().__init__(name) self.middle_value = middle_value def abstract_method(self) -> str: """Implementation of abstract method.""" return f"middle_abstract_{self.name}" @property def abstract_property(self) -> int: """Implementation of abstract property.""" return self.middle_value def concrete_method(self) -> str: """Override of concrete method.""" base_result = super().concrete_method() return f"middle_override_{base_result}_{self.middle_value}" def middle_specific_method(self) -> str: """Method specific to middle class.""" return f"middle_specific_{self.name}" @classmethod def base_class_method(cls) -> str: """Override of base class method.""" base_result = super().base_class_method() return f"middle_override_{base_result}" class ConcreteImplementation(MiddleClass): """Concrete implementation at bottom of hierarchy.""" def __init__(self, name: str, middle_value: int, concrete_data: str): super().__init__(name, middle_value) self.concrete_data = concrete_data def abstract_method(self) -> str: """Final implementation of abstract method.""" middle_result = super().abstract_method() return f"concrete_{middle_result}_{self.concrete_data}" def concrete_method(self) -> str: """Final override of concrete method.""" middle_result = super().concrete_method() return f"concrete_final_{middle_result}" def concrete_specific_method(self) -> str: """Method specific to concrete class.""" return f"concrete_specific_{self.concrete_data}" # ============================================================================= # 2. MULTIPLE INHERITANCE WITH COMPLEX MRO # ============================================================================= class MixinA: """First mixin with shared method names.""" def shared_method(self) -> str: return "mixin_a_shared" def mixin_a_only(self) -> str: return "mixin_a_only" def cooperative_method(self) -> str: """Method designed for cooperative inheritance.""" return "mixin_a_cooperative" class MixinB: """Second mixin with shared method names.""" def shared_method(self) -> str: return "mixin_b_shared" def mixin_b_only(self) -> str: return "mixin_b_only" def cooperative_method(self) -> str: """Cooperative method that calls super().""" result = super().cooperative_method() if hasattr(super(), 'cooperative_method') else "" return f"mixin_b_cooperative_{result}" class MixinC: """Third mixin for more complex MRO.""" def shared_method(self) -> str: return "mixin_c_shared" def mixin_c_only(self) -> str: return "mixin_c_only" def cooperative_method(self) -> str: """Cooperative method that calls super().""" result = super().cooperative_method() if hasattr(super(), 'cooperative_method') else "" return f"mixin_c_cooperative_{result}" class ComplexMultipleInheritance(MixinC, MixinB, MixinA, ConcreteImplementation): """Class with complex multiple inheritance demonstrating MRO.""" def __init__(self, name: str, middle_value: int, concrete_data: str, multi_data: str): ConcreteImplementation.__init__(self, name, middle_value, concrete_data) self.multi_data = multi_data def shared_method(self) -> str: """Override shared method from multiple mixins.""" # Demonstrate MRO by calling super() - will call MixinC's version mixin_result = super().shared_method() return f"complex_multi_{mixin_result}_{self.multi_data}" def cooperative_method(self) -> str: """Override cooperative method.""" super_result = super().cooperative_method() return f"complex_multi_cooperative_{super_result}" def demonstrate_mro(self) -> Dict[str, Any]: """Method to demonstrate method resolution order.""" mro_info = { "mro_classes": [cls.__name__ for cls in self.__class__.__mro__], "shared_method_result": self.shared_method(), "cooperative_method_result": self.cooperative_method(), "available_methods": { "mixin_a_only": hasattr(self, "mixin_a_only"), "mixin_b_only": hasattr(self, "mixin_b_only"), "mixin_c_only": hasattr(self, "mixin_c_only"), "middle_specific_method": hasattr(self, "middle_specific_method"), "concrete_specific_method": hasattr(self, "concrete_specific_method") } } return mro_info # ============================================================================= # 3. DIAMOND INHERITANCE PATTERN # ============================================================================= class DiamondBase: """Base class for diamond inheritance.""" def __init__(self, value: str): self.value = value print(f"DiamondBase.__init__: {value}") def base_method(self) -> str: return f"diamond_base_{self.value}" class DiamondLeft(DiamondBase): """Left branch of diamond.""" def __init__(self, value: str, left_data: str): super().__init__(value) self.left_data = left_data print(f"DiamondLeft.__init__: {left_data}") def base_method(self) -> str: base_result = super().base_method() return f"diamond_left_{base_result}_{self.left_data}" def left_method(self) -> str: return f"left_specific_{self.left_data}" class DiamondRight(DiamondBase): """Right branch of diamond.""" def __init__(self, value: str, right_data: str): super().__init__(value) self.right_data = right_data print(f"DiamondRight.__init__: {right_data}") def base_method(self) -> str: base_result = super().base_method() return f"diamond_right_{base_result}_{self.right_data}" def right_method(self) -> str: return f"right_specific_{self.right_data}" class DiamondBottom(DiamondLeft, DiamondRight): """Bottom of diamond - inherits from both branches.""" def __init__(self, value: str, left_data: str, right_data: str, bottom_data: str): # Careful initialization to avoid multiple calls to DiamondBase.__init__ DiamondLeft.__init__(self, value, left_data) DiamondRight.__init__(self, value, right_data) # This will call DiamondBase.__init__ again self.bottom_data = bottom_data print(f"DiamondBottom.__init__: {bottom_data}") def base_method(self) -> str: """Override showing how MRO resolves diamond.""" # super() will follow MRO: DiamondLeft -> DiamondRight -> DiamondBase left_result = super().base_method() return f"diamond_bottom_{left_result}_{self.bottom_data}" def combined_method(self) -> str: """Method that uses both branches.""" left = self.left_method() right = self.right_method() return f"combined_{left}_{right}_{self.bottom_data}" # ============================================================================= # 4. CROSS-FILE REFERENCES AND IMPORTS # ============================================================================= class ImportDependentClass: """Class that depends on imports for functionality.""" def __init__(self, config_path: Optional[str] = None): self.config_path = config_path self.config_data: Dict[str, Any] = {} self.dependencies: List[str] = [] # Demonstrate various import patterns self._setup_dependencies() def _setup_dependencies(self) -> None: """Setup method showing various import dependencies.""" # Standard library imports self.dependencies.append("datetime") self.dependencies.append("pathlib") self.dependencies.append("json") # Conditional imports try: import numpy as np self.dependencies.append("numpy") self._numpy_available = True except ImportError: self._numpy_available = False # Dynamic imports try: pandas = importlib.import_module("pandas") self.dependencies.append("pandas") self._pandas_available = True except ImportError: self._pandas_available = False def process_with_datetime(self) -> str: """Method using datetime import.""" now = datetime.now(timezone.utc) return f"processed_at_{now.isoformat()}" def process_with_pathlib(self) -> Optional[Dict[str, Any]]: """Method using pathlib import.""" if not self.config_path: return None path = Path(self.config_path) return { "exists": path.exists(), "is_file": path.is_file(), "suffix": path.suffix, "parent": str(path.parent) } def process_with_json(self, data: Dict[str, Any]) -> str: """Method using json import.""" return json.dumps(data, indent=2) def process_with_numpy(self, data: List[float]) -> Optional[Dict[str, float]]: """Method conditionally using numpy.""" if not self._numpy_available: return None import numpy as np arr = np.array(data) return { "mean": float(np.mean(arr)), "std": float(np.std(arr)), "min": float(np.min(arr)), "max": float(np.max(arr)) } class CircularReferenceA: """Class that potentially creates circular references.""" def __init__(self, name: str): self.name = name self.ref_to_b: Optional['CircularReferenceB'] = None self.shared_data: Dict[str, Any] = {"type": "A", "name": name} def set_reference(self, ref_b: 'CircularReferenceB') -> None: """Set reference to B object.""" self.ref_to_b = ref_b ref_b.ref_to_a = self def method_using_b(self) -> Optional[str]: """Method that uses reference to B.""" if self.ref_to_b: return f"A_{self.name}_uses_B_{self.ref_to_b.get_name()}" return None def get_name(self) -> str: """Get name method.""" return self.name class CircularReferenceB: """Class that potentially creates circular references with A.""" def __init__(self, name: str): self.name = name self.ref_to_a: Optional[CircularReferenceA] = None self.shared_data: Dict[str, Any] = {"type": "B", "name": name} def method_using_a(self) -> Optional[str]: """Method that uses reference to A.""" if self.ref_to_a: return f"B_{self.name}_uses_A_{self.ref_to_a.get_name()}" return None def get_name(self) -> str: """Get name method.""" return self.name def combined_operation(self) -> Optional[Dict[str, Any]]: """Method that combines data from both A and B.""" if self.ref_to_a: return { "a_data": self.ref_to_a.shared_data, "b_data": self.shared_data, "cross_reference": { "a_to_b": self.ref_to_a.method_using_b(), "b_to_a": self.method_using_a() } } return None # ============================================================================= # 5. METHOD OVERRIDE DETECTION AND ANALYSIS # ============================================================================= class BaseForOverrides: """Base class to demonstrate method override patterns.""" def __init__(self, base_value: str): self.base_value = base_value def simple_method(self) -> str: """Simple method to be overridden.""" return f"base_simple_{self.base_value}" def complex_method(self, param1: int, param2: str = "default") -> Dict[str, Any]: """Complex method to be overridden with different signatures.""" return { "source": "base", "param1": param1, "param2": param2, "base_value": self.base_value } def template_hook_method(self) -> str: """Template method with hooks.""" before = self._before_hook() main = self._main_operation() after = self._after_hook() return f"{before}_{main}_{after}" def _before_hook(self) -> str: """Hook method for before operation.""" return "base_before" def _main_operation(self) -> str: """Main operation to be potentially overridden.""" return "base_main" def _after_hook(self) -> str: """Hook method for after operation.""" return "base_after" @classmethod def class_method_to_override(cls) -> str: """Class method to be overridden.""" return f"base_class_{cls.__name__}" @staticmethod def static_method_to_override() -> str: """Static method to be overridden.""" return "base_static" @property def property_to_override(self) -> str: """Property to be overridden.""" return f"base_property_{self.base_value}" class ChildWithOverrides(BaseForOverrides): """Child class demonstrating various override patterns.""" def __init__(self, base_value: str, child_value: int): super().__init__(base_value) self.child_value = child_value def simple_method(self) -> str: """Override simple method - same signature.""" base_result = super().simple_method() return f"child_simple_{base_result}_{self.child_value}" def complex_method(self, param1: int, param2: str = "child_default", param3: bool = False) -> Dict[str, Any]: """Override with extended signature.""" base_result = super().complex_method(param1, param2) base_result.update({ "source": "child", "param3": param3, "child_value": self.child_value, "base_result": base_result.copy() }) return base_result def _main_operation(self) -> str: """Override hook method.""" base_main = super()._main_operation() return f"child_main_{base_main}_{self.child_value}" def _after_hook(self) -> str: """Override after hook.""" return f"child_after_{self.child_value}" @classmethod def class_method_to_override(cls) -> str: """Override class method.""" base_result = super().class_method_to_override() return f"child_class_{base_result}" @staticmethod def static_method_to_override() -> str: """Override static method.""" return "child_static" @property def property_to_override(self) -> str: """Override property.""" base_prop = super().property_to_override return f"child_property_{base_prop}_{self.child_value}" # New methods not in parent def child_specific_method(self) -> str: """Method specific to child class.""" return f"child_specific_{self.child_value}" class GrandChildWithDeepOverrides(ChildWithOverrides): """Grandchild class with deep override chains.""" def __init__(self, base_value: str, child_value: int, grand_value: str): super().__init__(base_value, child_value) self.grand_value = grand_value def simple_method(self) -> str: """Deep override - calls parent which calls grandparent.""" child_result = super().simple_method() return f"grand_simple_{child_result}_{self.grand_value}" def complex_method(self, param1: int, param2: str = "grand_default", param3: bool = True, param4: List[str] = None) -> Dict[str, Any]: """Deep override with even more complex signature.""" if param4 is None: param4 = [] child_result = super().complex_method(param1, param2, param3) child_result.update({ "source": "grandchild", "param4": param4, "grand_value": self.grand_value, "child_result": child_result.copy() }) return child_result def _before_hook(self) -> str: """Override before hook at grandchild level.""" return f"grand_before_{self.grand_value}" def demonstrate_override_chain(self) -> Dict[str, str]: """Method to demonstrate the full override chain.""" return { "simple": self.simple_method(), "template": self.template_hook_method(), "class_method": self.class_method_to_override(), "static_method": self.static_method_to_override(), "property": self.property_to_override } # ============================================================================= # 6. GENERIC CLASS HIERARCHIES # ============================================================================= class GenericBase(Generic[T]): """Generic base class.""" def __init__(self, value: T): self.value = value def get_value(self) -> T: """Get the stored value.""" return self.value def process_value(self, processor: callable) -> Any: """Process the value with a given function.""" return processor(self.value) class GenericChild(GenericBase[T]): """Generic child class maintaining type parameter.""" def __init__(self, value: T, extra: str): super().__init__(value) self.extra = extra def get_combined(self) -> Tuple[T, str]: """Combine value and extra.""" return (self.value, self.extra) class ConcreteGeneric(GenericChild[str]): """Concrete implementation of generic hierarchy.""" def __init__(self, value: str, extra: str, specific: int): super().__init__(value, extra) self.specific = specific def string_operation(self) -> str: """Operation specific to string type.""" return f"{self.value.upper()}_{self.extra}_{self.specific}" if __name__ == "__main__": # Test relationship mapping print("Testing Layer 3 Relationship Mapping:") # Test complex inheritance concrete = ConcreteImplementation("test", 42, "concrete_data") print(f"Template method: {concrete.template_method()}") # Test multiple inheritance MRO multi = ComplexMultipleInheritance("multi", 10, "concrete", "multi_data") mro_info = multi.demonstrate_mro() print(f"MRO info: {mro_info}") # Test diamond inheritance diamond = DiamondBottom("base", "left_data", "right_data", "bottom_data") print(f"Diamond method: {diamond.base_method()}") print(f"Combined method: {diamond.combined_method()}") # Test method overrides grand = GrandChildWithDeepOverrides("base", 5, "grand") override_chain = grand.demonstrate_override_chain() print(f"Override chain: {override_chain}")