6đź‘Ť
You can type-hint to PyCharm what kind of classes to expect.
class DictMixin(object):
def megamethod(
self, # type: dict
key
):
return self.get(key)
It’s still not quite comparable to other type handling.
PyCharm is lazy in evaluating it, and only does so when first working on self
.
Things are a bit tricky when accessing attributes of the mixin as well – self, # type: dict | DictMixin
works for one of my classes, but not in my test code.
In python 3.5, you should be able to use # type: typing.Union[dict, DictMixin]
.
2đź‘Ť
If you are creating Mixin, for, let’s say ClassSub, which is subclass of ClassSuper, you can implement Mixins this way:
class Mixin1(ClassSuper):
pass
class Mixin2(ClassSuper):
pass
and then use them like:
class ClassSub(Mixin1, Mixin2):
pass
That way I use some mixins for models in Django. Also, django-extensions uses similar pattern (gives models that are actually mixins). Basically, this way you don’t have to inherit ClassSuper
, because it’s “included” in every of your mixins.
Most important – PyCharm works like a charm this way.
- Django – Can I alter construction of field defined in abstract base model for specific child model?
- Logging Django SQL queries with DEBUG set to False
- Profiling Django webserver for high startup times