Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 类型暗示子类,其中包含对自身类型的其他实例的引用_Python 3.x_Python Typing - Fatal编程技术网

Python 3.x 类型暗示子类,其中包含对自身类型的其他实例的引用

Python 3.x 类型暗示子类,其中包含对自身类型的其他实例的引用,python-3.x,python-typing,Python 3.x,Python Typing,我正在尝试为二维链表创建一个基类。从功能的角度来看,下面的代码已经运行良好。问题是,当我将LinkedList2D子类化以创建更具体的版本时,我的所有属性(上、下、前、后)和生成器函数仍然将类型提示解析为LinkedList2D而不是更具体的子类,这是必要的,因为我想访问它的附加属性和方法 我有一种感觉,我需要以某种方式使用绑定泛型,但我似乎无法将我的头脑集中在它上面 类LinkedList2D: up:可选[LinkedList2D] 向下:可选[LinkedList2D] before:可选

我正在尝试为二维链表创建一个基类。从功能的角度来看,下面的代码已经运行良好。问题是,当我将LinkedList2D子类化以创建更具体的版本时,我的所有属性(上、下、前、后)和生成器函数仍然将类型提示解析为LinkedList2D而不是更具体的子类,这是必要的,因为我想访问它的附加属性和方法

我有一种感觉,我需要以某种方式使用绑定泛型,但我似乎无法将我的头脑集中在它上面

类LinkedList2D:
up:可选[LinkedList2D]
向下:可选[LinkedList2D]
before:可选[LinkedList2D]
之后:可选[LinkedList2D]
定义初始化(自)->无:
self.up=无
self.down=无
self.before=无
self.after=无
def iterate_children(self)->Generator[LinkedList2D,None,None]:
链接=自动关闭
而链接:
屈服链
link=link.after
def iterate_tree(self)->生成器[LinkedList2D,无,无]:
对于self.iterate_children()中的子对象:
如果是儿童:
让子
从child.iterate_children()生成
def get_children(self)->List[LinkedList2D]:
返回列表(self.iterate_children())
在(self,op:LinkedList2D)->bool下插入def_:
如果是op:
如果op.down:
在前插入_(op.down)
op.down=self
self.up=op
返回真值
def insert_位于_last(self,op:LinkedList2D)->bool:
self.up=op
如果是op:
如果op.down:
last=self.get_children()[-1]
self.insert_后面(最后一个)
其他:
op.down=self
返回真值
def insert_后(self,obj:LinkedList2D)->bool:
如果obj:
如果obj.after:
obj.after.before=自我
obj.after=self
self.after=obj.after
self.up=obj.up
self.before=obj
返回真值
def insert_before(self,obj:LinkedList2D)->bool:
如果obj:
如果在以下情况之前:
obj.before.after=self
obj.before=自我
self.up=obj.up
self.before=obj.before
self.after=obj
返回真值
我想,通用版本应该是这样的。我使用bound来确保潜在实例是LinkedList2D的子类,并实现必要的函数和属性

T=TypeVar(“T”,bind=“LinkedList2D”)
类LinkedList2D(泛型[T]):
up:可选[T]
向下:可选[T]
before:可选[T]
后:可选[T]
定义初始化(自)->无:
...
def iterate_children(self)->Generator[T,None,None]:
...
def iterate_tree(self)->生成器[T,None,None]:
...
def get_children(self)->List[T]:
...
在(自身,op:T)->bool下插入def_:
如果是op:
如果op.down:
在前插入_(op.down)
op.down=self
self.up=op
返回真值
def在_last下插入_(self,op:T)->bool:
...
def插入_之前(自身,对象:T)->bool:
...
问题是,现在pylance正在抱怨方法下插入的中的行op.down=self

无法为类型“LinkedList2D”分配成员“down”[T@LinkedList2D]"
“LinkedList2D”类型的表达式[T@LinkedList2D]无法将“”分配给类“LinkedList2D”的成员“down”[T@LinkedList2D]"
键入“LinkedList2D”[T@LinkedList2D]“无法分配给类型”T@LinkedList2D|无”
键入“LinkedList2D”[T@LinkedList2D]“无法分配给类型”T@LinkedList2D"
无法分配给“无”PylancereportGeneralTypeIssues
任何想法和提示都将不胜感激。谢谢