Skip to content

Commit 09c482f

Browse files
isidenticalilevkivskyi
authored andcommitted
bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile (GH-17560)
1 parent 4dc5a9d commit 09c482f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/tempfile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,18 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1,
643643
'encoding': encoding, 'newline': newline,
644644
'dir': dir, 'errors': errors}
645645

646+
def __class_getitem__(cls, type):
647+
"""Provide minimal support for using this class as generic
648+
(for example in type annotations).
649+
650+
See PEP 484 and PEP 560 for more details. For example,
651+
`SpooledTemporaryFile[str]` is a valid expression at runtime (type
652+
argument `str` indicates whether the file is open in bytes or text
653+
mode). Note, no type checking happens at runtime, but a static type
654+
checker can be used.
655+
"""
656+
return cls
657+
646658
def _check(self, file):
647659
if self._rolled: return
648660
max_size = self._max_size

Lib/test/test_tempfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ def test_truncate_with_size_parameter(self):
12291229
self.assertTrue(f._rolled)
12301230
self.assertEqual(os.fstat(f.fileno()).st_size, 20)
12311231

1232+
def test_class_getitem(self):
1233+
self.assertIs(tempfile.SpooledTemporaryFile[bytes],
1234+
tempfile.SpooledTemporaryFile)
12321235

12331236
if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
12341237

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`.

0 commit comments

Comments
 (0)