From 5364ad5902f703ac0a914e2eb7c46900cd2c8743 Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 15:47:46 +0330 Subject: [PATCH 1/7] bpo-38979: return class from __class_getitem__ to simplify subclassing --- Python/context.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Python/context.c b/Python/context.c index f48c376b4ffaa9..5c30e47f35dd78 100644 --- a/Python/context.c +++ b/Python/context.c @@ -1010,9 +1010,10 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token) static PyObject * -contextvar_cls_getitem(PyObject *self, PyObject *args) +contextvar_cls_getitem(PyObject *self, PyObject *arg) { - Py_RETURN_NONE; + Py_INCREF(self); + return self; } static PyMemberDef PyContextVar_members[] = { @@ -1025,7 +1026,7 @@ static PyMethodDef PyContextVar_methods[] = { _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF {"__class_getitem__", contextvar_cls_getitem, - METH_VARARGS | METH_STATIC, NULL}, + METH_O | METH_CLASS, NULL}, {NULL, NULL} }; From 50b93aed341ef8882ccfcbc989eb038d34b8b2c8 Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 16:33:37 +0330 Subject: [PATCH 2/7] add ContextVar.__class_getitem__ test --- Lib/test/test_context.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py index efd7319a23ae02..c76200010c60eb 100644 --- a/Lib/test/test_context.py +++ b/Lib/test/test_context.py @@ -361,6 +361,10 @@ def sub(num): tp.shutdown() self.assertEqual(results, list(range(10))) + def test_contextvar_getitem(self): + clss = contextvars.ContextVar + self.assertEqual(clss[str], clss) + # HAMT Tests From 26ab478779d37680fb2700c3ae7812bfabb65974 Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 16:34:20 +0330 Subject: [PATCH 3/7] add ContextVar.__class_getitem__ NEWS --- .../next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst diff --git a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst new file mode 100644 index 00000000000000..d5ab6fa1764a08 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst @@ -0,0 +1,3 @@ +return class from __class_getitem__ to simplify subclassing. E.g. `clss = +contextvars.ContextVar[str]` returned `None` instead of `` From 53fe5056f957bcea0b31a11b9004d2b74bd4f2cd Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 20:10:34 +0330 Subject: [PATCH 4/7] revert to previous commit --- Lib/test/test_context.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py index c76200010c60eb..efd7319a23ae02 100644 --- a/Lib/test/test_context.py +++ b/Lib/test/test_context.py @@ -361,10 +361,6 @@ def sub(num): tp.shutdown() self.assertEqual(results, list(range(10))) - def test_contextvar_getitem(self): - clss = contextvars.ContextVar - self.assertEqual(clss[str], clss) - # HAMT Tests From 7003848d5346bc6e5976c78862badd42f7f9098d Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 20:12:13 +0330 Subject: [PATCH 5/7] remove example --- .../next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst index d5ab6fa1764a08..61924ef8747717 100644 --- a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst +++ b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst @@ -1,3 +1 @@ -return class from __class_getitem__ to simplify subclassing. E.g. `clss = -contextvars.ContextVar[str]` returned `None` instead of `` +return class from ``__class_getitem__`` to simplify subclassing. \ No newline at end of file From d27d71c502ae65662206a7456c20117d44a22952 Mon Sep 17 00:00:00 2001 From: amir Date: Sat, 7 Dec 2019 20:16:30 +0330 Subject: [PATCH 6/7] remove test_context_var_new_2 --- Lib/test/test_context.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py index efd7319a23ae02..b9e991a4000929 100644 --- a/Lib/test/test_context.py +++ b/Lib/test/test_context.py @@ -38,9 +38,6 @@ def test_context_var_new_1(self): self.assertNotEqual(hash(c), hash('aaa')) - def test_context_var_new_2(self): - self.assertIsNone(contextvars.ContextVar[int]) - @isolated_context def test_context_var_repr_1(self): c = contextvars.ContextVar('a') @@ -361,6 +358,10 @@ def sub(num): tp.shutdown() self.assertEqual(results, list(range(10))) + def test_contextvar_getitem(self): + clss = contextvars.ContextVar + self.assertEqual(clss[str], clss) + # HAMT Tests From 938b77c454ddaf648fec402af75a654b81f74cd1 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 8 Dec 2019 13:17:05 +0200 Subject: [PATCH 7/7] Update 2019-12-07-16-32-42.bpo-38979.q0sIHy.rst --- .../next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst index 61924ef8747717..6a91a12e4930a9 100644 --- a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst +++ b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst @@ -1 +1 @@ -return class from ``__class_getitem__`` to simplify subclassing. \ No newline at end of file +Return class from ``ContextVar.__class_getitem__`` to simplify subclassing.