summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-05-07 14:16:05 (GMT)
committerGitHub <noreply@github.com>2024-05-07 14:16:05 (GMT)
commit71080b8a0fe5da46fb97659060db76fd95a3fb61 (patch)
tree3551c62ad58d442e3764bd5d73d3d9ad60240a7c /Lib
parenta855f824a2f5a310ffa58a973a8fe9feaa2500b3 (diff)
downloadcpython-71080b8a0fe5da46fb97659060db76fd95a3fb61.zip
cpython-71080b8a0fe5da46fb97659060db76fd95a3fb61.tar.gz
cpython-71080b8a0fe5da46fb97659060db76fd95a3fb61.tar.bz2
gh-118660: Add second type parameter to (Async)ContextManager (#118681)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_typing.py23
-rw-r--r--Lib/typing.py2
2 files changed, 20 insertions, 5 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 0126133..bd116bb 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -7511,6 +7511,15 @@ class OtherABCTests(BaseTestCase):
self.assertIsInstance(cm, typing.ContextManager)
self.assertNotIsInstance(42, typing.ContextManager)
+ def test_contextmanager_type_params(self):
+ cm1 = typing.ContextManager[int]
+ self.assertEqual(get_args(cm1), (int, bool | None))
+ cm2 = typing.ContextManager[int, None]
+ self.assertEqual(get_args(cm2), (int, types.NoneType))
+
+ type gen_cm[T1, T2] = typing.ContextManager[T1, T2]
+ self.assertEqual(get_args(gen_cm.__value__[int, None]), (int, types.NoneType))
+
def test_async_contextmanager(self):
class NotACM:
pass
@@ -7522,11 +7531,17 @@ class OtherABCTests(BaseTestCase):
cm = manager()
self.assertNotIsInstance(cm, typing.AsyncContextManager)
- self.assertEqual(typing.AsyncContextManager[int].__args__, (int,))
+ self.assertEqual(typing.AsyncContextManager[int].__args__, (int, bool | None))
with self.assertRaises(TypeError):
isinstance(42, typing.AsyncContextManager[int])
with self.assertRaises(TypeError):
- typing.AsyncContextManager[int, str]
+ typing.AsyncContextManager[int, str, float]
+
+ def test_asynccontextmanager_type_params(self):
+ cm1 = typing.AsyncContextManager[int]
+ self.assertEqual(get_args(cm1), (int, bool | None))
+ cm2 = typing.AsyncContextManager[int, None]
+ self.assertEqual(get_args(cm2), (int, types.NoneType))
class TypeTests(BaseTestCase):
@@ -9953,7 +9968,7 @@ class SpecialAttrsTests(BaseTestCase):
typing.ValuesView: 'ValuesView',
# Subscribed ABC classes
typing.AbstractSet[Any]: 'AbstractSet',
- typing.AsyncContextManager[Any]: 'AsyncContextManager',
+ typing.AsyncContextManager[Any, Any]: 'AsyncContextManager',
typing.AsyncGenerator[Any, Any]: 'AsyncGenerator',
typing.AsyncIterable[Any]: 'AsyncIterable',
typing.AsyncIterator[Any]: 'AsyncIterator',
@@ -9963,7 +9978,7 @@ class SpecialAttrsTests(BaseTestCase):
typing.ChainMap[Any, Any]: 'ChainMap',
typing.Collection[Any]: 'Collection',
typing.Container[Any]: 'Container',
- typing.ContextManager[Any]: 'ContextManager',
+ typing.ContextManager[Any, Any]: 'ContextManager',
typing.Coroutine[Any, Any, Any]: 'Coroutine',
typing.Counter[Any]: 'Counter',
typing.DefaultDict[Any, Any]: 'DefaultDict',
diff --git a/Lib/typing.py b/Lib/typing.py
index e485836..8e61f50 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -3783,7 +3783,7 @@ def __getattr__(attr):
obj = _alias(getattr(re, attr), 1)
elif attr in {"ContextManager", "AsyncContextManager"}:
import contextlib
- obj = _alias(getattr(contextlib, f"Abstract{attr}"), 1, name=attr)
+ obj = _alias(getattr(contextlib, f"Abstract{attr}"), 2, name=attr, defaults=(bool | None,))
else:
raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")
globals()[attr] = obj