跳到内容

type-name-incorrect-variance (PLC0105)

源自 Pylint 代码检查工具。

作用

检查类型名称是否与其关联的类型参数的变异性不匹配。

为什么这不好?

PEP 484 建议对协变和逆变类型参数分别使用 _co_contra 后缀(而不变类型参数不应有任何此类后缀)。

示例

from typing import TypeVar

T = TypeVar("T", covariant=True)
U = TypeVar("U", contravariant=True)
V_co = TypeVar("V_co")

建议改为

from typing import TypeVar

T_co = TypeVar("T_co", covariant=True)
U_contra = TypeVar("U_contra", contravariant=True)
V = TypeVar("V")

参考