Python type hinting without cyclic imports

后端 未结 5 838
太阳男子
太阳男子 2020-11-28 04:57

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:

相关标签:
5条回答
  • 2020-11-28 05:22

    I think the perfect way should be to import all the classes and dependencies in a file (like __init__.py) and then from __init__ import * in all the other files.

    In this case you are

    1. avoiding multiple references to those files and classes and
    2. also only have to add one line in each of the other files and
    3. the third would be the pycharm knowing about all of the classes that you might use.
    0 讨论(0)
  • 2020-11-28 05:24

    For people struggling with cyclic imports when importing class only for Type checking: you will likely want to use a Forward Reference (PEP 484 - Type Hints):

    When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later.

    So instead of:

    class Tree:
        def __init__(self, left: Tree, right: Tree):
            self.left = left
            self.right = right
    

    you do:

    class Tree:
        def __init__(self, left: 'Tree', right: 'Tree'):
            self.left = left
            self.right = right
    
    0 讨论(0)
  • 2020-11-28 05:24

    Turns out my original attempt was quite close to the solution as well. This is what I'm currently using:

    # main.py
    import mymixin.py
    
    class Main(object, MyMixin):
        def func1(self, xxx):
            ...
    
    
    # mymixin.py
    if False:
        from main import Main
    
    class MyMixin(object):
        def func2(self: 'Main', xxx):  # <--- note the type hint
            ...
    

    Note the import within if False statement that never gets imported (but IDE knows about it anyway) and using the Main class as string because it's not known at runtime.

    0 讨论(0)
  • 2020-11-28 05:32

    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'):
            ...
    

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

    If you are using Python 3.7+, we can at least skip having to provide an explicit string annotation by taking advantage of PEP 563:

    # some_file.py
    
    from __future__ import annotations
    from typing import TYPE_CHECKING
    if TYPE_CHECKING:
        from main import Main
    
    class MyObject(object):
        # Hooray, cleaner annotations!
        def func2(self, some_param: Main):
            ...
    

    The from __future__ import annotations import will make all type hints be strings and skip evaluating them. This can help make our code here mildly more ergonomic.

    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 Main and 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.

    0 讨论(0)
  • 2020-11-28 05:33

    The bigger issue is that your types aren't sane to begin with. MyMixin makes a hardcoded assumption that it will be mixed into Main, 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.

    To properly do this with sane typing, MyMixin should be coded against an interface, or abstract class in Python parlance:

    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')
    
    0 讨论(0)
提交回复
热议问题