]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile (GH-17560)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Mon, 30 Dec 2019 16:08:08 +0000 (19:08 +0300)
committerIvan Levkivskyi <levkivskyi@gmail.com>
Mon, 30 Dec 2019 16:08:08 +0000 (16:08 +0000)
Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst [new file with mode: 0644]

index 62875540f8b92041446e7213d6debdf8fbee0fc6..448163f04380d575213fa888634f24750357f7df 100644 (file)
@@ -643,6 +643,18 @@ class SpooledTemporaryFile:
                                    'encoding': encoding, 'newline': newline,
                                    'dir': dir, 'errors': errors}
 
+    def __class_getitem__(cls, type):
+        """Provide minimal support for using this class as generic
+        (for example in type annotations).
+
+        See PEP 484 and PEP 560 for more details. For example,
+        `SpooledTemporaryFile[str]` is a valid expression at runtime (type
+        argument `str` indicates whether the file is open in bytes or text
+        mode). Note, no type checking happens at runtime, but a static type
+        checker can be used.
+        """
+        return cls
+
     def _check(self, file):
         if self._rolled: return
         max_size = self._max_size
index 232c5dae10fdf494be5e84f41cc110e70f82b0ed..5fe9506b0b7ba1fd23a818976e10b3f807836839 100644 (file)
@@ -1229,6 +1229,9 @@ class TestSpooledTemporaryFile(BaseTestCase):
         self.assertTrue(f._rolled)
         self.assertEqual(os.fstat(f.fileno()).st_size, 20)
 
+    def test_class_getitem(self):
+        self.assertIs(tempfile.SpooledTemporaryFile[bytes],
+                      tempfile.SpooledTemporaryFile)
 
 if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
 
diff --git a/Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst b/Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst
new file mode 100644 (file)
index 0000000..7bdf291
--- /dev/null
@@ -0,0 +1 @@
+Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`.