diff --git a/AUTHORS b/AUTHORS index fc79b555a..96c434d6a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -139,6 +139,7 @@ Grigorii Eremeev (budulianin) Guido Wesdorp Guoqiang Zhang Harald Armin Massa +Harshna Henk-Jaap Wagenaar Holger Kohr Hugo van Kemenade diff --git a/changelog/8990.bugfix.rst b/changelog/8990.bugfix.rst new file mode 100644 index 000000000..ab63eb9a7 --- /dev/null +++ b/changelog/8990.bugfix.rst @@ -0,0 +1 @@ +Fix `pytest -vv` crashing with an internal exception `AttributeError: 'str' object has no attribute 'relative_to'` in some cases. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index f641e6491..b44753e1a 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -685,6 +685,8 @@ def bestrelpath(directory: Path, dest: Path) -> str: If no such path can be determined, returns dest. """ + assert isinstance(directory, Path) + assert isinstance(dest, Path) if dest == directory: return os.curdir # Find the longest common directory. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 8c5be3b78..96648d86c 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -862,8 +862,10 @@ class TerminalReporter: yellow=True, ) - def _locationline(self, nodeid, fspath, lineno, domain): - def mkrel(nodeid): + def _locationline( + self, nodeid: str, fspath: str, lineno: Optional[int], domain: str + ) -> str: + def mkrel(nodeid: str) -> str: line = self.config.cwd_relative_nodeid(nodeid) if domain and line.endswith(domain): line = line[: -len(domain)] @@ -873,13 +875,12 @@ class TerminalReporter: return line # collect_fspath comes from testid which has a "/"-normalized path. - if fspath: res = mkrel(nodeid) if self.verbosity >= 2 and nodeid.split("::")[0] != fspath.replace( "\\", nodes.SEP ): - res += " <- " + bestrelpath(self.startpath, fspath) + res += " <- " + bestrelpath(self.startpath, Path(fspath)) else: res = "[location]" return res + " "