Skip to content

Commit 1e391b8

Browse files
committed
perf: Compute costly message lazily in alias resolution errors
1 parent 03cae74 commit 1e391b8

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎packages/griffelib/src/griffe/_internal/exceptions.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ def __init__(self, alias: Alias) -> None:
5656
self.alias: Alias = alias
5757
"""The alias that triggered the error."""
5858

59-
message = f"Could not resolve alias {alias.path} pointing at {alias.target_path}"
59+
self._detailed_message: str | None = None
60+
super().__init__(f"Could not resolve alias {alias.path} pointing at {self.alias.target_path}")
61+
62+
def _format_message(self) -> str:
63+
message = super().__str__()
6064
try:
61-
filepath = alias.parent.relative_filepath # ty:ignore[unresolved-attribute]
65+
filepath = self.alias.parent.relative_filepath # ty:ignore[unresolved-attribute]
6266
except BuiltinModuleError:
63-
pass
64-
else:
65-
message += f" (in {filepath}:{alias.alias_lineno})"
66-
super().__init__(message)
67+
return message
68+
return message + f" (in {filepath}:{self.alias.alias_lineno})"
69+
70+
def __str__(self) -> str:
71+
if self._detailed_message is None:
72+
self._detailed_message = self._format_message()
73+
return self._detailed_message
6774

6875

6976
class CyclicAliasError(GriffeError):

‎packages/griffelib/tests/test_loader.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import pytest
2626

2727
from griffe import (
28+
AliasResolutionError,
2829
ExprName,
2930
GriffeLoader,
3031
temporary_inspected_package,
@@ -33,6 +34,22 @@
3334
temporary_visited_package,
3435
)
3536

37+
38+
def test_resolve_aliases_defers_caught_error_messages(monkeypatch: pytest.MonkeyPatch) -> None:
39+
"""Do not format alias errors that the resolver catches internally."""
40+
with temporary_pypackage("package", {"__init__.py": "from missing import value"}) as package:
41+
loader = GriffeLoader(search_paths=[package.tmpdir])
42+
loader.load("package")
43+
44+
def fail_if_formatted(_error: AliasResolutionError) -> str:
45+
raise AssertionError("caught alias-resolution error was formatted")
46+
47+
monkeypatch.setattr(AliasResolutionError, "_format_message", fail_if_formatted)
48+
unresolved, _ = loader.resolve_aliases(implicit=True, external=False)
49+
50+
assert unresolved == {"package.value"}
51+
52+
3653
if TYPE_CHECKING:
3754
from pathlib import Path
3855

‎packages/griffelib/tests/test_models.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
import sys
2222
from copy import deepcopy
2323
from textwrap import dedent
24+
from typing import TYPE_CHECKING
2425

2526
import pytest
2627

28+
if TYPE_CHECKING:
29+
from pathlib import Path
30+
2731
from griffe import (
32+
Alias,
33+
AliasResolutionError,
2834
Attribute,
2935
Class,
3036
Docstring,
@@ -46,6 +52,21 @@
4652
)
4753

4854

55+
def test_lazy_alias_resolution_error_message(tmp_path: Path) -> None:
56+
"""Build detailed alias-resolution messages only when rendered."""
57+
module = Module("module", filepath=tmp_path / "module.py")
58+
alias = Alias("alias", "missing.target", parent=module)
59+
summary = "Could not resolve alias module.alias pointing at missing.target"
60+
expected = f"Could not resolve alias module.alias pointing at missing.target (in {module.relative_filepath}:None)"
61+
62+
error = AliasResolutionError(alias)
63+
assert error.args == (summary,)
64+
assert error._detailed_message is None
65+
assert str(error) == expected
66+
assert error.args == (summary,)
67+
assert error._detailed_message == expected
68+
69+
4970
def test_submodule_exports() -> None:
5071
"""Check that a module is exported depending on whether it was also imported."""
5172
root = Module("root")

0 commit comments

Comments
 (0)