Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
isidentical marked this conversation as resolved.

def _check(self, file):
if self._rolled: return
max_size = self._max_size
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`.