Python type hinting without cyclic imports
<p>I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so:</p>

<pre><code># main.py
import mymixin.py

class Main(object, MyMixin):
    def func1(self, xxx):
        ...


# mymixin.py
class MyMixin(object):
    def func2(self: Main, xxx):  # &lt;--- note the type hint
        ...
</code></pre>

<p>Now, while this works just fine, the type hint in MyMixin.func2 of course can't work. I can't import main.py, because I'd get a cyclic import and without the hint, my editor (PyCharm) can't tell what <code>self</code> is.</p>

<p>Using Python 3.4, willing to move to 3.5 if a solution is available there.</p>

<p>Is there any way I can split my class into two files and keep all the "connections" so that my IDE still offers me auto completion &amp; all the other goodies that come from it knowing the types?</p>

<p>The bigger issue is that your types aren't sane to begin with. <code>MyMixin</code> makes a hardcoded assumption that it will be mixed into <code>Main</code>, whereas it could be mixed into any number of other classes, in which case it would probably break. If your mixin is hardcoded to be mixed into one specific class, you may as well write the methods directly into that class instead of separating them out.</p>

<p>To properly do this with sane typing, <code>MyMixin</code> should be coded against an <em>interface</em>, or abstract class in Python parlance:</p>

<pre><code>import abc


class MixinDependencyInterface(abc.ABC):
    @abc.abstractmethod
    def foo(self):
        pass


class MyMixin:
    def func2(self: MixinDependencyInterface, xxx):
        self.foo()  # â mixin only depends on the interface


class Main(MixinDependencyInterface, MyMixin):
    def foo(self):
        print('bar')
</code></pre>

python pycharm python-3.4 python-3.5 type-hinting