When I try to type hint to my custom class, python will throw an AttributeError on 'module.class'. Intellisence tells me, the import is right and the class can be found.
function_dispatcher.py, it goes wrong in this file at : iterator_function.IteratorFunction
. When I remove the particular type hint, the program will run fine.
from abc import ABC, abstractclassmethod
import iterator_function
class FunctionDispatcher(ABC):
@abstractclassmethod
def dispatch(self, function : iterator_function.IteratorFunction):
pass
iterator_function.py, this is the imported class. Notice: the type hinting to : function_dispatcher.FunctionDispatcher
works here.
import function
import function_dispatcher
class IteratorFunction(function.Function):
def accept(self, dispatcher : function_dispatcher.FunctionDispatcher):
dispatcher.dispatch(self)
This is the traceback output:
1> Traceback (most recent call last):
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\main.py", line 1, in <module>
1> import generator
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\generator.py", line 3, in <module>
1> from iterator_function import IteratorFunction
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\iterator_function.py", line 1, in <module>
1> import function
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\function.py", line 2, in <module>
1> import function_dispatcher
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\function_dispatcher.py", line 4, in <module>
1> class FunctionDispatcher(ABC):
1> File "C:\Users\admin\Documents\visual studio 2015\Projects\Preprocessor\Program\preprocessor\function_dispatcher.py", line 6, in FunctionDispatcher
1> def dispatch(self, function : iterator_function.IteratorFunction):
1> AttributeError: module 'iterator_function' has no attribute 'IteratorFunction'
For anyone wondering what I am trying to do: implementing the visitor pattern.
hiro protagonist was right. I was able to solve the circular import and maintain the type checking. It is not as clean but it does the job.
function.py
iterator_function.py