File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
6976class CyclicAliasError (GriffeError ):
Original file line number Diff line number Diff line change 2525import pytest
2626
2727from griffe import (
28+ AliasResolutionError ,
2829 ExprName ,
2930 GriffeLoader ,
3031 temporary_inspected_package ,
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+
3653if TYPE_CHECKING :
3754 from pathlib import Path
3855
Original file line number Diff line number Diff line change 2121import sys
2222from copy import deepcopy
2323from textwrap import dedent
24+ from typing import TYPE_CHECKING
2425
2526import pytest
2627
28+ if TYPE_CHECKING :
29+ from pathlib import Path
30+
2731from griffe import (
32+ Alias ,
33+ AliasResolutionError ,
2834 Attribute ,
2935 Class ,
3036 Docstring ,
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+
4970def test_submodule_exports () -> None :
5071 """Check that a module is exported depending on whether it was also imported."""
5172 root = Module ("root" )
You can’t perform that action at this time.
0 commit comments