Skip to content

Instantly share code, notes, and snippets.

@ixuuux
Last active November 6, 2021 09:51
Show Gist options
  • Save ixuuux/b3d0b90961c6d91c2523456f7115f413 to your computer and use it in GitHub Desktop.
Save ixuuux/b3d0b90961c6d91c2523456f7115f413 to your computer and use it in GitHub Desktop.
解决因类型标注导致的循环导入报错
# 解决因类型标注导致的循环导入报错
# file1.py
from typing import TYPE_CHECKING # https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING
if TYPE_CHECKING: # 关键
from file2 import C2
class C1:
def __init__(self, c2_self: "C2"): # 关键,以字符串标注
...
# -----------------------------------------------
# file2.py
from typing import TYPE_CHECKING
if TYPE_CHECKING: # 关键
from file1 import C1
class C2:
def __init__(self, c1_self: "C1"): # 关键,以字符串标注
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment