Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-78707: deprecate passing >1 argument to PurePath.[is_]relative_to()
This brings `relative_to()` and `is_relative_to()` more in line with other
pathlib methods like `rename()` and `symlink_to()`.
  • Loading branch information
barneygale committed Jun 30, 2022
commit 594bd8f246947574799d870fabd6f1d8020b14a5
4 changes: 2 additions & 2 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ Pure paths provide the following methods and properties:
True


.. method:: PurePath.is_relative_to(*other)
.. method:: PurePath.is_relative_to(other)
Comment thread
barneygale marked this conversation as resolved.

Return whether or not this path is relative to the *other* path.

Expand Down Expand Up @@ -564,7 +564,7 @@ Pure paths provide the following methods and properties:
True


.. method:: PurePath.relative_to(*other)
.. method:: PurePath.relative_to(other)

Compute a version of this path relative to the path represented by
*other*. If it's impossible, ValueError is raised::
Expand Down
21 changes: 15 additions & 6 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def with_suffix(self, suffix):
return self._from_parsed_parts(self._drv, self._root,
self._parts[:-1] + [name])

def relative_to(self, *other):
def relative_to(self, other, *args):
Comment thread
barneygale marked this conversation as resolved.
Outdated
"""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.
Expand All @@ -649,16 +649,19 @@ def relative_to(self, *other):
# separate parts, i.e.:
# Path('c:/').relative_to('c:') gives Path('/')
# Path('c:/').relative_to('/') raise ValueError
if not other:
raise TypeError("need at least one argument")
if args:
warnings.warn("support for supplying more than one argument to "
"pathlib.PurePath.relative_to() is deprecated and "
"scheduled for removal in Python 3.14.",
DeprecationWarning, stacklevel=2)
parts = self._parts
drv = self._drv
root = self._root
if root:
abs_parts = [drv, root] + parts[1:]
else:
abs_parts = parts
to_drv, to_root, to_parts = self._parse_args(other)
to_drv, to_root, to_parts = self._parse_args((other,) + args)
if to_root:
to_abs_parts = [to_drv, to_root] + to_parts[1:]
else:
Expand All @@ -673,11 +676,17 @@ def relative_to(self, *other):
return self._from_parsed_parts('', root if n == 1 else '',
abs_parts[n:])

def is_relative_to(self, *other):
def is_relative_to(self, other, *args):
"""Return True if the path is relative to another path or False.
"""
if args:
warnings.warn("support for supplying more than one argument to "
"pathlib.PurePath.is_relative_to() is deprecated "
"and scheduled for removal in Python 3.14.",
DeprecationWarning, stacklevel=2)
Comment thread
barneygale marked this conversation as resolved.
Outdated
other = self._from_parts((other,) + args)
try:
self.relative_to(*other)
self.relative_to(other)
return True
except ValueError:
return False
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ def test_relative_to_common(self):
self.assertEqual(p.relative_to(P('a/b')), P())
self.assertEqual(p.relative_to('a/b'), P())
# With several args.
self.assertEqual(p.relative_to('a', 'b'), P())
with self.assertWarns(DeprecationWarning):
p.relative_to('a', 'b')
# Unrelated paths.
self.assertRaises(ValueError, p.relative_to, P('c'))
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
Expand Down Expand Up @@ -671,7 +672,8 @@ def test_is_relative_to_common(self):
self.assertTrue(p.is_relative_to(P('a/b')))
self.assertTrue(p.is_relative_to('a/b'))
# With several args.
self.assertTrue(p.is_relative_to('a', 'b'))
with self.assertWarns(DeprecationWarning):
p.is_relative_to('a', 'b')
# Unrelated paths.
self.assertFalse(p.is_relative_to(P('c')))
self.assertFalse(p.is_relative_to(P('a/b/c')))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecate passing more than one argument to
:meth:`pathlib.PurePath.relative_to` and
:meth:`~pathlib.PurePath.is_relative_to`.