Scenario
I have a Foo
protocol:
class Foo(Protocol):
fooname: str
def do_fooey_things(self):
pass
and a bunch of different types of concrete Foos:
class PurpleFoo:
fooname: str = "purple"
def do_fooey_things(self):
print("purple foo!")
What I’ve achieved with this is a way of being able to type-hint, have my IDE help me, and run tests to make sure all my concrete Foos are doing what I expect: isinstance(purple_foo, Foo)
. And I’ve met another design requirement which is I want to minimize abstraction (ie I don’t want to use abstract base classes).
But now I need all my Foo’s to use a mixin from a third party library like:
class PurpleFoo(BarMixin):
....
But now my IDE can’t help me with the BarMixin
methods and attributes, and my isinstance
checks will pass even if one of the Foos fails to use BarMixin
(and I want them all too use it).
Question and what I’ve tried
I know I can’t do
class Foo(Protocol, BarMixin)
...
So what do I do?
The best I’ve come up with so far is to make Foo
have class attributes pointing to the attributes and methods of BarMixin
that I’m interested in adopting:
class Foo(Protocol, BarMixin)
fooname: str
bar_attribute = BarMixin.bar_attribute
bar_method = BarMixin.bar_method
At least this way I benefit from the IDE help (at least in VSCode) and the isinstance
check catches any Foo’s that don’t inherit from BarMixin
.