[New-bugs-announce] [issue45121] Regression in 3.9.7 with typing.Protocol

Shantanu report at bugs.python.org
Mon Sep 6 18:05:20 EDT 2021


New submission from Shantanu <hauntsaninja at gmail.com>:

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.

----------
components: Library (Lib)
messages: 401184
nosy: hauntsaninja
priority: normal
severity: normal
status: open
title: Regression in 3.9.7 with typing.Protocol
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45121>
_______________________________________


More information about the New-bugs-announce mailing list