Skip to content

Commit

Permalink
fix: Prevent alias resolution errors when checking for API breaking c…
Browse files Browse the repository at this point in the history
…hanges

Issue #145: #145
  • Loading branch information
pawamoy committed Jul 2, 2023
1 parent f9e8ba3 commit 93c964a
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/griffe/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

import contextlib
import enum
from typing import Any, Iterable, Iterator
from typing import TYPE_CHECKING, Any, Iterable, Iterator

from colorama import Fore, Style

from griffe.dataclasses import Alias, Attribute, Class, Function, Object, ParameterKind
from griffe.exceptions import AliasResolutionError
from griffe.logger import get_logger

if TYPE_CHECKING:
from pathlib import Path

POSITIONAL = frozenset((ParameterKind.positional_only, ParameterKind.positional_or_keyword))
KEYWORD = frozenset((ParameterKind.keyword_only, ParameterKind.positional_or_keyword))
POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
Expand Down Expand Up @@ -97,14 +100,39 @@ def explain(self, style: ExplanationStyle = ExplanationStyle.ONE_LINE) -> str:
"""
return getattr(self, f"_explain_{style.value}")()

@property
def _filepath(self) -> Path:
if self.obj.is_alias:
return self.obj.parent.filepath # type: ignore[union-attr,return-value]
return self.obj.filepath # type: ignore[union-attr,return-value]

@property
def _canonical_path(self) -> str:
if self.obj.is_alias:
return self.obj.path
return self.obj.canonical_path

@property
def _module_path(self) -> str:
if self.obj.is_alias:
return self.obj.parent.module.path # type: ignore[union-attr]
return self.obj.module.path

@property
def _relative_path(self) -> str:
return self.obj.canonical_path[len(self.obj.module.canonical_path) + 1 :]
return self._canonical_path[len(self._module_path) + 1 :]

@property
def _lineno(self) -> int:
if self.obj.is_alias:
return self.obj.alias_lineno or 0 # type: ignore[attr-defined]
return self.obj.lineno or 0

def _format_location(self) -> str:
return f"{Style.BRIGHT}{self.obj.filepath}{Style.RESET_ALL}:{self.obj.lineno}"
return f"{Style.BRIGHT}{self._filepath}{Style.RESET_ALL}:{self._lineno}"

def _format_title(self) -> str:
return self._relative_path()
return self._relative_path

def _format_kind(self) -> str:
return f"{Fore.YELLOW}{self.kind.value}{Fore.RESET}"
Expand Down Expand Up @@ -156,7 +184,7 @@ class ParameterMovedBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_MOVED

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return ""
Expand All @@ -171,7 +199,7 @@ class ParameterRemovedBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_REMOVED

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return ""
Expand All @@ -186,7 +214,7 @@ class ParameterChangedKindBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_CHANGED_KIND

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return str(self.old_value.kind.value)
Expand All @@ -201,7 +229,7 @@ class ParameterChangedDefaultBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_CHANGED_DEFAULT

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return str(self.old_value.default)
Expand All @@ -216,7 +244,7 @@ class ParameterChangedRequiredBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_CHANGED_REQUIRED

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.old_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return ""
Expand All @@ -231,7 +259,7 @@ class ParameterAddedRequiredBreakage(Breakage):
kind: BreakageKind = BreakageKind.PARAMETER_ADDED_REQUIRED

def _format_title(self) -> str:
return f"{self._relative_path()}({Fore.BLUE}{self.new_value.name}{Fore.RESET})"
return f"{self._relative_path}({Fore.BLUE}{self.new_value.name}{Fore.RESET})"

def _format_old_value(self) -> str:
return ""
Expand Down

0 comments on commit 93c964a

Please sign in to comment.