Skip to content

Commit

Permalink
fix: Guard against more alias resolution errors
Browse files Browse the repository at this point in the history
Issue #83: #83
PR #103: #103
  • Loading branch information
pawamoy authored Sep 30, 2022
1 parent 24a5998 commit 2be135d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ def parent(self) -> Module | Class | None:
def parent(self, value: Module | Class) -> None:
self._parent = value
if self.resolved:
with suppress(AliasResolutionError):
with suppress(AliasResolutionError, CyclicAliasError):
self._target.aliases[self.path] = self # type: ignore[union-attr] # we just checked the target is not None

@cached_property
Expand Down Expand Up @@ -923,7 +923,7 @@ def target(self) -> Object | Alias:

@target.setter
def target(self, value: Object | Alias) -> None:
if value is self:
if value is self or value.path == self.path:
raise CyclicAliasError([self.target_path])
self._target = value
self.target_path = value.path
Expand Down Expand Up @@ -958,7 +958,7 @@ def resolved(self) -> bool:
Returns:
True or False.
"""
return self._target is not None
return self._target is not None and (not self._target.is_alias or self._target.resolved) # type: ignore[union-attr]

@cached_property
def wildcard(self) -> str | None:
Expand Down
13 changes: 9 additions & 4 deletions src/griffe/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from contextlib import suppress
from typing import Any, Sequence, Type, TypeVar

from griffe.exceptions import AliasResolutionError, CyclicAliasError
from griffe.logger import get_logger
from griffe.merger import merge_stubs

Expand Down Expand Up @@ -69,11 +70,15 @@ def __setitem__(self, key: str | Sequence[str], value) -> None: # noqa: WPS231
# when reassigning a module to an existing one,
# try to merge them as one regular and one stubs module
# (implicit support for .pyi modules)
if member.is_module and value.is_module:
with suppress(ValueError):
value = merge_stubs(member, value)
if member.is_module:
with suppress(AliasResolutionError, CyclicAliasError):
if value.is_module:
logger.debug(f"Trying to merge {member.filepath} and {value.filepath}")
with suppress(ValueError):
value = merge_stubs(member, value)
for alias in member.aliases.values():
alias.target = value
with suppress(CyclicAliasError):
alias.target = value
self.members[name] = value # type: ignore[attr-defined]
if self.is_collection: # type: ignore[attr-defined]
value._modules_collection = self # noqa: WPS437
Expand Down

0 comments on commit 2be135d

Please sign in to comment.