Skip to content

Commit

Permalink
GH-125012: Make PurePath emit FutureWarning when converting separ…
Browse files Browse the repository at this point in the history
…ators

Make `PurePosixPath(PureWindowsPath(...))` emit a `FutureWarning`
indicating that implicit conversion of backward slashes to forward slashes
will cease in a future Python release. This feature isn't documented, and
it wasn't tested until I accidentally broke it a couple of releases ago!
  • Loading branch information
barneygale committed Oct 23, 2024
1 parent 34653bb commit 5054b5f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
16 changes: 14 additions & 2 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,23 @@ def __init__(self, *args):
paths = []
for arg in args:
if isinstance(arg, PurePath):
if arg.parser is not self.parser:
if arg.parser is self.parser:
paths.extend(arg._raw_paths)
elif arg.parser is ntpath:
# GH-103631: Convert separators for backwards compatibility.
# GH-125012: This emits FutureWarning as of Python 3.14.
import warnings
msg = ("pathlib.PurePosixPath(pathlib.PureWindowsPath(...)): "
"converting backward slashes to forward slashes. "
"This will cease in a future Python release. Use "
"PurePosixPath(PureWindowsPath(...).as_posix()) to "
"explicitly convert Windows separators to POSIX "
"separators when 'casting' a Windows-flavoured "
"path object to a POSIX-flavoured path object.")
warnings.warn(msg, FutureWarning, stacklevel=2)
paths.append(arg.as_posix())
else:
paths.extend(arg._raw_paths)
paths.append(str(arg))
else:
try:
path = os.fspath(arg)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ def test_as_uri_non_ascii(self):
def test_parse_windows_path(self):
P = self.cls
p = P('c:', 'a', 'b')
pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
with self.assertWarns(FutureWarning):
pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
self.assertEqual(p, pp)

windows_equivalences = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` now emits a
:exc:`FutureWarning` indicating that implicit conversion of backward slashes
to forward slashes will cease in a future Python release. Use
``PurePosixPath(PureWindowsPath(...).as_posix())`` to explicitly convert
Windows path separators to POSIX path separators when converting between
path flavours.

0 comments on commit 5054b5f

Please sign in to comment.