Skip to content

Commit

Permalink
pythonGH-113568: Stop raising deprecation warnings from pathlib ABCs (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale authored and kulikjak committed Jan 22, 2024
1 parent 4fde343 commit b651780
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
27 changes: 27 additions & 0 deletions Lib/pathlib/__init__.py
Expand Up @@ -166,6 +166,33 @@ def __ge__(self, other):
return NotImplemented
return self._parts_normcase >= other._parts_normcase

def relative_to(self, other, /, *_deprecated, walk_up=False):
"""Return the relative path to another path identified by the passed
arguments. If the operation is not possible (because this is not
related to the other path), raise ValueError.
The *walk_up* parameter controls whether `..` may be used to resolve
the path.
"""
if _deprecated:
msg = ("support for supplying more than one positional argument "
"to pathlib.PurePath.relative_to() is deprecated and "
"scheduled for removal in Python 3.14")
warnings.warn(msg, DeprecationWarning, stacklevel=2)
other = self.with_segments(other, *_deprecated)
return _abc.PurePathBase.relative_to(self, other, walk_up=walk_up)

def is_relative_to(self, other, /, *_deprecated):
"""Return True if the path is relative to another path or False.
"""
if _deprecated:
msg = ("support for supplying more than one argument to "
"pathlib.PurePath.is_relative_to() is deprecated and "
"scheduled for removal in Python 3.14")
warnings.warn(msg, DeprecationWarning, stacklevel=2)
other = self.with_segments(other, *_deprecated)
return _abc.PurePathBase.is_relative_to(self, other)

def as_uri(self):
"""Return the path as a URI."""
if not self.is_absolute():
Expand Down
21 changes: 4 additions & 17 deletions Lib/pathlib/_abc.py
Expand Up @@ -2,7 +2,6 @@
import ntpath
import posixpath
import sys
import warnings
from _collections_abc import Sequence
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
from itertools import chain
Expand Down Expand Up @@ -383,21 +382,15 @@ def with_suffix(self, suffix):
else:
raise ValueError(f"Invalid suffix {suffix!r}")

def relative_to(self, other, /, *_deprecated, walk_up=False):
def relative_to(self, other, *, walk_up=False):
"""Return the relative path to another path identified by the passed
arguments. If the operation is not possible (because this is not
related to the other path), raise ValueError.
The *walk_up* parameter controls whether `..` may be used to resolve
the path.
"""
if _deprecated:
msg = ("support for supplying more than one positional argument "
"to pathlib.PurePath.relative_to() is deprecated and "
"scheduled for removal in Python 3.14")
warnings.warn(msg, DeprecationWarning, stacklevel=2)
other = self.with_segments(other, *_deprecated)
elif not isinstance(other, PurePathBase):
if not isinstance(other, PurePathBase):
other = self.with_segments(other)
for step, path in enumerate(chain([other], other.parents)):
if path == self or path in self.parents:
Expand All @@ -411,16 +404,10 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
parts = ['..'] * step + self._tail[len(path._tail):]
return self._from_parsed_parts('', '', parts)

def is_relative_to(self, other, /, *_deprecated):
def is_relative_to(self, other):
"""Return True if the path is relative to another path or False.
"""
if _deprecated:
msg = ("support for supplying more than one argument to "
"pathlib.PurePath.is_relative_to() is deprecated and "
"scheduled for removal in Python 3.14")
warnings.warn(msg, DeprecationWarning, stacklevel=2)
other = self.with_segments(other, *_deprecated)
elif not isinstance(other, PurePathBase):
if not isinstance(other, PurePathBase):
other = self.with_segments(other)
return other == self or other in self.parents

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Expand Up @@ -214,6 +214,19 @@ def test_repr_roundtrips(self):
self.assertEqual(q, p)
self.assertEqual(repr(q), r)

def test_relative_to_several_args(self):
P = self.cls
p = P('a/b')
with self.assertWarns(DeprecationWarning):
p.relative_to('a', 'b')
p.relative_to('a', 'b', walk_up=True)

def test_is_relative_to_several_args(self):
P = self.cls
p = P('a/b')
with self.assertWarns(DeprecationWarning):
p.is_relative_to('a', 'b')


class PurePosixPathTest(PurePathTest):
cls = pathlib.PurePosixPath
Expand Down
7 changes: 0 additions & 7 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Expand Up @@ -542,10 +542,6 @@ def test_relative_to_common(self):
self.assertEqual(p.relative_to('a/b/c', walk_up=True), P('..'))
self.assertEqual(p.relative_to(P('c'), walk_up=True), P('../a/b'))
self.assertEqual(p.relative_to('c', walk_up=True), P('../a/b'))
# With several args.
with self.assertWarns(DeprecationWarning):
p.relative_to('a', 'b')
p.relative_to('a', 'b', walk_up=True)
# Unrelated paths.
self.assertRaises(ValueError, p.relative_to, P('c'))
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
Expand Down Expand Up @@ -607,9 +603,6 @@ def test_is_relative_to_common(self):
self.assertTrue(p.is_relative_to('a/'))
self.assertTrue(p.is_relative_to(P('a/b')))
self.assertTrue(p.is_relative_to('a/b'))
# With several args.
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
@@ -0,0 +1,2 @@
Raise deprecation warnings from :class:`pathlib.PurePath` and not its
private base class ``PurePathBase``.

0 comments on commit b651780

Please sign in to comment.