Skip to content

Commit db08c7f

Browse files
committed
pathlib: improve comments on commonpath and bestrelpath
1 parent cd67c2a commit db08c7f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/_pytest/pathlib.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,10 @@ def absolutepath(path: Union[Path, str]) -> Path:
581581

582582
def commonpath(path1: Path, path2: Path) -> Optional[Path]:
583583
"""Return the common part shared with the other path, or None if there is
584-
no common part."""
584+
no common part.
585+
586+
If one path is relative and one is absolute, returns None.
587+
"""
585588
try:
586589
return Path(os.path.commonpath((str(path1), str(path2))))
587590
except ValueError:
@@ -592,13 +595,17 @@ def bestrelpath(directory: Path, dest: Path) -> str:
592595
"""Return a string which is a relative path from directory to dest such
593596
that directory/bestrelpath == dest.
594597
598+
The paths must be either both absolute or both relative.
599+
595600
If no such path can be determined, returns dest.
596601
"""
597602
if dest == directory:
598603
return os.curdir
599604
# Find the longest common directory.
600605
base = commonpath(directory, dest)
601-
# Can be the case on Windows.
606+
# Can be the case on Windows for two absolute paths on different drives.
607+
# Can be the case for two relative paths without common prefix.
608+
# Can be the case for a relative path and an absolute path.
602609
if not base:
603610
return str(dest)
604611
reldirectory = directory.relative_to(base)

0 commit comments

Comments
 (0)