8000 gh-126451: Register contextvars.Context to collections.abc.Mapping (#… · python/cpython@5dc36dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dc36dc

Browse files
tungolsobolevnAlexWaygoodZeroIntensity
authored
gh-126451: Register contextvars.Context to collections.abc.Mapping (#126452)
Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent b908295 commit 5dc36dc

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/contextvars.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import _collections_abc
12
from _contextvars import Context, ContextVar, Token, copy_context
23

34

45
__all__ = ('Context', 'ContextVar', 'Token', 'copy_context')
6+
7+
8+
_collections_abc.Mapping.register(Context)

Lib/test/test_context.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections.abc
12
import concurrent.futures
23
import contextvars
34
import functools
@@ -350,6 +351,19 @@ def ctx2_fun():
350351

351352
ctx1.run(ctx1_fun)
352353

354+
def test_context_isinstance(self):
355+
ctx = contextvars.Context()
356+
self.assertIsInstance(ctx, collections.abc.Mapping)
357+
self.assertTrue(issubclass(contextvars.Context, collections.abc.Mapping))
358+
359+
mapping_methods = (
360+
'__contains__', '__eq__', '__getitem__', '__iter__', '__len__',
361+
'__ne__', 'get', 'items', 'keys', 'values',
362+
)
363+
for name in mapping_methods:
364+
with self.subTest(name=name):
365+
self.assertTrue(callable(getattr(ctx, name)))
366+
353367
@isolated_context
354368
@threading_helper.requires_working_threading()
355369
def test_context_threads_1(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Register the :class:`contextvars.Context` type to
2+
:class:`collections.abc.Mapping`.

0 commit comments

Comments
 (0)
0