From 784a4ac8d2dd84e5d0d13a80e63432899bb218fe Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 8 Sep 2021 13:25:09 +0300 Subject: [PATCH 1/2] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) --- Lib/test/test_typing.py | 18 ++++++++++++++++++ Lib/typing.py | 5 +++++ .../2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst | 2 ++ 3 files changed, 25 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e05ac4539fb503..db03fae6bc53f3 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1459,6 +1459,24 @@ class CustomProtocol(TestCase, Protocol): class CustomContextManager(typing.ContextManager, Protocol): pass + def test_non_runtime_protocol_isinstance_check(self): + class P(Protocol): + x: int + + with self.assertRaisesRegex(TypeError, "@runtime_checkable"): + isinstance(1, P) + + def test_super_call_init(self): + class P(Protocol): + x: int + + class Foo(P): + def __init__(self): + super().__init__() + + Foo() # Previously triggered RecursionError + + class GenericTests(BaseTestCase): def test_basics(self): diff --git a/Lib/typing.py b/Lib/typing.py index 9010775c0de7e1..775f17822afc1a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1085,6 +1085,11 @@ def _no_init_or_replace_init(self, *args, **kwargs): if cls._is_protocol: raise TypeError('Protocols cannot be instantiated') + # Already using a custom `__init__`. No need to calculate correct + # `__init__` to call. This can lead to RecursionError. See bpo-45121. + if cls.__init__ is not _no_init_or_replace_init: + return + # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`. # The first instantiation of the subclass will call `_no_init_or_replace_init` which # searches for a proper new `__init__` in the MRO. The new `__init__` diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst b/Misc/NEWS.d/next/Core and Builtins/2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst new file mode 100644 index 00000000000000..19eb3314125167 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst @@ -0,0 +1,2 @@ +Fix issue where ``Protocol.__init__`` raises ``RecursionError`` when it's +called directly or via ``super()``. Patch provided by Yurii Karabas. From ab284379fa6bbaa6c8a397c72a5fbfa491c2383e Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 8 Sep 2021 18:32:27 +0800 Subject: [PATCH 2/2] remove unrelated test --- Lib/test/test_typing.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index db03fae6bc53f3..930f675c624ec4 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1459,13 +1459,6 @@ class CustomProtocol(TestCase, Protocol): class CustomContextManager(typing.ContextManager, Protocol): pass - def test_non_runtime_protocol_isinstance_check(self): - class P(Protocol): - x: int - - with self.assertRaisesRegex(TypeError, "@runtime_checkable"): - isinstance(1, P) - def test_super_call_init(self): class P(Protocol): x: int