Skip to content

Commit 594bd8f

Browse files
committed
pythongh-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()`.
1 parent 62bb7a3 commit 594bd8f

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

Doc/library/pathlib.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Pure paths provide the following methods and properties:
490490
True
491491

492492

493-
.. method:: PurePath.is_relative_to(*other)
493+
.. method:: PurePath.is_relative_to(other)
494494

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

@@ -564,7 +564,7 @@ Pure paths provide the following methods and properties:
564564
True
565565

566566

567-
.. method:: PurePath.relative_to(*other)
567+
.. method:: PurePath.relative_to(other)
568568

569569
Compute a version of this path relative to the path represented by
570570
*other*. If it's impossible, ValueError is raised::

Lib/pathlib.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ def with_suffix(self, suffix):
640640
return self._from_parsed_parts(self._drv, self._root,
641641
self._parts[:-1] + [name])
642642

643-
def relative_to(self, *other):
643+
def relative_to(self, other, *args):
644644
"""Return the relative path to another path identified by the passed
645645
arguments. If the operation is not possible (because this is not
646646
a subpath of the other path), raise ValueError.
@@ -649,16 +649,19 @@ def relative_to(self, *other):
649649
# separate parts, i.e.:
650650
# Path('c:/').relative_to('c:') gives Path('/')
651651
# Path('c:/').relative_to('/') raise ValueError
652-
if not other:
653-
raise TypeError("need at least one argument")
652+
if args:
653+
warnings.warn("support for supplying more than one argument to "
654+
"pathlib.PurePath.relative_to() is deprecated and "
655+
"scheduled for removal in Python 3.14.",
656+
DeprecationWarning, stacklevel=2)
654657
parts = self._parts
655658
drv = self._drv
656659
root = self._root
657660
if root:
658661
abs_parts = [drv, root] + parts[1:]
659662
else:
660663
abs_parts = parts
661-
to_drv, to_root, to_parts = self._parse_args(other)
664+
to_drv, to_root, to_parts = self._parse_args((other,) + args)
662665
if to_root:
663666
to_abs_parts = [to_drv, to_root] + to_parts[1:]
664667
else:
@@ -673,11 +676,17 @@ def relative_to(self, *other):
673676
return self._from_parsed_parts('', root if n == 1 else '',
674677
abs_parts[n:])
675678

676-
def is_relative_to(self, *other):
679+
def is_relative_to(self, other, *args):
677680
"""Return True if the path is relative to another path or False.
678681
"""
682+
if args:
683+
warnings.warn("support for supplying more than one argument to "
684+
"pathlib.PurePath.is_relative_to() is deprecated "
685+
"and scheduled for removal in Python 3.14.",
686+
DeprecationWarning, stacklevel=2)
687+
other = self._from_parts((other,) + args)
679688
try:
680-
self.relative_to(*other)
689+
self.relative_to(other)
681690
return True
682691
except ValueError:
683692
return False

Lib/test/test_pathlib.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ def test_relative_to_common(self):
637637
self.assertEqual(p.relative_to(P('a/b')), P())
638638
self.assertEqual(p.relative_to('a/b'), P())
639639
# With several args.
640-
self.assertEqual(p.relative_to('a', 'b'), P())
640+
with self.assertWarns(DeprecationWarning):
641+
p.relative_to('a', 'b')
641642
# Unrelated paths.
642643
self.assertRaises(ValueError, p.relative_to, P('c'))
643644
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
@@ -671,7 +672,8 @@ def test_is_relative_to_common(self):
671672
self.assertTrue(p.is_relative_to(P('a/b')))
672673
self.assertTrue(p.is_relative_to('a/b'))
673674
# With several args.
674-
self.assertTrue(p.is_relative_to('a', 'b'))
675+
with self.assertWarns(DeprecationWarning):
676+
p.is_relative_to('a', 'b')
675677
# Unrelated paths.
676678
self.assertFalse(p.is_relative_to(P('c')))
677679
self.assertFalse(p.is_relative_to(P('a/b/c')))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecate passing more than one argument to
2+
:meth:`pathlib.PurePath.relative_to` and
3+
:meth:`~pathlib.PurePath.is_relative_to`.

0 commit comments

Comments
 (0)