Skip to content

Commit

Permalink
Include error reason in CouldNotResolve (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-hilden committed Jan 28, 2023
1 parent 4c25cc8 commit eca0308
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/sphinx_codeautolink/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,12 @@ def filter_and_resolve(
continue # empty transform target (2 calls in a row)
try:
key = resolve_location(name, self.inventory)
except CouldNotResolve:
except CouldNotResolve as e:
if self.warn_failed_resolve:
path = ".".join(name.import_components).replace(".()", "()")
msg = (
f"Could not resolve {self._resolve_msg(name)}"
f" using path `{path}`.\nPossibly missing a type hint"
" or unable to follow highly dynamic code."
f" using path `{path}`.\n{str(e)}"
)
logger.warning(
msg,
Expand Down
14 changes: 7 additions & 7 deletions src/sphinx_codeautolink/extension/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class Cursor:
def make_cursor(components: List[str]) -> Tuple[List[str], Cursor]:
"""Divide components into module and rest, create cursor for following the rest."""
value, index = closest_module(tuple(components))
if value is None or index is None:
raise CouldNotResolve()
location = ".".join(components[:index])
return components[index:], Cursor(location, value, False)

Expand All @@ -68,7 +66,7 @@ def locate_type(cursor: Cursor, components: Tuple[str, ...], inventory) -> Curso
)

if cursor.value is None:
raise CouldNotResolve()
raise CouldNotResolve(f"{cursor.location} does not exist.")

if isclass(cursor.value):
cursor.instance = False
Expand Down Expand Up @@ -139,7 +137,9 @@ def get_return_annotation(func: Callable) -> Optional[type]:
or not isinstance(ret_annotation, type)
or hasattr(ret_annotation, "__origin__")
):
raise CouldNotResolve()
raise CouldNotResolve(
f"Unable to follow return annotation of {fully_qualified_name(func)}."
)

return ret_annotation

Expand All @@ -150,12 +150,12 @@ def fully_qualified_name(thing: Union[type, Callable]) -> str:


@lru_cache(maxsize=None)
def closest_module(components: Tuple[str, ...]) -> Tuple[Any, Optional[int]]:
def closest_module(components: Tuple[str, ...]) -> Tuple[Any, int]:
"""Find closest importable module."""
try:
mod = import_module(components[0])
except ImportError:
return None, None
except ImportError as e:
raise CouldNotResolve(f"Could not import {components[0]}.") from e

for i in range(1, len(components)):
try:
Expand Down

0 comments on commit eca0308

Please sign in to comment.