[Python-checkins] bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile (GH-17560)
Ivan Levkivskyi
webhook-mailer at python.org
Mon Dec 30 11:08:16 EST 2019
https://github.com/python/cpython/commit/09c482fad11c769be38b2449f1056e264b701bb7
commit: 09c482fad11c769be38b2449f1056e264b701bb7
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Ivan Levkivskyi <levkivskyi at gmail.com>
date: 2019-12-30T16:08:08Z
summary:
bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile (GH-17560)
files:
A Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst
M Lib/tempfile.py
M Lib/test/test_tempfile.py
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 62875540f8b92..448163f04380d 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -643,6 +643,18 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1,
'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
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 232c5dae10fdf..5fe9506b0b7ba 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -1229,6 +1229,9 @@ def test_truncate_with_size_parameter(self):
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
index 0000000000000..7bdf291950f82
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst
@@ -0,0 +1 @@
+Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`.
More information about the Python-checkins
mailing list