Python mypy在嵌套数据结构中未检测到泛型类型不匹配
我遇到过一个例子,在嵌套结构中使用泛型时,mypy似乎无法检测到类型不匹配Python mypy在嵌套数据结构中未检测到泛型类型不匹配,python,generics,mypy,Python,Generics,Mypy,我遇到过一个例子,在嵌套结构中使用泛型时,mypy似乎无法检测到类型不匹配 T = TypeVar('T') class Constructable(ABC, Generic[T]): def __init__(self, parameter: T): pass class ConstructableWithInt(Constructable[int]): pass class TypeContainer(Generic[T]): const
T = TypeVar('T')
class Constructable(ABC, Generic[T]):
def __init__(self, parameter: T):
pass
class ConstructableWithInt(Constructable[int]):
pass
class TypeContainer(Generic[T]):
constructable: Type[Constructable[T]]
def __init__(
self,
constructable: Type[Constructable[T]]
):
self.constructable = constructable
class ConstructionTuple(Generic[T]):
constructable: TypeContainer[T]
parameter: T
def __init__(self, spec: TypeContainer[T], parameter: T):
self.spec = spec
self.parameter = parameter
class ConstructableTupleWrapper(Generic[T]):
constructable_tuple: ConstructionTuple[T]
def __init__(self, constructable_tuple: ConstructionTuple[T]):
self.constructable_tuple = constructable_tuple
constructable_with_int_wrapper = TypeContainer(
constructable=ConstructableWithInt,
)
# this leads to an error in mypy due to mismatch of `str` and `int`
mismatching_int_and_string_directly = ConstructionTuple(
spec=constructable_with_int_wrapper,
parameter="5"
)
mismatching_int_and_string_within_wrapper = ConstructableTupleWrapper(
# this produces no error, even though it has the same mismatch
constructable_tuple=ConstructionTuple(
spec=constructable_with_int_wrapper,
parameter="5"
)
)
我使用泛型是错误的还是这可能是[python-generics/mypy]中的一个bug