ksindi / implements

:snake: Pythonic interfaces using decorators
http://implements.readthedocs.io/
Apache License 2.0
33 stars 4 forks source link

Document and prevent an implementation class inheriting an interface #24

Closed pshirali closed 3 years ago

pshirali commented 3 years ago

One of the common patterns to define and enforce interface relationship is where the Implementation class inherits the Interface class. This pattern can be seen in Python as well (example: abc)

With implements, an Implementation class must not inherit from an Interface class. implements works by comparing methods between the interface and the implementation. When an Implementation class derives from an Interface class, missing attributes/methods in the Implementation class are not detected as they are inherited from the parent (Interface).

Proposed changes:

  1. implements must explicitly call this pattern out in its Documentation.
  2. implements can prevent this scenario by ensuring that the class hierarchies of the Implementation class, and of the Interface class have no classes in common**.

**Skipping the root The root class in either hierarchy may be object or a metaclass. A tradeoff (for not being too strict) could be to skip the root class entirely (and avoid handing a case of a common metaclass, at the root of both hierarchies)

This enhancement is a consequence of #23

cc: @ksindi @jharwell

ksindi commented 3 years ago

Thanks @pshirali!