From b2fddc123952bea880f44fef11af8c6108f8f96c Mon Sep 17 00:00:00 2001 From: isidentical Date: Tue, 10 Dec 2019 21:28:10 +0300 Subject: [PATCH 1/3] bpo-39019: Implement missing __class_getitem__ for http.cookies.Morsel --- Lib/http/cookies.py | 3 +++ Lib/test/test_http_cookies.py | 3 +++ .../next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst | 1 + 3 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 6694f5478bdadf..cb3742ca8920b5 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -293,6 +293,9 @@ def __init__(self): for key in self._reserved: dict.__setitem__(self, key, "") + def __class_getitem__(cls, type): + return cls + @property def key(self): return self._key diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 6072c7e15e92be..d36b163c60d02e 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -479,6 +479,9 @@ def test_repr(self): r'Set-Cookie: key=coded_val; ' r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+') + def test_class_getitem(self): + self.assertIs(cookies.Morsel[str], cookies.Morsel) + def test_main(): run_unittest(CookieTests, MorselTests) run_doctest(cookies) diff --git a/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst b/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst new file mode 100644 index 00000000000000..b6af2ae27a2ee6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst @@ -0,0 +1 @@ +Implement ``__class_getitem__`` for ``http.cookies.Morsel``. From 73d925730d7d1563efce5e9a8971bd8e018a4a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= <47358913+isidentical@users.noreply.github.com> Date: Fri, 27 Dec 2019 19:21:37 +0300 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst Co-Authored-By: Ivan Levkivskyi --- .../next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst b/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst index b6af2ae27a2ee6..ae103eb3d74514 100644 --- a/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst +++ b/Misc/NEWS.d/next/Library/2019-12-10-21-27-37.bpo-39019.7o43Gq.rst @@ -1 +1 @@ -Implement ``__class_getitem__`` for ``http.cookies.Morsel``. +Implement dummy ``__class_getitem__`` for ``http.cookies.Morsel``. From 7afd413823cd195461ebfc77bbfc3ea920206462 Mon Sep 17 00:00:00 2001 From: isidentical Date: Fri, 27 Dec 2019 21:53:29 +0300 Subject: [PATCH 3/3] clarify purpose of __class_getitems__ --- Lib/http/cookies.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index cb3742ca8920b5..35c9a329faef34 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -294,6 +294,14 @@ def __init__(self): dict.__setitem__(self, key, "") 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, `Morsel[T]` + is a valid expression at runtime (type argument `T` indicates the + type used for mode). Note, no type checking happens at runtime, but + a static type checker can be used. + """ return cls @property