Skip to content

Commit 5054b5f

Browse files
committed
GH-125012: Make PurePath emit FutureWarning when converting separators
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!
1 parent 34653bb commit 5054b5f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Lib/pathlib/_local.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,23 @@ def __init__(self, *args):
119119
paths = []
120120
for arg in args:
121121
if isinstance(arg, PurePath):
122-
if arg.parser is not self.parser:
122+
if arg.parser is self.parser:
123+
paths.extend(arg._raw_paths)
124+
elif arg.parser is ntpath:
123125
# GH-103631: Convert separators for backwards compatibility.
126+
# GH-125012: This emits FutureWarning as of Python 3.14.
127+
import warnings
128+
msg = ("pathlib.PurePosixPath(pathlib.PureWindowsPath(...)): "
129+
"converting backward slashes to forward slashes. "
130+
"This will cease in a future Python release. Use "
131+
"PurePosixPath(PureWindowsPath(...).as_posix()) to "
132+
"explicitly convert Windows separators to POSIX "
133+
"separators when 'casting' a Windows-flavoured "
134+
"path object to a POSIX-flavoured path object.")
135+
warnings.warn(msg, FutureWarning, stacklevel=2)
124136
paths.append(arg.as_posix())
125137
else:
126-
paths.extend(arg._raw_paths)
138+
paths.append(str(arg))
127139
else:
128140
try:
129141
path = os.fspath(arg)

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ def test_as_uri_non_ascii(self):
388388
def test_parse_windows_path(self):
389389
P = self.cls
390390
p = P('c:', 'a', 'b')
391-
pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
391+
with self.assertWarns(FutureWarning):
392+
pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
392393
self.assertEqual(p, pp)
393394

394395
windows_equivalences = {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` now emits a
2+
:exc:`FutureWarning` indicating that implicit conversion of backward slashes
3+
to forward slashes will cease in a future Python release. Use
4+
``PurePosixPath(PureWindowsPath(...).as_posix())`` to explicitly convert
5+
Windows path separators to POSIX path separators when converting between
6+
path flavours.

0 commit comments

Comments
 (0)