[Python] Python type hinting without cyclic imports

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so:

    main.py file:

    import mymixin.py

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


    mymixin.py file:

    class MyMixin(object):
    def func2(self: Main, xxx): # <--- note the type hint
    ...


    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 self is.

    I'm using Python 3.4, but I'm willing to move to 3.5 if a solution is available there.

    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 and all the other goodies that come from it knowing the types?

    Continue reading...

Compartilhe esta Página