Skip to content

bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile #17560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 30, 2019
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

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`.