Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] GH-119113: Raise TypeError from pathlib.PurePath.with_suffix(None) (GH-119124) #119183

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,13 @@ def with_suffix(self, suffix):
string, remove the suffix from the path.
"""
stem = self.stem
if not suffix:
return self.with_name(stem)
elif not stem:
if not stem:
# If the stem is empty, we can't make the suffix non-empty.
raise ValueError(f"{self!r} has an empty name")
elif suffix.startswith('.') and len(suffix) > 1:
return self.with_name(stem + suffix)
else:
elif suffix and not (suffix.startswith('.') and len(suffix) > 1):
raise ValueError(f"Invalid suffix {suffix!r}")
else:
return self.with_name(stem + suffix)

def relative_to(self, other, *, walk_up=False):
"""Return the relative path to another path identified by the passed
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,14 +999,15 @@ def test_with_suffix_windows(self):
self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c\\d')
self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c/d')
self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c\\d')
self.assertRaises(TypeError, P('c:a/b').with_suffix, None)

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

def test_with_suffix_seps(self):
def test_with_suffix_invalid(self):
P = self.cls
# Invalid suffix.
self.assertRaises(ValueError, P('a/b').with_suffix, 'gz')
Expand All @@ -1017,6 +1018,7 @@ def test_with_suffix_seps(self):
self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d')
self.assertRaises(ValueError, P('a/b').with_suffix, './.d')
self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.')
self.assertRaises(TypeError, P('a/b').with_suffix, None)

def test_relative_to_common(self):
P = self.cls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where :meth:`pathlib.PurePath.with_suffix` didn't raise
:exc:`TypeError` when given ``None`` as a suffix.
Loading