我正在使用类型注释编写python-3代码,如下所示:
from typing import Generic, TypeVar
T = TypeVar("T")
U = TypeVar("U")
class A(Generic[T]):
def __init__(self, a: T) -> None:
self.a = a
class B(Generic[U]):
def __init__(self, b: U) -> None:
self.b = b
class C(A[T], B[U]):
def __init__(self, a: T, b: U) -> None:
A.__init__(self, a)
B.__init__(self, b)
class D(C[int, str]):
pass
if __name__ == "__main__":
d: D = D(1, "abc")
mypy
批准此代码的类型注释是正确的,但是pylint
会发出警告
E0241: Duplicate bases for class 'D' (duplicate-bases)
我不明白为什么pylint
检测到类D
存在重复的基类。
一个可能的推测是原因是一个diamond继承,它的根是typing.Generic
,但是如果是这样的话,pylint
就不能直观地为类C
发出警告。你知道吗
此外,当我从代码中删除所有类型注释时,pylint
批准此代码是正确的。你知道吗
如何为pylint
更正此代码?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐