Last active
November 6, 2021 09:51
-
-
Save ixuuux/b3d0b90961c6d91c2523456f7115f413 to your computer and use it in GitHub Desktop.
解决因类型标注导致的循环导入报错
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 解决因类型标注导致的循环导入报错 | |
# 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