Skip to content

Commit 9d17ceb

Browse files
authored
Use correct input arg type for _bestrelpath (#9238)
Closes #8990
1 parent f288afd commit 9d17ceb

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Grigorii Eremeev (budulianin)
139139
Guido Wesdorp
140140
Guoqiang Zhang
141141
Harald Armin Massa
142+
Harshna
142143
Henk-Jaap Wagenaar
143144
Holger Kohr
144145
Hugo van Kemenade

changelog/8990.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `pytest -vv` crashing with an internal exception `AttributeError: 'str' object has no attribute 'relative_to'` in some cases.

src/_pytest/pathlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ def bestrelpath(directory: Path, dest: Path) -> str:
685685
686686
If no such path can be determined, returns dest.
687687
"""
688+
assert isinstance(directory, Path)
689+
assert isinstance(dest, Path)
688690
if dest == directory:
689691
return os.curdir
690692
# Find the longest common directory.

src/_pytest/terminal.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,10 @@ def _report_keyboardinterrupt(self) -> None:
862862
yellow=True,
863863
)
864864

865-
def _locationline(self, nodeid, fspath, lineno, domain):
866-
def mkrel(nodeid):
865+
def _locationline(
866+
self, nodeid: str, fspath: str, lineno: Optional[int], domain: str
867+
) -> str:
868+
def mkrel(nodeid: str) -> str:
867869
line = self.config.cwd_relative_nodeid(nodeid)
868870
if domain and line.endswith(domain):
869871
line = line[: -len(domain)]
@@ -873,13 +875,12 @@ def mkrel(nodeid):
873875
return line
874876

875877
# collect_fspath comes from testid which has a "/"-normalized path.
876-
877878
if fspath:
878879
res = mkrel(nodeid)
879880
if self.verbosity >= 2 and nodeid.split("::")[0] != fspath.replace(
880881
"\\", nodes.SEP
881882
):
882-
res += " <- " + bestrelpath(self.startpath, fspath)
883+
res += " <- " + bestrelpath(self.startpath, Path(fspath))
883884
else:
884885
res = "[location]"
885886
return res + " "

0 commit comments

Comments
 (0)