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's relative_to should behave like os.path.relpath
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CAM-Gerlach, CuriousLearner, Socob, ammar2, barneygale, bowiechen, d.ragusa, eryksun, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2020-04-22 00:03 by d.ragusa, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
relative_to.patch d.ragusa, 2020-04-28 03:52
Pull Requests
URL Status Linked Edit
PR 19807 closed python-dev, 2020-04-30 00:03
PR 19813 open d.ragusa, 2020-04-30 10:30
Messages (11)
msg366958 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-04-22 00:03
Can we improve pathlib.relative_to(other) so that it handles the case of a path not being a direct child of other, like os.path.relpath?

For example:
Path('/some/thing').relative_to('/foo') -> Path('../some/thing')

At the moment it just raises an exception.
msg367037 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2020-04-22 18:18
The current behaviour is by design.  I would not mind adding an option to control it, though.

If you are new to Python development and want to submit a patch or PR, I recommend reading the Developer's Guide:
https://devguide.python.org/
msg367041 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-04-22 19:00
Thanks for your answer. Yeah, I'm new, I'm reading the guide, sorry for any
faux pas :)

Ok, an option would be great as well, a simple True/False switch? Any
suggestion for the name?
I'll get back with a proper patch this time.

On Wed, Apr 22, 2020 at 8:18 PM Antoine Pitrou <report@bugs.python.org>
wrote:

>
> Antoine Pitrou <solipsis@pitrou.net> added the comment:
>
> The current behaviour is by design.  I would not mind adding an option to
> control it, though.
>
> If you are new to Python development and want to submit a patch or PR, I
> recommend reading the Developer's Guide:
> https://devguide.python.org/
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue40358>
> _______________________________________
>
msg367047 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-04-22 19:50
Note that the implementation of relpath is pure and thus assumes it's working with existing, resolved paths (i.e. "the filesystem is not accessed to confirm the existence or nature of path or start"). For example:

    >>> os.path.relpath('/some/thing', '/symlink')
    '../some/thing'

If "symlink" targets "/spam/eggs/foo", then the resolved path would be "/spam/eggs/some/thing" instead of "/some/thing". Maybe the relative_to method should default to a `strict` mode that raises ValueError on ambiguous cases that depend on the "existence or nature" of the paths. I think the current implementation is strict.
msg367490 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-04-28 03:52
Yeah, you're right, there's no access to the filesystem and the result
is generated assuming the paths are already resolved.
`strict` seems to be an appropriate name for the option, thanks.

I've looked into the test suite, it helped a lot especially with
Windows paths, they were more complicated than I though.
I've duplicated the tests to verify that it still function as before
and I've added some tests for values that would raise an exception. It
works.
I'm not overly fond of the way I check for unrelated paths, but I
couldn't think of something more elegant.

Any input is appreciated.
msg367697 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-04-29 20:08
Thank you for your work on this Domenico. For reviewing the code, would you mind creating a Github pull request for it as described here https://devguide.python.org/pullrequest/
msg367723 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-04-30 00:49
I may have forgotten to use the proper format for the title of each
commit, should I delete the pull request and make a new one or can it
be fixed when (or if) it's pulled?

On Thu, Apr 30, 2020 at 2:03 AM Roundup Robot <report@bugs.python.org> wrote:
>
>
> Change by Roundup Robot <devnull@psf.upfronthosting.co.za>:
>
>
> ----------
> nosy: +python-dev
> nosy_count: 5.0 -> 6.0
> pull_requests: +19128
> stage:  -> patch review
> pull_request: https://github.com/python/cpython/pull/19807
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue40358>
> _______________________________________
msg370254 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-05-28 17:18
I've solved the conflicts with GH-19611 (bpo-23082: Better error message for PurePath.relative_to() from pathlib) that was merged in the mean time and improved the documentation.

Everything appears to be in order, can you take a look at it?
msg393696 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-05-14 22:18
On #44078 (closed as duplicate), Mark Hammond made a similar request.
msg393777 - (view) Author: Barney Gale (barneygale) * Date: 2021-05-17 00:55
Also requested in #42234.
msg408249 - (view) Author: C.A.M. Gerlach (CAM-Gerlach) * Date: 2021-12-10 21:44
For the record, requested [on Discourse](https://discuss.python.org/t/how-to-go-on-with-a-tiny-improvement-idea-for-pathlib/12472) as well, with a fairly similar proposal.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84538
2022-02-22 23:57:02terry.reedysetnosy: - terry.reedy
2022-02-22 23:50:06Socobsetnosy: + Socob
2021-12-10 21:44:00CAM-Gerlachsetnosy: + CAM-Gerlach
messages: + msg408249
2021-06-23 10:41:51serhiy.storchakalinkissue42234 superseder
2021-06-23 10:41:12serhiy.storchakalinkissue20012 superseder
2021-05-17 00:55:45barneygalesetnosy: + barneygale
messages: + msg393777
2021-05-14 22:18:59terry.reedysetnosy: + terry.reedy

messages: + msg393696
versions: + Python 3.11, - Python 3.10
2021-05-14 22:17:48terry.reedylinkissue44078 superseder
2020-05-28 17:18:23d.ragusasetmessages: + msg370254
versions: + Python 3.10, - Python 3.9
2020-04-30 10:54:35CuriousLearnersetnosy: + CuriousLearner
2020-04-30 10:30:01d.ragusasetpull_requests: + pull_request19133
2020-04-30 00:49:29d.ragusasetmessages: + msg367723
2020-04-30 00:03:25python-devsetnosy: + python-dev

pull_requests: + pull_request19128
stage: patch review
2020-04-29 20:08:41ammar2setnosy: + ammar2
messages: + msg367697
2020-04-28 03:53:15d.ragusasetfiles: - pathlib.diff
2020-04-28 03:52:45d.ragusasetfiles: + relative_to.patch

messages: + msg367490
2020-04-22 19:50:21eryksunsetnosy: + eryksun
messages: + msg367047
2020-04-22 19:00:13d.ragusasetmessages: + msg367041
2020-04-22 18:18:16pitrousetmessages: + msg367037
2020-04-22 05:10:18bowiechensetnosy: + bowiechen
2020-04-22 02:47:14xtreaksetnosy: + pitrou
2020-04-22 00:03:03d.ragusacreate