Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 27b61c1

Browse filesBrowse files
[3.13] GH-119113: Raise TypeError from pathlib.PurePath.with_suffix(None) (GH-119124) (#119183)
Restore behaviour from 3.12 when `path.with_suffix(None)` is called. (cherry picked from commit 3c28510) Co-authored-by: Barney Gale <barney.gale@gmail.com>
1 parent fdc50ba commit 27b61c1
Copy full SHA for 27b61c1

File tree

3 files changed

+9
-7
lines changed
Filter options

3 files changed

+9
-7
lines changed

‎Lib/pathlib/_abc.py

Copy file name to clipboardExpand all lines: Lib/pathlib/_abc.py
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,13 @@ def with_suffix(self, suffix):
225225
string, remove the suffix from the path.
226226
"""
227227
stem = self.stem
228-
if not suffix:
229-
return self.with_name(stem)
230-
elif not stem:
228+
if not stem:
231229
# If the stem is empty, we can't make the suffix non-empty.
232230
raise ValueError(f"{self!r} has an empty name")
233-
elif suffix.startswith('.') and len(suffix) > 1:
234-
return self.with_name(stem + suffix)
235-
else:
231+
elif suffix and not (suffix.startswith('.') and len(suffix) > 1):
236232
raise ValueError(f"Invalid suffix {suffix!r}")
233+
else:
234+
return self.with_name(stem + suffix)
237235

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

‎Lib/test/test_pathlib/test_pathlib_abc.py

Copy file name to clipboardExpand all lines: Lib/test/test_pathlib/test_pathlib_abc.py
+3-1Lines 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
+2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.