小编use*_*175的帖子

mypy麻烦列表对象的继承

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'没有错误。

python typing python-3.x mypy

6
推荐指数
2
解决办法
482
查看次数

标签 统计

mypy ×1

python ×1

python-3.x ×1

typing ×1