Skip to content

Commit 318a18e

Browse files
bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)
Co-authored-by: Sadhana Srinivasan <rotuna@Sadhanas-MBP.fritz.box> (cherry picked from commit 4483253) Co-authored-by: Rotuna <sadhanasrinivasan@protonmail.com>
1 parent 31084be commit 318a18e

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Doc/library/pathlib.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ Pure paths provide the following methods and properties:
551551
File "<stdin>", line 1, in <module>
552552
File "pathlib.py", line 694, in relative_to
553553
.format(str(self), str(formatted)))
554-
ValueError: '/etc/passwd' does not start with '/usr'
554+
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.
555+
556+
NOTE: This function is part of :class:`PurePath` and works with strings. It does not check or access the underlying file structure.
555557

556558

557559
.. method:: PurePath.with_name(name)

Lib/pathlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,8 @@ def relative_to(self, *other):
922922
cf = self._flavour.casefold_parts
923923
if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
924924
formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
925-
raise ValueError("{!r} does not start with {!r}"
925+
raise ValueError("{!r} is not in the subpath of {!r}"
926+
" OR one path is relative and the other is absolute."
926927
.format(str(self), str(formatted)))
927928
return self._from_parsed_parts('', root if n == 1 else '',
928929
abs_parts[n:])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated the error message and docs of PurePath.relative_to() to better reflect the function behaviour.

0 commit comments

Comments
 (0)