Pylint警告,该类继承了一个继承多个键入。Generi

2024-04-23 10:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用类型注释编写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更正此代码?你知道吗


Tags: 代码fromimportselfnone警告typing类型