This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib relative_to() can give confusing error message
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pitrou Nosy List: Arfrever, Ido Michael, chris.jerdonek, miss-islington, pitrou, rotuna, steve.dower, xtreak
Priority: normal Keywords: easy, newcomer friendly, patch

Created on 2014-12-18 13:37 by chris.jerdonek, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19611 merged rotuna, 2020-04-19 23:04
PR 20395 merged miss-islington, 2020-05-25 19:42
Messages (7)
msg232876 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2014-12-18 13:37
pathlib's relative_to(other) can give a confusing message when "other" is os.curdir.

For example--

    Python 3.4.2 (default, Nov 12 2014, 18:23:59) 
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> from pathlib import Path
    >>> Path("/foo").relative_to(os.curdir)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/pathlib.py", line 806, in relative_to
        .format(str(self), str(formatted)))
    ValueError: '/foo' does not start with ''

I guess the error here is that the path must be relative when "other" is relative.
msg232877 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2014-12-18 13:46
By the way, here is another (less) confusing error message:

    >>> Path("foo").relative_to("fo")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/pathlib.py", line 806, in relative_to
        .format(str(self), str(formatted)))
    ValueError: 'foo' does not start with 'fo'

Without knowing that "foo" is a path, the message seems wrong.  If it said something like "Path 'foo' does not start with part 'fo'", it would be clearer.
msg366433 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-04-14 19:23
I agree it's worth improving the error message (and probably the docs too).

It's never made clear that relative_to only looks deeper (won't ever generate leading ".." parts) - the closest hint in the docs is that os.path.relpath is different (and that isn't even in the relative_to() section).


Tagging this easy/newcomer friendly. If you'd like to work on it, just post here - I'm happy to help get it merged.
msg366568 - (view) Author: Sadhana Srinivasan (rotuna) * Date: 2020-04-15 23:13
I'll work on this. 

I tried to come up with a single error message but having two different error messages seems like a better idea to me. One for when the path isn't a subpath and one for when absolute and relative paths are mixed. Basically to explain this:

>>> Path("/Users/rotuna/Documents/cpython/Libs").relative_to(Path("./Libs"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rotuna/Documents/cpython/Lib/pathlib.py", line 907, in relative_to
    raise ValueError("{!r} is not a subpath of{!r}. NOTE: If this is not true, use absolute paths"
ValueError: '/Users/rotuna/Documents/cpython/Libs' does not start with 'Libs'
msg369838 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-05-24 22:24
Hey looks like this PR is good to go?
msg369914 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-05-25 19:42
New changeset 448325369ff73011d34d6c3a493014fe3ead8843 by Rotuna in branch 'master':
bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)
https://github.com/python/cpython/commit/448325369ff73011d34d6c3a493014fe3ead8843
msg369919 - (view) Author: miss-islington (miss-islington) Date: 2020-05-25 20:01
New changeset 318a18eb889e8733ffb25ada139fdd423606a609 by Miss Islington (bot) in branch '3.9':
bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)
https://github.com/python/cpython/commit/318a18eb889e8733ffb25ada139fdd423606a609
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67271
2020-05-25 20:01:26miss-islingtonsetnosy: + miss-islington
messages: + msg369919
2020-05-25 19:43:02steve.dowersetstatus: open -> closed
versions: + Python 3.10, - Python 3.8
nosy: - miss-islington

resolution: fixed
stage: patch review -> resolved
2020-05-25 19:42:49miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request19658
2020-05-25 19:42:32steve.dowersetmessages: + msg369914
2020-05-24 22:24:12Ido Michaelsetnosy: + Ido Michael
messages: + msg369838
2020-04-19 23:04:34rotunasetkeywords: + patch
stage: patch review
pull_requests: + pull_request18945
2020-04-15 23:13:58rotunasetnosy: + rotuna
messages: + msg366568
2020-04-14 19:23:26steve.dowersetversions: + Python 3.8, Python 3.9, - Python 3.4
nosy: + steve.dower

messages: + msg366433

keywords: + easy, newcomer friendly
2018-09-22 18:16:31xtreaksetnosy: + xtreak
2014-12-19 00:33:19Arfreversetassignee: pitrou

nosy: + pitrou, Arfrever
2014-12-18 13:46:59chris.jerdoneksetmessages: + msg232877
2014-12-18 13:37:19chris.jerdonekcreate