typehint 避免環狀import的方法 - jenhaoyang/backend_blog GitHub Wiki

#python3.7+
# 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):
        ...
#python 3.7以前
# some_file.py

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from main import Main

class MyObject(object):
    def func2(self, some_param: 'Main'):
        ...

參考:
https://newbedev.com/python-type-hinting-without-cyclic-imports