I’m using typing.Protocols
a lot (actually, typing_extensions.Protocol
because I try to take advantage of additional functionalities added in Python >= 3.11) and I’m a bit confused about the fact that Protocols can have implementation and child classes directly inheriting from said protocols use said implementations when no method is defined.
i.e.
from typing_extensions import Protocol, runtime_checkable
@runtime_checkable
class MyProtocol(Protocol):
def my_method(self) -> None:
...
@runtime_checkable
class InheritProto(MyProtocol, Protocol):
def my_method(self) -> None:
print("InheritProto.my_method called")
class ExampleClass(InheritProto):
...
obj = ExampleClass()
obj.my_method()
In this case, when calling my_method
, I originally suspected that the interpreter would give me some error of some sort saying that a Protocol
shouldn’t have an implementation or something, but apparently it works.
Of course the benefit of Protocols is that can classes also be structurally identical to a protocol and being able to recognized as such when using @runtime_checkable
(with all the warnings about the fact that the check may be expensive for protocols with a lot of methods). So… is there any point about using ABC
at all at this point? What’s the catch? I’m definetely missing something here.
EDIT: while writing it got me thinking that maybe I’m losing the ability of using metaclasses in some capacity, but I have very little experience with those and rarely ever used them.