Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45121: Fix issue when Protocol.__init__ raise RecursionError #28206

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,16 @@ class P(Protocol):
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__()

uriyyo marked this conversation as resolved.
Show resolved Hide resolved
Foo()


class GenericTests(BaseTestCase):

Expand Down
6 changes: 6 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,12 @@ def _no_init_or_replace_init(self, *args, **kwargs):
if cls._is_protocol:
raise TypeError('Protocols cannot be instantiated')

# When `_no_init_or_replace_init` called using super() there are no
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
# need to calculate correct `__init__` method to call.
# 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__`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue when ``Protocol.__init__`` raise ``RecursionError`` when it called
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
using ``super()``. Patch provided by Yurii Karabas.