bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile (GH-17560)
This commit is contained in:
parent
4dc5a9df59
commit
09c482fad1
|
@ -643,6 +643,18 @@ class SpooledTemporaryFile:
|
||||||
'encoding': encoding, 'newline': newline,
|
'encoding': encoding, 'newline': newline,
|
||||||
'dir': dir, 'errors': errors}
|
'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):
|
def _check(self, file):
|
||||||
if self._rolled: return
|
if self._rolled: return
|
||||||
max_size = self._max_size
|
max_size = self._max_size
|
||||||
|
|
|
@ -1229,6 +1229,9 @@ class TestSpooledTemporaryFile(BaseTestCase):
|
||||||
self.assertTrue(f._rolled)
|
self.assertTrue(f._rolled)
|
||||||
self.assertEqual(os.fstat(f.fileno()).st_size, 20)
|
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:
|
if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`.
|
Loading…
Reference in New Issue