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 448325369f)

Co-authored-by: Rotuna <sadhanasrinivasan@protonmail.com>
This commit is contained in:
Miss Islington (bot) 2020-05-25 13:01:20 -07:00 committed by GitHub
parent 31084be618
commit 318a18eb88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 2 deletions

View file

@ -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)

View file

@ -922,7 +922,8 @@ class PurePath(object):
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:])

View file

@ -0,0 +1 @@
Updated the error message and docs of PurePath.relative_to() to better reflect the function behaviour.