[issue40358] pathlib's relative_to should behave like os.path.relpath

2022-02-22 Thread Terry J. Reedy


Change by Terry J. Reedy :


--
nosy:  -terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2022-02-22 Thread Socob


Change by Socob <206a8...@opayq.com>:


--
nosy: +Socob

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2021-12-10 Thread C.A.M. Gerlach


C.A.M. Gerlach  added the comment:

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.

--
nosy: +CAM-Gerlach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2021-05-16 Thread Barney Gale


Barney Gale  added the comment:

Also requested in #42234.

--
nosy: +barneygale

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2021-05-14 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

On #44078 (closed as duplicate), Mark Hammond made a similar request.

--
nosy: +terry.reedy
versions: +Python 3.11 -Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-05-28 Thread Domenico Ragusa


Domenico Ragusa  added the comment:

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?

--
versions: +Python 3.10 -Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-30 Thread Sanyam Khurana


Change by Sanyam Khurana :


--
nosy: +CuriousLearner

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-30 Thread Domenico Ragusa


Change by Domenico Ragusa :


--
pull_requests: +19133
pull_request: https://github.com/python/cpython/pull/19813

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-29 Thread Domenico Ragusa


Domenico Ragusa  added the comment:

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  wrote:
>
>
> Change by Roundup Robot :
>
>
> --
> 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 
> 
> ___

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-29 Thread Roundup Robot


Change by Roundup Robot :


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

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-29 Thread Ammar Askar


Ammar Askar  added the comment:

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/

--
nosy: +ammar2

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-27 Thread Domenico Ragusa

Domenico Ragusa  added the comment:

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.

--
Added file: https://bugs.python.org/file49095/relative_to.patch

___
Python tracker 

___diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index f98d69e..eb25761 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -895,10 +895,10 @@ class PurePath(object):
 return self._from_parsed_parts(self._drv, self._root,
self._parts[:-1] + [name])
 
-def relative_to(self, *other):
+def relative_to(self, *other, strict=True):
 """Return the relative path to another path identified by the passed
 arguments.  If the operation is not possible (because this is not
-a subpath of the other path), raise ValueError.
+related to the other path), raise ValueError.
 """
 # For the purpose of this method, drive and root are considered
 # separate parts, i.e.:
@@ -918,14 +918,31 @@ class PurePath(object):
 to_abs_parts = [to_drv, to_root] + to_parts[1:]
 else:
 to_abs_parts = to_parts
+
 n = len(to_abs_parts)
 cf = self._flavour.casefold_parts
-if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
+common = 0
+for p, tp in zip(cf(abs_parts), cf(to_abs_parts)):
+if p != tp:
+break
+common += 1
+
+if strict:
+failure = (root or drv) if n == 0 else common != n
+error_message = "{!r} does not start with {!r}"
+up_parts = []
+else:
+failure = root != to_root
+if drv or to_drv:
+failure = cf([drv]) != cf([to_drv]) or (failure and n > 1)
+error_message = "{!r} is not related to {!r}"
+up_parts = (n-common)*['..']
+
+if failure:
 formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
-raise ValueError("{!r} does not start with {!r}"
- .format(str(self), str(formatted)))
-return self._from_parsed_parts('', root if n == 1 else '',
-   abs_parts[n:])
+raise ValueError(error_message.format(str(self), str(formatted)))
+return self._from_parsed_parts('', root if common == 1 else '',
+   up_parts+abs_parts[common:])
 
 def is_relative_to(self, *other):
 """Return True if the path is relative to another path or False.
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 1589282..a6d8fe4 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -613,13 +613,30 @@ class _BasePurePathTest(object):
 self.assertEqual(p.relative_to('a/'), P('b'))
 self.assertEqual(p.relative_to(P('a/b')), P())
 self.assertEqual(p.relative_to('a/b'), P())
+self.assertEqual(p.relative_to(P(), strict=False), P('a/b'))
+self.assertEqual(p.relative_to('', strict=False), P('a/b'))
+self.assertEqual(p.relative_to(P('a'), strict=False), P('b'))
+self.assertEqual(p.relative_to('a', strict=False), P('b'))
+self.assertEqual(p.relative_to('a/', strict=False), P('b'))
+self.assertEqual(p.relative_to(P('a/b'), strict=False), P())
+self.assertEqual(p.relative_to('a/b', strict=False), P())
+self.assertEqual(p.relative_to(P('a/c'), strict=False), P('../b'))
+self.assertEqual(p.relative_to('a/c', strict=False), P('../b'))
+self.assertEqual(p.relative_to(P('a/b/c'), strict=False), P('..'))
+self.assertEqual(p.relative_to('a/b/c', strict=False), P('..'))
+self.assertEqual(p.relative_to(P('c'), strict=False), P('../a/b'))
+self.assertEqual(p.relative_to('c', strict=False), P('../a/b'))
 # With several args.
 self.assertEqual(p.relative_to('a', 'b'), P())
+self.assertEqual(p.relative_to('a', 'b', strict=False), P())
 # Unrelated paths.
 self.assertRaises(ValueError, p.relative_to, P('c'))
 self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
 self.assertRaises(ValueError, p.relative_to, P('a/c'))
 self.assertRaises(ValueError, p.relative_to, P('/a'))
+self.assertRaises(Valu

[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-27 Thread Domenico Ragusa


Change by Domenico Ragusa :


Removed file: https://bugs.python.org/file49081/pathlib.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-22 Thread Eryk Sun


Eryk Sun  added the comment:

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.

--
nosy: +eryksun

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-22 Thread Domenico Ragusa


Domenico Ragusa  added the comment:

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 
wrote:

>
> Antoine Pitrou  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 
> 
> ___
>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-22 Thread Antoine Pitrou


Antoine Pitrou  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-21 Thread Bowie Chen


Change by Bowie Chen :


--
nosy: +bowiechen

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-21 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +pitrou

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40358] pathlib's relative_to should behave like os.path.relpath

2020-04-21 Thread Domenico Ragusa


New submission from Domenico Ragusa :

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.

--
components: Library (Lib)
files: pathlib.diff
keywords: patch
messages: 366958
nosy: Domenico Ragusa
priority: normal
severity: normal
status: open
title: pathlib's relative_to should behave like os.path.relpath
type: enhancement
versions: Python 3.9
Added file: https://bugs.python.org/file49081/pathlib.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com