Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Guard against more alias resolution errors #103

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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