Skip to content
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

pathlib relative_to() can give confusing error message #67271

Closed
cjerdonek opened this issue Dec 18, 2014 · 7 comments
Closed

pathlib relative_to() can give confusing error message #67271

cjerdonek opened this issue Dec 18, 2014 · 7 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@cjerdonek
Copy link
Member

BPO 23082
Nosy @pitrou, @cjerdonek, @zooba, @miss-islington, @tirkarthi, @idomic, @rotuna
PRs
  • bpo-23082: Better error message for PurePath.relative_to() from pathlib #19611
  • [3.9] bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611) #20395
  • 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:

    assignee = 'https://github.com/pitrou'
    closed_at = <Date 2020-05-25.19:43:02.507>
    created_at = <Date 2014-12-18.13:37:19.235>
    labels = ['easy', 'type-bug', 'library', '3.9', '3.10']
    title = 'pathlib relative_to() can give confusing error message'
    updated_at = <Date 2020-05-25.20:01:26.106>
    user = 'https://github.com/cjerdonek'

    bugs.python.org fields:

    activity = <Date 2020-05-25.20:01:26.106>
    actor = 'miss-islington'
    assignee = 'pitrou'
    closed = True
    closed_date = <Date 2020-05-25.19:43:02.507>
    closer = 'steve.dower'
    components = ['Library (Lib)']
    creation = <Date 2014-12-18.13:37:19.235>
    creator = 'chris.jerdonek'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 23082
    keywords = ['patch', 'easy', 'newcomer friendly']
    message_count = 7.0
    messages = ['232876', '232877', '366433', '366568', '369838', '369914', '369919']
    nosy_count = 8.0
    nosy_names = ['pitrou', 'Arfrever', 'chris.jerdonek', 'steve.dower', 'miss-islington', 'xtreak', 'Ido Michael', 'rotuna']
    pr_nums = ['19611', '20395']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23082'
    versions = ['Python 3.9', 'Python 3.10']

    @cjerdonek
    Copy link
    Member Author

    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.

    @cjerdonek cjerdonek added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 18, 2014
    @cjerdonek
    Copy link
    Member Author

    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.

    @Arfrever Arfrever mannequin assigned pitrou Dec 19, 2014
    @zooba
    Copy link
    Member

    zooba commented Apr 14, 2020

    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.

    @zooba zooba added easy 3.8 only security fixes 3.9 only security fixes labels Apr 14, 2020
    @rotuna
    Copy link
    Mannequin

    rotuna mannequin commented Apr 15, 2020

    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'

    @idomic
    Copy link
    Mannequin

    idomic mannequin commented May 24, 2020

    Hey looks like this PR is good to go?

    @zooba
    Copy link
    Member

    zooba commented May 25, 2020

    New changeset 4483253 by Rotuna in branch 'master':
    bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)
    4483253

    @zooba zooba added 3.10 only security fixes and removed 3.8 only security fixes labels May 25, 2020
    @zooba zooba closed this as completed May 25, 2020
    @miss-islington
    Copy link
    Contributor

    New changeset 318a18e by Miss Islington (bot) in branch '3.9':
    bpo-23082: Better error message for PurePath.relative_to() from pathlib (GH-19611)
    318a18e

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants