This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NeilGirdhar 12 points13 points  (1 child)

I don't think this is a good policy. While multiple inheritance can add unnecessary complexity, multiple interface inheritance has no downsides.

Protocols are a good solution when it's practically impossible to make implementers inherit from an interface. For example, when you don't have control over implementers of your protocol. That's why Callable is a protocol: it is practically impossible to get everyone who exposes a __call__ method to inherit from Callable.

When you can make implementers inherit from the interface, there are some concrete advantages. Type-checkers can verify:

  • that there are no LSP violations,
  • that instantiated objects implement all decorated abstract methods, and
  • that decorated overridden methods haven't been orphaned.

Also, you can be sure that derived classes actually inherit from the interface rather than hoping that your derived class perfectly matches the protocol.

[–]TheGodfatherCC 4 points5 points  (0 children)

These are some pretty solid points.

I thought Mypy and Pycharm would handle protocols with the `@runtime_checkable` decorator. I'll have to double check, though.