Skip to content

Commit

Permalink
refactor: Use final target in alias proxies
Browse files Browse the repository at this point in the history
The previous comment stating that we should not use
the final target because it could be of another kind
than the first target makes no sense. If the first target
is an alias, then accessing its proxy would access the
next one, until the final target one is reached.
Aliases chains are just that: chains of aliases
with a concrete object at the end. There cannot be
a concrete object in the middle, therefore there
cannot be different kinds in a chain, only aliases
and a single kind at the end.
  • Loading branch information
pawamoy committed Oct 17, 2023
1 parent 7320640 commit 731d662
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,16 +1013,16 @@ def del_member(self, key: str | Sequence[str]) -> None: # noqa: D102

# SPECIFIC MODULE/CLASS/FUNCTION/ATTRIBUTE PROXIES ---------------
# These methods and properties exist on targets of specific kind.
# We have to call the method/property on the first target, not the final one
# (which could be of another kind).
# We first try to reach the final target, trigerring alias resolution errors
# and cyclic aliases errors early. We avoid recursing in the alias chain.

@property
def _filepath(self) -> Path | list[Path] | None:
return cast(Module, self.target)._filepath
return cast(Module, self.final_target)._filepath

@property
def bases(self) -> list[Expr | str]: # noqa: D102
return cast(Class, self.target).bases
return cast(Class, self.final_target).bases

@property
def decorators(self) -> list[Decorator]: # noqa: D102
Expand Down Expand Up @@ -1054,43 +1054,43 @@ def is_namespace_subpackage(self) -> bool: # noqa: D102

@property
def overloads(self) -> dict[str, list[Function]] | list[Function] | None: # noqa: D102
return cast(Union[Module, Class, Function], self.target).overloads
return cast(Union[Module, Class, Function], self.final_target).overloads

@overloads.setter
def overloads(self, overloads: list[Function] | None) -> None:
cast(Union[Module, Class, Function], self.target).overloads = overloads
cast(Union[Module, Class, Function], self.final_target).overloads = overloads

@property
def parameters(self) -> Parameters: # noqa: D102
return cast(Function, self.target).parameters
return cast(Function, self.final_target).parameters

@property
def returns(self) -> str | Expr | None: # noqa: D102
return cast(Function, self.target).returns
return cast(Function, self.final_target).returns

@returns.setter
def returns(self, returns: str | Expr | None) -> None:
cast(Function, self.target).returns = returns
cast(Function, self.final_target).returns = returns

@property
def setter(self) -> Function | None: # noqa: D102
return cast(Function, self.target).setter
return cast(Function, self.final_target).setter

@property
def deleter(self) -> Function | None: # noqa: D102
return cast(Function, self.target).deleter
return cast(Function, self.final_target).deleter

@property
def value(self) -> str | Expr | None: # noqa: D102
return cast(Attribute, self.target).value
return cast(Attribute, self.final_target).value

@property
def annotation(self) -> str | Expr | None: # noqa: D102
return cast(Attribute, self.target).annotation
return cast(Attribute, self.final_target).annotation

@annotation.setter
def annotation(self, annotation: str | Expr | None) -> None:
cast(Attribute, self.target).annotation = annotation
cast(Attribute, self.final_target).annotation = annotation

@property
def resolved_bases(self) -> list[Object]: # noqa: D102
Expand Down

0 comments on commit 731d662

Please # to comment.