-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
pathlib relative_to() can give confusing error message #67271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
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. |
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. |
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' |
Hey looks like this PR is good to go? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: