Get PyCharm to know what classes are mixin for

后端 未结 2 1439
遇见更好的自我
遇见更好的自我 2021-02-20 09:46

Our application has set of complex form wizards. To avoid code duplication I created several mixins.

The problem is that PyCharm highlights mixin methods with Unre

2条回答
  •  悲哀的现实
    2021-02-20 10:08

    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].

提交回复
热议问题