bpo-38979: fix ContextVar "__class_getitem__" method (GH-17497)

now contextvars.ContextVar "__class_getitem__" method returns ContextVar class, not None.

https://bugs.python.org/issue38979

Automerge-Triggered-By: @asvetlov
(cherry picked from commit 28c91631c2)

Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2019-12-08 04:49:07 -08:00 committed by GitHub
parent 9d3cacd590
commit 960fca1a58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 6 deletions

View File

@ -38,9 +38,6 @@ class ContextTest(unittest.TestCase):
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 @@ class ContextTest(unittest.TestCase):
tp.shutdown()
self.assertEqual(results, list(range(10)))
def test_contextvar_getitem(self):
clss = contextvars.ContextVar
self.assertEqual(clss[str], clss)
# HAMT Tests

View File

@ -0,0 +1 @@
Return class from ``ContextVar.__class_getitem__`` to simplify subclassing.

View File

@ -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}
};