Skip to content

Commit

Permalink
refactor: Full expressions use canonical names
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 22, 2023
1 parent c1b08b1 commit 65c7184
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/griffe/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ def __init__(self, source: str, full: str | Callable) -> None:
def __eq__(self, other: Any) -> bool:
if isinstance(other, str):
return self.full == other or self.brief == other
if isinstance(other, (Name, Expression)):
if isinstance(other, Name):
return self.full == other.full
if isinstance(other, Expression):
return self.full == other.source
raise NotImplementedError(f"uncomparable types: {type(self)} and {type(other)}")

def __repr__(self) -> str:
Expand All @@ -49,6 +51,15 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return self.source

@property
def brief(self) -> str:
"""Return the brief source name.
Returns:
The last part of the source name.
"""
return self.source.rsplit(".", 1)[-1]

@property
def full(self) -> str:
"""Return the full, resolved name.
Expand All @@ -69,13 +80,13 @@ def full(self) -> str:
return self._full

@property
def brief(self) -> str:
"""Return the brief source name.
def canonical(self) -> str:
"""Return the canonical name (resolved one, not alias name).
Returns:
The last part of the source name.
The canonical name.
"""
return self.source.rsplit(".", 1)[-1]
return self.full.rsplit(".", 1)[-1]

def as_dict(self, **kwargs: Any) -> dict[str, Any]: # noqa: ARG002
"""Return this name's data as a dictionary.
Expand Down Expand Up @@ -109,23 +120,38 @@ def __init__(self, *values: str | Expression | Name) -> None:
"""
super().__init__()
self.extend(values)
# for value in values:
# if isinstance(value, Expression):

def __str__(self):
return "".join(str(element) for element in self)

@property
def full(self) -> str:
"""Return self as a string.
def source(self) -> str:
"""Return the expression as written in the source.
This property is only useful to the AST utils.
Returns:
Self as a string.
The expression as a string.
"""
return str(self)

@property
def full(self) -> str:
"""Return the full expression as a string with canonical names (imported ones, not aliases).
This property is only useful to the AST utils.
Returns:
The expression as a string.
"""
parts = []
for element in self:
if isinstance(element, str):
parts.append(element)
else:
parts.append(element.canonical)
return "".join(parts)

@property
def kind(self) -> str:
"""Return the main type object as a string.
Expand Down

0 comments on commit 65c7184

Please sign in to comment.