clan-lib: Add object_name to ClassSource and don't override __repr__ from NetworkTechnologyBase instead overwrite it in ClassSource

This commit is contained in:
Qubasa
2025-07-21 14:20:07 +07:00
parent ff65dfc883
commit 4055508588
2 changed files with 20 additions and 0 deletions

View File

@@ -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,
)

View File

@@ -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