8000 GH-119113: Raise `TypeError` from `pathlib.PurePath.with_suffix(None)… · python/cpython@3c28510 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c28510

Browse files
authored
GH-119113: Raise TypeError from pathlib.PurePath.with_suffix(None) (#119124)
Restore behaviour from 3.12 when `path.with_suffix(None)` is called.
1 parent 4b76671 commit 3c28510

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

Lib/pathlib/_abc.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,13 @@ def with_suffix(self, suffix):
205205
string, remove the suffix from the path.
206206
"""
207207
stem = self.stem
208-
if not suffix:
209-
return self.with_name(stem)
210-
elif not stem:
208+
if not stem:
211209
# If the stem is empty, we can't make the suffix non-empty.
212210
raise ValueError(f"{self!r} has an empty name")
213-
elif suffix.startswith('.') and len(suffix) > 1:
214-
return self.with_name(stem + suffix)
215-
else:
211+
elif suffix and not (suffix.startswith('.') and len(suffix) > 1):
216212
raise ValueError(f"Invalid suffix {suffix!r}")
213+
else:
214+
return self.with_name(stem + suffix)
217215

218216
def relative_to(self, other, *, walk_up=False):
219217
"""Return the relative path to another path identified by the passed

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,15 @@ def test_with_suffix_windows(self):
999999
self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c\\d')
10001000
self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c/d')
10011001
self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c\\d')
1002+
self.assertRaises(TypeError, P('c:a/b').with_suffix, None)
10021003

10031004
def test_with_suffix_empty(self):
10041005
P = self.cls
10051006
# Path doesn't have a "filename" component.
10061007
self.assertRaises(ValueError, P('').with_suffix, '.gz')
10071008
self.assertRaises(ValueError, P('/').with_suffix, '.gz')
10081009

1009-
def test_with_suffix_seps(self):
1010+
def test_with_suffix_invalid(self):
10101011
P = self.cls
10111012
# Invalid suffix.
10121013
self.assertRaises(ValueError, P('a/b').with_suffix, 'gz')
@@ -1017,6 +1018,7 @@ def test_with_suffix_seps(self):
10171018
self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d')
10181019
self.assertRaises(ValueError, P('a/b').with_suffix, './.d')
10191020
self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.')
1021+
self.assertRaises(TypeError, P('a/b').with_suffix, None)
10201022

10211023
def test_relative_to_common(self):
10221024
P = self.cls
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix issue where :meth:`pathlib.PurePath.with_suffix` didn't raise
2+
:exc:`TypeError` when given ``None`` as a suffix.

0 commit comments

Comments
 (0)
0