]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-119113: Raise `TypeError` from `pathlib.PurePath.with_suffix(None)` (#119124)
authorBarney Gale <barney.gale@gmail.com>
Sun, 19 May 2024 16:04:56 +0000 (17:04 +0100)
committerGitHub <noreply@github.com>
Sun, 19 May 2024 16:04:56 +0000 (17:04 +0100)
Restore behaviour from 3.12 when `path.with_suffix(None)` is called.

Lib/pathlib/_abc.py
Lib/test/test_pathlib/test_pathlib_abc.py
Misc/NEWS.d/next/Library/2024-05-17-17-32-12.gh-issue-119113.kEv1Ll.rst [new file with mode: 0644]

index 568a17df26fc336dd6f1ba106db7e45069494a11..3cdbb735096edb7369d675ae83147c8bb8901b6e 100644 (file)
@@ -205,15 +205,13 @@ class PurePathBase:
         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
index aadecbc142cca6f4f181fee211ce94fa7a7f4f83..d9e51c0e3d6411ca896a852fc853b5a35ac0b471 100644 (file)
@@ -999,6 +999,7 @@ class DummyPurePathTest(unittest.TestCase):
         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
@@ -1006,7 +1007,7 @@ class DummyPurePathTest(unittest.TestCase):
         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')
@@ -1017,6 +1018,7 @@ class DummyPurePathTest(unittest.TestCase):
         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
diff --git a/Misc/NEWS.d/next/Library/2024-05-17-17-32-12.gh-issue-119113.kEv1Ll.rst b/Misc/NEWS.d/next/Library/2024-05-17-17-32-12.gh-issue-119113.kEv1Ll.rst
new file mode 100644 (file)
index 0000000..195be06
--- /dev/null
@@ -0,0 +1,2 @@
+Fix issue where :meth:`pathlib.PurePath.with_suffix` didn't raise
+:exc:`TypeError` when given ``None`` as a suffix.