Skip to content

Commit

Permalink
fix: Fix getting line numbers on aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 17, 2022
1 parent a3fbbf4 commit 351750e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
26 changes: 24 additions & 2 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ def __init__(
if parent is not None:
with suppress(AliasResolutionError):
target.aliases[self.path] = self
self.lineno: int | None = lineno
self.endlineno: int | None = endlineno
self.alias_lineno: int | None = lineno
self.alias_endlineno: int | None = endlineno
self._parent: Module | Class | None = parent
self._passed_through: bool = False

Expand Down Expand Up @@ -817,6 +817,28 @@ def kind(self) -> Kind:
except (AliasResolutionError, CyclicAliasError):
return Kind.ALIAS

@property
def lineno(self) -> int | None:
"""Return the target lineno or the alias lineno.
Returns:
The target lineno or the alias lineno.
"""
if self.resolved:
return self.target.lineno
return self.alias_lineno

@property
def endlineno(self) -> int | None:
"""Return the target endlineno or the alias endlineno.
Returns:
The target endlineno or the alias endlineno.
"""
if self.resolved:
return self.target.endlineno
return self.alias_endlineno

@property
def parent(self) -> Module | Class | None:
"""Return the parent of this alias.
Expand Down
25 changes: 15 additions & 10 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def expand_wildcards(self, obj: Object, seen: set | None = None) -> None: # noq
obj: The object and its members to recurse on.
seen: Used to avoid infinite recursion.
"""
expanded = {}
expanded = []
to_remove = []
seen = seen or set()
seen.add(obj.path)
Expand All @@ -201,27 +201,32 @@ def expand_wildcards(self, obj: Object, seen: set | None = None) -> None: # noq
logger.debug(f"Could not expand wildcard import {member.name} in {obj.path}: {error}")
continue
self.expand_wildcards(self.modules_collection[member.target_path]) # type: ignore[union-attr]
expanded.update(self._expand_wildcard(member)) # type: ignore[arg-type]
expanded.extend(self._expand_wildcard(member)) # type: ignore[arg-type]
to_remove.append(member.name)
elif not member.is_alias and member.is_module and member.path not in seen:
self.expand_wildcards(member, seen) # type: ignore[arg-type]

for name in to_remove:
del obj[name] # noqa: WPS420

for new_member in list(expanded.values()):
for new_member, alias_lineno, alias_endlineno in expanded:
if new_member.is_alias:
try:
# TODO: maybe don't shortcut aliases:
# we want to keep public paths
alias = Alias(new_member.name, new_member.target) # type: ignore[union-attr]
alias = Alias(
new_member.name,
new_member.target, # type: ignore[union-attr]
lineno=alias_lineno,
endlineno=alias_endlineno,
)
except AliasResolutionError:
alias = new_member # type: ignore[assignment] # noqa: WPS437
except CyclicAliasError as error: # noqa: WPS440
logger.debug(str(error))
continue
else:
alias = Alias(new_member.name, new_member)
alias = Alias(new_member.name, new_member, lineno=alias_lineno, endlineno=alias_endlineno)
obj[new_member.name] = alias

def resolve_module_aliases( # noqa: WPS231
Expand Down Expand Up @@ -419,11 +424,11 @@ def _member_parent(self, module: Module, subparts: tuple[str, ...], subpath: Pat
return member_parent
raise UnimportableModuleError(f"{subpath} is not importable")

def _expand_wildcard(self, wildcard_obj: Alias) -> dict[str, Object | Alias]:
def _expand_wildcard(self, wildcard_obj: Alias) -> list[tuple[Object | Alias, int | None, int | None]]:
module = self.modules_collection[wildcard_obj.wildcard] # type: ignore[index] # we know it's a wildcard
explicitely = "__all__" in module.members
return {
name: imported_member
for name, imported_member in module.members.items()
return [
(imported_member, wildcard_obj.alias_lineno, wildcard_obj.alias_endlineno)
for imported_member in module.members.values()
if imported_member.is_exported(explicitely=explicitely)
}
]

0 comments on commit 351750e

Please sign in to comment.