Python 3.6.5和mypy 0.600
我写了代码:
from typing import List
class Animal():
pass
class Dog(Animal):
def __init__(self) -> None:
super()
def bark(self) -> None:
pass
class Cat(Animal):
def __init__(self) -> None:
super()
def meow(self) -> None:
pass
arr1: List[Dog] = [Dog(), Dog()]
arr2: List[Animal] = [Dog(), Dog()]
# error: Incompatible types in assignment (expression has type "List[Dog]", variable has type "List[Animal]")
arr3: List[Animal] = arr1
Run Code Online (Sandbox Code Playgroud)
我不明白,为什么我有一个错误“赋值中的不兼容类型”和变量“ arr3”。狗是从动物继承的类。例如,变量'arr2'没有错误。