Skip to content

Commit

Permalink
fix: Fix name resolution for inspected data
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 31, 2022
1 parent a9a378f commit ed3e7e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ def resolve(self, name: str) -> str:
Returns:
The resolved name.
"""
if name in self.members and not self.members[name].is_alias:
if name in self.members:
if self.members[name].is_alias:
return self.members[name].target_path # type: ignore[union-attr]
return self.members[name].path
if name in self.imports:
return self.imports[name]
Expand Down Expand Up @@ -709,6 +711,7 @@ class Alias(ObjectAliasMixin):
lineno: The alias starting line number.
endlineno: The alias ending line number.
parent: The alias parent.
target_path: The alias target path.
"""

is_alias: bool = True
Expand All @@ -735,10 +738,10 @@ def __init__(
self.name: str = name
if isinstance(target, str):
self._target: Object | Alias | None = None
self._target_path: str = target
self.target_path: str = target
else:
self._target = target
self._target_path = target.path
self.target_path = target.path
if parent is not None:
with suppress(AliasResolutionError):
target.aliases[self.path] = self
Expand All @@ -750,12 +753,12 @@ def __init__(
def __getattr__(self, name: str) -> Any:
# forward everything to the target
if self._passed_through:
raise CyclicAliasError([self._target_path])
raise CyclicAliasError([self.target_path])
self._passed_through = True
try:
attr = getattr(self.target, name)
except CyclicAliasError as error:
raise CyclicAliasError([self._target_path] + error.chain)
raise CyclicAliasError([self.target_path] + error.chain)
finally:
self._passed_through = False
return attr
Expand Down Expand Up @@ -836,7 +839,7 @@ def target(self) -> Object | Alias:
@target.setter
def target(self, value: Object | Alias) -> None:
if value is self:
raise CyclicAliasError([self._target_path])
raise CyclicAliasError([self.target_path])
self._target = value
if self.parent is not None:
self._target.aliases[self.path] = self
Expand All @@ -853,11 +856,11 @@ def resolve_target(self) -> None:
CyclicAliasError: When the resolved target is the alias itself.
"""
try:
resolved = self.modules_collection[self._target_path]
resolved = self.modules_collection[self.target_path]
except KeyError as error:
raise AliasResolutionError(self._target_path) from error
raise AliasResolutionError(self.target_path) from error
if resolved is self:
raise CyclicAliasError([self._target_path])
raise CyclicAliasError([self.target_path])
self._target = resolved
if self.parent is not None:
self._target.aliases[self.path] = self # type: ignore[union-attr] # we just set the target
Expand All @@ -879,7 +882,7 @@ def wildcard(self) -> str | None:
The wildcard imported module, or None.
"""
if self.name.endswith("/*"):
return self._target_path
return self.target_path
return None

def as_dict(self, full: bool = False, **kwargs: Any) -> dict[str, Any]:
Expand All @@ -895,7 +898,7 @@ def as_dict(self, full: bool = False, **kwargs: Any) -> dict[str, Any]:
base = {
"kind": Kind.ALIAS,
"name": self.name,
"target_path": self._target_path,
"target_path": self.target_path,
}

if full:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ def test_annotations_from_classes():
assert func.parameters[0].name == "a"
assert func.parameters[0].annotation == Name("A", full=f"{module.name}.A")
assert func.returns == Name("A", full=f"{module.name}.A")


def test_class_level_imports():
"""Assert annotations using class-level imports are resolved."""
with temporary_inspected_module(
"""
class A:
from io import StringIO
def method(self, p: StringIO):
pass
"""
) as module:
method = module["A.method"]
assert method.parameters["p"].annotation == Name("StringIO", full="io.StringIO")

0 comments on commit ed3e7e5

Please sign in to comment.