mirror of https://github.com/python/cpython
bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206)
This commit is contained in:
parent
d003a5bd25
commit
c11956a8bd
|
@ -1610,6 +1610,16 @@ class ProtocolTests(BaseTestCase):
|
||||||
with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
|
with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
|
||||||
isinstance(1, P)
|
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):
|
class GenericTests(BaseTestCase):
|
||||||
|
|
||||||
|
|
|
@ -1406,6 +1406,11 @@ def _no_init_or_replace_init(self, *args, **kwargs):
|
||||||
if cls._is_protocol:
|
if cls._is_protocol:
|
||||||
raise TypeError('Protocols cannot be instantiated')
|
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`.
|
# 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
|
# 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__`
|
# searches for a proper new `__init__` in the MRO. The new `__init__`
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix issue where ``Protocol.__init__`` raises ``RecursionError`` when it's
|
||||||
|
called directly or via ``super()``. Patch provided by Yurii Karabas.
|
Loading…
Reference in New Issue