summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-09-08 15:05:23 (GMT)
committerGitHub <noreply@github.com>2021-09-08 15:05:23 (GMT)
commitc081649e6df55203178a44d16bc4c96f9fa2c6a4 (patch)
tree6c8b3a3e6ea79d48781cf240cad0e4ba3a27990a
parent2fe15dbaad651707fb198c3477b7db77ab89ade0 (diff)
downloadcpython-c081649e6df55203178a44d16bc4c96f9fa2c6a4.zip
cpython-c081649e6df55203178a44d16bc4c96f9fa2c6a4.tar.gz
cpython-c081649e6df55203178a44d16bc4c96f9fa2c6a4.tar.bz2
bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28232)
(cherry picked from commit c11956a8bddd75f02ccc7b4da7e4d8123e1f3c5f) Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
-rw-r--r--Lib/test/test_typing.py10
-rw-r--r--Lib/typing.py5
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2021-09-07-17-10-16.bpo-45121.iG-Hsf.rst2
3 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 459af25..c84ff0f 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1604,6 +1604,16 @@ class ProtocolTests(BaseTestCase):
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):
diff --git a/Lib/typing.py b/Lib/typing.py
index 24f834e..5873d53 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1404,6 +1404,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 0000000..19eb331
--- /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.