From ea070427eb2686440db2907cd5a0061b6a31795b Mon Sep 17 00:00:00 2001 From: isidentical Date: Tue, 10 Dec 2019 21:11:40 +0300 Subject: [PATCH 1/5] bpo-39019: Implement missing __class_getitem__ for SpooledTemporaryFile --- Lib/tempfile.py | 3 +++ Lib/test/test_tempfile.py | 2 ++ .../next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst | 1 + 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 62875540f8b9204..8339a9adda325db 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -643,6 +643,9 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1, 'encoding': encoding, 'newline': newline, 'dir': dir, 'errors': errors} + def __class_getitem__(cls, type): + 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 232c5dae10fdf49..2e765096303fb52 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1229,6 +1229,8 @@ 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 000000000000000..6946e177515a316 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst @@ -0,0 +1 @@ +Implement ``__class_getitem__`` for ``tempfile.SpooledTemporaryFile``. From 81ac8a22052f18511b0b3b7287862d70076fdd92 Mon Sep 17 00:00:00 2001 From: isidentical Date: Wed, 25 Dec 2019 20:18:46 +0300 Subject: [PATCH 2/5] divide test call to 2 lines for PEP8 --- Lib/test/test_tempfile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 2e765096303fb52..5fe9506b0b7ba1f 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1230,7 +1230,8 @@ def test_truncate_with_size_parameter(self): self.assertEqual(os.fstat(f.fileno()).st_size, 20) def test_class_getitem(self): - self.assertIs(tempfile.SpooledTemporaryFile[bytes], tempfile.SpooledTemporaryFile) + self.assertIs(tempfile.SpooledTemporaryFile[bytes], + tempfile.SpooledTemporaryFile) if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: From acaf46de518f0fd077761e1d71f79ba81b75ae19 Mon Sep 17 00:00:00 2001 From: isidentical Date: Wed, 25 Dec 2019 20:21:48 +0300 Subject: [PATCH 3/5] reference with :class: --- .../next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6946e177515a316..e890634623d0a3d 100644 --- 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 @@ -1 +1 @@ -Implement ``__class_getitem__`` for ``tempfile.SpooledTemporaryFile``. +Implement ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`. From 5f6343e608c2edd50c2fd3c169d49ba09d1703a9 Mon Sep 17 00:00:00 2001 From: isidentical Date: Fri, 27 Dec 2019 21:16:07 +0300 Subject: [PATCH 4/5] clarify purpose of __class_getitems__ --- Lib/tempfile.py | 8 ++++++++ .../next/Library/2019-12-10-21-11-05.bpo-39019.YIlgZ7.rst | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 8339a9adda325db..4f2df4576363d82 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -644,6 +644,14 @@ def __init__(self, max_size=0, mode='w+b', buffering=-1, '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 the type used for mode). Note, no type + checking happens at runtime, but a static type checker can be used. + """ return cls def _check(self, file): 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 index e890634623d0a3d..7bdf291950f821d 100644 --- 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 @@ -1 +1 @@ -Implement ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`. +Implement dummy ``__class_getitem__`` for :class:`tempfile.SpooledTemporaryFile`. From d1fa25036ae7c7f7ae78b185a91579a72850a77b Mon Sep 17 00:00:00 2001 From: isidentical Date: Fri, 27 Dec 2019 22:34:05 +0300 Subject: [PATCH 5/5] clarify the mode --- Lib/tempfile.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 4f2df4576363d82..448163f04380d57 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -649,8 +649,9 @@ def __class_getitem__(cls, type): See PEP 484 and PEP 560 for more details. For example, `SpooledTemporaryFile[str]` is a valid expression at runtime (type - argument `str` indicates the type used for mode). Note, no type - checking happens at runtime, but a static type checker can be used. + 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