[New-bugs-announce] [issue45454] Unable to explicitly subclass protocol when subclass has mixin requiring init

Chris Meyer report at bugs.python.org
Tue Oct 12 22:09:30 EDT 2021


New submission from Chris Meyer <cmeyer1969 at gmail.com>:

If I make a explicit subclass of a protocol that also inherits from a mixin and calls super() in order to initialize the mixin, I get the "Protocols cannot be instantiated" exception.

This case arises when having a hierarchy of both protocols and concrete classes that implement the protocols.

A simple example is:

import typing


class P(typing.Protocol):
    def m1(self) -> None: ...

class M:
    def __init__(self) -> None:
        super().__init__()
        self.o = True

class C(M, P):
    def __init__(self) -> None:
        super().__init__()
        self.op = True

    def m1(self) -> None:
        pass

c = C()

I can resolve this in particular cases by not invoking super in the mixin or putting a special no-super class in the hierarchy. However, that is not a general solution and once the class hierarchy gets more complicated, it fails to work.

Am I missing any known solution to this issue?

----------
components: Library (Lib)
messages: 403782
nosy: cmeyer
priority: normal
severity: normal
status: open
title: Unable to explicitly subclass protocol when subclass has mixin requiring init
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list