I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so:
class Main(object, MyMixin):
def func1(self, xxx):
def func2(self: Main, xxx): # <--- note the type hint
There isn't a hugely elegant way to handle import cycles in general, I'm afraid. Your choices are to either redesign your code to remove the cyclic dependency, or if it isn't feasible, do something like this:
# some_file.py from typing import TYPE_CHECKING if TYPE_CHECKING: from main import Main class MyObject(object): def func2(self, some_param: 'Main'): ...
TYPE_CHECKING constant is always
False at runtime, so the import won't be evaluated, but mypy (and other type-checking tools) will evaluate the contents of that block.
We also need to make the
Main type annotation into a string, effectively forward declaring it since the
Main symbol isn't available at runtime.
All that said, using mixins with mypy will likely require a bit more structure then you currently have. Mypy recommends an approach that's basically what
deceze is describing -- to create an ABC that both your
MyMixin classes inherit. I wouldn't be surprised if you ended up needing to do something similar in order to make Pycharm's checker happy.