[Python-checkins] bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)

Rotuna webhook-mailer at python.org
Mon May 25 15:42:33 EDT 2020


https://github.com/python/cpython/commit/448325369ff73011d34d6c3a493014fe3ead8843
commit: 448325369ff73011d34d6c3a493014fe3ead8843
branch: master
author: Rotuna <sadhanasrinivasan at protonmail.com>
committer: GitHub <noreply at github.com>
date: 2020-05-25T20:42:28+01:00
summary:

bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)

Co-authored-by: Sadhana Srinivasan <rotuna at Sadhanas-MBP.fritz.box>

files:
A Misc/NEWS.d/next/Library/2020-04-20-22-08-36.bpo-23082.iX90Id.rst
M Doc/library/pathlib.rst
M Lib/pathlib.py

diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index 83f7c836f0e71..bf6fee44df2c8 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -551,7 +551,9 @@ Pure paths provide the following methods and properties:
         File "<stdin>", line 1, in <module>
         File "pathlib.py", line 694, in relative_to
           .format(str(self), str(formatted)))
-      ValueError: '/etc/passwd' does not start with '/usr'
+      ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.
+
+   NOTE: This function is part of :class:`PurePath` and works with strings. It does not check or access the underlying file structure.
 
 
 .. method:: PurePath.with_name(name)
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index f98d69eb04ac3..9f5e27b91178e 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -922,7 +922,8 @@ def relative_to(self, *other):
         cf = self._flavour.casefold_parts
         if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
             formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
-            raise ValueError("{!r} does not start with {!r}"
+            raise ValueError("{!r} is not in the subpath of {!r}"
+                    " OR one path is relative and the other is absolute."
                              .format(str(self), str(formatted)))
         return self._from_parsed_parts('', root if n == 1 else '',
                                        abs_parts[n:])
diff --git a/Misc/NEWS.d/next/Library/2020-04-20-22-08-36.bpo-23082.iX90Id.rst b/Misc/NEWS.d/next/Library/2020-04-20-22-08-36.bpo-23082.iX90Id.rst
new file mode 100644
index 0000000000000..13ed0defe529c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-20-22-08-36.bpo-23082.iX90Id.rst
@@ -0,0 +1 @@
+Updated the error message and docs of PurePath.relative_to() to better reflect the function behaviour.
\ No newline at end of file



More information about the Python-checkins mailing list