From 6eea0a1904dafe3c28c61aa1bf05b6cc097fd3ee Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 11 Nov 2024 13:24:53 -0800 Subject: [PATCH 1/2] allow AsyncIterator to be used as a base for Protocols --- Lib/test/test_typing.py | 3 +++ Lib/typing.py | 3 ++- .../Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 2f1f9e86a0bce4..244ce1e5da9bd2 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4255,6 +4255,9 @@ class CustomProtocol(TestCase, Protocol): class CustomContextManager(typing.ContextManager, Protocol): pass + class CustomAsyncIterator(typing.AsyncIterator, Protocol): + pass + def test_non_runtime_protocol_isinstance_check(self): class P(Protocol): x: int diff --git a/Lib/typing.py b/Lib/typing.py index c924c767042552..8e6381033fd28e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1940,7 +1940,8 @@ def _allow_reckless_class_checks(depth=2): _PROTO_ALLOWLIST = { 'collections.abc': [ 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable', - 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer', + 'AsyncIterator', 'Hashable', 'Sized', 'Container', 'Collection', + 'Reversible', 'Buffer', ], 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'], } diff --git a/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst b/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst new file mode 100644 index 00000000000000..af776d7413317a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst @@ -0,0 +1 @@ +Allow :class:`AsyncIterator` to be a base for Protocols. From 7c82b926251f80b32903abf90f145739e7caea59 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 11 Nov 2024 14:04:57 -0800 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst Co-authored-by: Jelle Zijlstra --- .../next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst b/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst index af776d7413317a..9741294487d716 100644 --- a/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst +++ b/Misc/NEWS.d/next/Library/2024-11-11-13-24-22.gh-issue-126699.ONGbMd.rst @@ -1 +1 @@ -Allow :class:`AsyncIterator` to be a base for Protocols. +Allow :class:`collections.abc.AsyncIterator` to be a base for Protocols.