Resolving Cyclic Imports with Typing
Recently when working on a non-trivial pygame toy project, I found I was having issues with implementing some typehints, it would cause my program to fail to startup when the code ran fine at runtime
I would get errors like:
AttributeError: partially initialized module 'module_x' has no attribute 'Thing' (most likely due to a circular import)
Debugging
To understand a bit deeper the cause of the cycle, you can look for repeated files in your traceback
Solution
If this is the issue you face, you can make use of some features introduced in pep-484/#forward-references (with considerations in pep-563) to easily solve it.
For example, change the following:
import module_x
def a_function(thing: module_x.Thing):
...
to this:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import module_x
def a_function(thing: 'module_x.Thing'):
...