From 4055508588abe6336999af12937c1da7dee13175 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Mon, 21 Jul 2025 14:20:07 +0700 Subject: [PATCH] clan-lib: Add object_name to ClassSource and don't override __repr__ from NetworkTechnologyBase instead overwrite it in ClassSource --- pkgs/clan-cli/clan_lib/import_utils/__init__.py | 16 ++++++++++++++++ .../clan_lib/import_utils/import_utils_test.py | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/pkgs/clan-cli/clan_lib/import_utils/__init__.py b/pkgs/clan-cli/clan_lib/import_utils/__init__.py index 8fe60a716..1c7724af7 100644 --- a/pkgs/clan-cli/clan_lib/import_utils/__init__.py +++ b/pkgs/clan-cli/clan_lib/import_utils/__init__.py @@ -12,8 +12,23 @@ T = TypeVar("T") class ClassSource: module_name: str file_path: Path + object_name: str line_number: int | None = None + def vscode_clickable_path(self) -> str: + """Return a VSCode-clickable path for the class source.""" + return ( + f"{self.module_name}.{self.object_name}: {self.file_path}:{self.line_number}" + if self.line_number is not None + else f"{self.module_name}.{self.object_name}: {self.file_path}" + ) + + def __repr__(self) -> str: + return self.vscode_clickable_path() + + def __str__(self) -> str: + return self.vscode_clickable_path() + def import_with_source[T]( module_name: str, @@ -75,6 +90,7 @@ def import_with_source[T]( source = ClassSource( module_name=module_name, file_path=file_path, + object_name=class_name, line_number=line_number, ) diff --git a/pkgs/clan-cli/clan_lib/import_utils/import_utils_test.py b/pkgs/clan-cli/clan_lib/import_utils/import_utils_test.py index e50e84c19..fd34b8c71 100644 --- a/pkgs/clan-cli/clan_lib/import_utils/import_utils_test.py +++ b/pkgs/clan-cli/clan_lib/import_utils/import_utils_test.py @@ -56,16 +56,19 @@ def test_import_with_source(tmp_path: Path) -> None: # Verify source information assert instance.source.module_name == "test_module.test_tech" assert instance.source.file_path.name == "test_tech.py" + assert instance.source.object_name == "NetworkTechnology" assert instance.source.line_number == 4 # Line where class is defined # Test string representations str_repr = str(instance) assert "test_tech.py:" in str_repr + assert "NetworkTechnology" in str_repr assert str(instance.source.line_number) in str_repr repr_repr = repr(instance) assert "NetworkTechnology" in repr_repr assert "test_tech.py:" in repr_repr + assert "test_module.test_tech.NetworkTechnology" in repr_repr finally: # Clean up sys.path @@ -116,6 +119,7 @@ def test_import_with_source_with_args() -> None: # Verify arguments were passed correctly assert instance.extra_arg == "extra_value" # type: ignore[attr-defined] assert instance.keyword_arg == "keyword_value" # type: ignore[attr-defined] + assert instance.source.object_name == "NetworkTechnology" finally: # Clean up