classification
Title: Calling super().__init__ in subclasses of typing.Protocol raises RecursionError
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jelle Zijlstra, eric.smith, hauntsaninja, kj, lukasz.langa, miss-islington, pablogsal, uriyyo
Priority: release blocker Keywords: patch

Created on 2021-09-06 22:05 by hauntsaninja, last changed 2021-09-08 15:06 by lukasz.langa.

Pull Requests
URL Status Linked Edit
PR 28206 merged uriyyo, 2021-09-07 14:15
PR 28232 merged miss-islington, 2021-09-08 10:25
PR 28233 merged kj, 2021-09-08 10:35
Messages (9)
msg401184 - (view) Author: Shantanu (hauntsaninja) * Date: 2021-09-06 22:05
Consider:
```
from typing import Protocol

class P(Protocol):
    ...

class C(P):
    def __init__(self):
        super().__init__()

C()
```
This code passes without error on 3.9.6.

With 3.9.7, we get:
```
Traceback (most recent call last):
  File "/Users/shantanu/dev/test.py", line 10, in <module>
    C()
  File "/Users/shantanu/dev/test.py", line 8, in __init__
    super().__init__()
  File "/Users/shantanu/.pyenv/versions/3.9.7/lib/python3.9/typing.py", line 1083, in _no_init
    raise TypeError('Protocols cannot be instantiated')
TypeError: Protocols cannot be instantiated
```

I bisected this to:

bpo-44806: Fix __init__ in subclasses of protocols (GH-27545) (GH-27559)

Note there is also an interaction with the later commit:

bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121) (GH-28132)

This later commit actually causes a RecursionError:
```
  File "/Users/shantanu/dev/cpython/Lib/typing.py", line 1103, in _no_init_or_replace_init
    cls.__init__(self, *args, **kwargs)
  File "/Users/shantanu/dev/test.py", line 8, in __init__
    super().__init__()
  File "/Users/shantanu/dev/cpython/Lib/typing.py", line 1103, in _no_init_or_replace_init
    cls.__init__(self, *args, **kwargs)
  File "/Users/shantanu/dev/test.py", line 8, in __init__
    super().__init__()
```

Originally reported by @tyralla on Gitter.
msg401197 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-07 00:41
Possibly related to issue 45081, which ended up not having anything to do with dataclasses (despite the title).

Can you check if that fix solves your problem?
msg401199 - (view) Author: Shantanu (hauntsaninja) * Date: 2021-09-07 01:24
As I mentioned in the post, bpo-45081 actually makes this issue worse, since we get a RecursionError.

I think `TypeError: Protocols cannot be instantiated` is probably okay behaviour (as opposed to RecursionError), more just unfortunate that it seems to be an unanticipated breaking change.
msg401200 - (view) Author: Shantanu (hauntsaninja) * Date: 2021-09-07 01:27
Sorry if my message was confusing. Hopefully the following makes things clear:

3.9.6: snippet runs without error

3.9.7, with bpo-44806: (a probably reasonable) TypeError, but a breaking change

main, with bpo-45081: RecursionError
msg401284 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-07 14:18
Sorry I didn't notice your reference to issue 45081.
msg401369 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-09-08 10:25
New changeset c11956a8bddd75f02ccc7b4da7e4d8123e1f3c5f by Yurii Karabas in branch 'main':
bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206)
https://github.com/python/cpython/commit/c11956a8bddd75f02ccc7b4da7e4d8123e1f3c5f
msg401384 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-08 15:02
New changeset 99506dcbbe9fb56ceabe55f0a4333e5981b72095 by Ken Jin in branch '3.9':
[3.9] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28233)
https://github.com/python/cpython/commit/99506dcbbe9fb56ceabe55f0a4333e5981b72095
msg401385 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-08 15:05
New changeset c081649e6df55203178a44d16bc4c96f9fa2c6a4 by Miss Islington (bot) in branch '3.10':
bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28232)
https://github.com/python/cpython/commit/c081649e6df55203178a44d16bc4c96f9fa2c6a4
msg401386 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-08 15:06
Pablo, marking as release blocker. PR GH-28232 is merged to 3.10. It should be cherry-picked for 3.10.0 inclusion.
History
Date User Action Args
2021-09-08 15:06:58lukasz.langasetpriority: critical -> release blocker
nosy: + pablogsal
messages: + msg401386

2021-09-08 15:05:31lukasz.langasetmessages: + msg401385
2021-09-08 15:02:00lukasz.langasetmessages: + msg401384
2021-09-08 10:35:56kjsetpull_requests: + pull_request26653
2021-09-08 10:25:32miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26652
2021-09-08 10:25:28kjsetmessages: + msg401369
2021-09-07 16:00:22kjsetpriority: normal -> critical
title: Regression in 3.9.7 with typing.Protocol -> Calling super().__init__ in subclasses of typing.Protocol raises RecursionError
versions: + Python 3.10, Python 3.11
2021-09-07 15:53:11Jelle Zijlstrasetnosy: + Jelle Zijlstra, kj
2021-09-07 14:18:02eric.smithsetmessages: + msg401284
2021-09-07 14:15:20uriyyosetkeywords: + patch
nosy: + uriyyo

pull_requests: + pull_request26630
stage: patch review
2021-09-07 01:27:22hauntsaninjasetmessages: + msg401200
2021-09-07 01:24:08hauntsaninjasetmessages: + msg401199
2021-09-07 00:41:05eric.smithsetnosy: + eric.smith
messages: + msg401197
2021-09-06 22:06:45hauntsaninjasetnosy: + lukasz.langa
2021-09-06 22:05:20hauntsaninjacreate