[英]Expressing type hint of sibling nested class in class constructor
我在使用 pycharm 编码时使用 python 的类型提示系统,我相信它使用了typing
模块,但是我有一个场景 pycharm 给了我一个正确的答案,我可以在网上找不到答案:
from typing import List
class Something:
class A(object):
def __init__(self, d: int) -> None:
self.data = d
class B(object):
def __init__(self, inListStr: List[str], inListA: List[A]): # "A" here is marked as "Unresolved Reference". Something.A does not fix the issue either
self.list_of_str = inListStr
self.list_of_a = inListA
def __init__(self, inB: B): #B here is accepted ok
self.data_b = inB
你知道我如何正确地将inListA
输入为“As 列表”类型吗?
使用Something.A
,但将其括在引号中:
...
def __init__(self, inListStr: List[str], inListA: List['Something.A']):
...
Python 解释器无法在代码中的那一点评估A
或Something.A
。 通过将其设为字符串,类型检查器仍然可以在避免运行时评估的同时找出类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.