https://github.com/python/cpython/commit/d71da0feda679bea2d291ed59f121a4cdd38b118
commit: d71da0feda679bea2d291ed59f121a4cdd38b118
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2024-11-06T22:37:37Z
summary:

[3.12] gh-126451: Register contextvars.Context to collections.abc.Mapping 
(GH-126452) (#126519)

gh-126451: Register contextvars.Context to collections.abc.Mapping (GH-126452)
(cherry picked from commit 5dc36dc5658f6ba9cfd9d7a2771baaf17d2ee23a)

Co-authored-by: Stephen Morton <[email protected]>
Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-11-05-11-28-45.gh-issue-126451.XJMtqz.rst
M Lib/contextvars.py
M Lib/test/test_context.py

diff --git a/Lib/contextvars.py b/Lib/contextvars.py
index d78c80dfe6f99c..14514f185e069d 100644
--- a/Lib/contextvars.py
+++ b/Lib/contextvars.py
@@ -1,4 +1,8 @@
+import _collections_abc
 from _contextvars import Context, ContextVar, Token, copy_context
 
 
 __all__ = ('Context', 'ContextVar', 'Token', 'copy_context')
+
+
+_collections_abc.Mapping.register(Context)
diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py
index dc6856509a40a0..d9e1c6214e7057 100644
--- a/Lib/test/test_context.py
+++ b/Lib/test/test_context.py
@@ -1,3 +1,4 @@
+import collections.abc
 import concurrent.futures
 import contextvars
 import functools
@@ -342,6 +343,19 @@ def ctx2_fun():
 
         ctx1.run(ctx1_fun)
 
+    def test_context_isinstance(self):
+        ctx = contextvars.Context()
+        self.assertIsInstance(ctx, collections.abc.Mapping)
+        self.assertTrue(issubclass(contextvars.Context, 
collections.abc.Mapping))
+
+        mapping_methods = (
+            '__contains__', '__eq__', '__getitem__', '__iter__', '__len__',
+            '__ne__', 'get', 'items', 'keys', 'values',
+        )
+        for name in mapping_methods:
+            with self.subTest(name=name):
+                self.assertTrue(callable(getattr(ctx, name)))
+
     @isolated_context
     @threading_helper.requires_working_threading()
     def test_context_threads_1(self):
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-05-11-28-45.gh-issue-126451.XJMtqz.rst 
b/Misc/NEWS.d/next/Library/2024-11-05-11-28-45.gh-issue-126451.XJMtqz.rst
new file mode 100644
index 00000000000000..563cb2515eca60
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-05-11-28-45.gh-issue-126451.XJMtqz.rst
@@ -0,0 +1,2 @@
+Register the :class:`contextvars.Context` type to
+:class:`collections.abc.Mapping`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to