8000 GH-113568: Stop raising deprecation warnings from pathlib ABCs (#113757) · python/cpython@3375dfe · GitHub
[go: up one dir, main page]

Skip to content

Commit 3375dfe

Browse files
authored
GH-113568: Stop raising deprecation warnings from pathlib ABCs (#113757)
1 parent d99d871 commit 3375dfe

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

Lib/pathlib/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ def __ge__(self, other):
166166
return NotImplemented
167167
return self._parts_normcase >= other._parts_normcase
168168

169+
def relative_to(self, other, /, *_deprecated, walk_up=False):
170+
"""Return the relative path to another path identified by the passed
171+
arguments. If the operation is not possible (because this is not
172+
related to the other path), raise ValueError.
173+
174+
The *walk_up* parameter controls whether `..` may be used to resolve
175+
the path.
176+
"""
177+
if _deprecated:
178+
msg = ("support for supplying more than one positional argument "
179+
"to pathlib.PurePath.relative_to() is deprecated and "
180+
"scheduled for removal in Python 3.14")
181+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
182+
other = self.with_segments(other, *_deprecated)
183+
return _abc.PurePathBase.relative_to(self, other, walk_up=walk_up)
184+
185+
def is_relative_to(self, other, /, *_deprecated):
186+
"""Return True if the path is relative to another path or False.
187+
"""
188+
if _deprecated:
189+
msg = ("support for supplying more than one argument to "
190+
"pathlib.PurePath.is_relative_to() is deprecated and "
191+
"scheduled for removal in Python 3.14")
192+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
193+
other = self.with_segments(other, *_deprecated)
194+
return _abc.PurePathBase.is_relative_to(self, other)
195+
169196
def as_uri(self):
170197
"""Return the path as a URI."""
171198
if not self.is_absolute():

Lib/pathlib/_abc.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import ntpath
33
import posixpath
44
import sys
5-
import warnings
65
from _collections_abc import Sequence
76
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
87
from itertools import chain
@@ -383,21 +382,15 @@ def with_suffix(self, suffix):
383382
else:
384383
raise ValueError(f"Invalid suffix {suffix!r}")
385384

386-
def relative_to(self, other, /, *_deprecated, walk_up=False):
385+
def relative_to(self, other, *, walk_up=False):
387386
"""Return the relative path to another path identified by the passed
388387
arguments. If the operation is not possible (because this is not
389388
related to the other path), raise ValueError.
390389
391390
The *walk_up* parameter controls whether `..` may be used to resolve
392391
the path.
393392
"""
394-
if _deprecated:
395-
msg = ("support for supplying more than one positional argument "
396-
"to pathlib.PurePath.relative_to() is deprecated and "
397-
"scheduled for removal in Python 3.14")
398-
warnings.warn(msg, DeprecationWarning, stacklevel=2)
399-
other = self.with_segments(other, *_deprecated)
400-
elif not isinstance(other, PurePathBase):
393+
if not isinstance(other, PurePathBase):
401394
other = self.with_segments(other)
402395
for step, path in enumerate(chain([other], other.parents)):
403396
if path == self or path in self.parents:
@@ -411,16 +404,10 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
411404
parts = ['..'] * step + self._tail[len(path._tail):]
412405
return self._from_parsed_parts('', '', parts)
413406

414-
def is_relative_to(self, other, /, *_deprecated):
407+
def is_relative_to(self, other):
415408
"""Return True if the path is relative to another path or False.
416409
"""
417-
if _deprecated:
418-
msg = ("support for supplying more than one argument to "
419-
"pathlib.PurePath.is_relative_to() is deprecated and "
420-
"scheduled for removal in Python 3.14")
421-
warnings.warn(msg, DeprecationWarning, stacklevel=2)
422-
other = self.with_segments(other, *_deprecated)
423-
elif not isinstance(other, PurePathBase):
410+
if not isinstance(other, PurePathBase):
424411
other = self.with_segments(other)
425412
return other == self or other in self.parents
426413

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ def test_repr_roundtrips(self):
214214
self.assertEqual(q, p)
215215
self.assertEqual(repr(q), r)
216216

217+
def test_relative_to_several_args(self):
218+
P = self.cls
219+
p = P('a/b')
220+
with self.assertWarns(DeprecationWarning):
221+
p.relative_to('a', 'b')
222+
p.relative_to('a', 'b', walk_up=True)
223+
224+
def test_is_relative_to_several_args(self):
225+
P = self.cls
226+
p = P('a/b')
227+
with self.assertWarns(DeprecationWarning):
228+
p.is_relative_to('a', 'b')
229+
217230

218231
class PurePosixPathTest(PurePathTest):
219232
cls = pathlib.PurePosixPath
< F438 div class="DiffFileHeader-module__diff-file-header--TjXyn flex-wrap flex-sm-nowrap">

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,6 @@ def test_relative_to_common(self):
542542
self.assertEqual(p.relative_to('a/b/c', walk_up=True), P('..'))
543543
self.assertEqual(p.relative_to(P('c'), walk_up=True), P('../a/b'))
544544
self.assertEqual(p.relative_to('c', walk_up=True), P('../a/b'))
545-
# With several args.
546-
with self.assertWarns(DeprecationWarning):
547-
p.relative_to('a', 'b')
548-
p.relative_to('a', 'b', walk_up=True)
549545
# Unrelated paths.
550546
self.assertRaises(ValueError, p.relative_to, P('c'))
551547
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
@@ -607,9 +603,6 @@ def test_is_relative_to_common(self):
607603
self.assertTrue(p.is_relative_to('a/'))
608604
self.assertTrue(p.is_relative_to(P('a/b')))
609605
self.assertTrue(p.is_relative_to('a/b'))
610-
# With several args.
611-
with self.assertWarns(DeprecationWarning):
612-
p.is_relative_to('a', 'b')
613606
# Unrelated paths.
614607
self.assertFalse(p.is_relative_to(P('c')))
615608
self.assertFalse(p.is_relative_to(P('a/b/c')))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise deprecation warnings from :class:`pathlib.PurePath` and not its
2+
private base class ``PurePathBase``.

0 commit comments

Comments
 (0)
0