【发布时间】:2020-08-07 09:41:10
【问题描述】:
在我的项目中,我的 Mypy 因某些继承问题困扰着我,我找不到为什么在某些情况下它不会抱怨错误的原因:
note: In class "Cat":
Incompatible types in assignment (expression has type "Dict[str, Any]", base class "Animal" defined the type as "None")
而不是Dog,代码示例:
class Animal:
attributes = None
def __init__(self):
if attributes is None:
raise NotImplementedExcepton
class Cat(Animal):
attributes = {
'fur': 'black',
'sound': 'meow',
}
class Dog(Animal):
attributes = {
'fur': 'brown',
'sound': 'woof',
}
【问题讨论】:
-
"在某些情况下它不会抱怨" 在哪些情况下它不会抱怨?这些案例真的可取吗?为什么你首先分配
attributes = None,而不是仅仅注释为例如attributes: Dict[str, Any]? -
我会编辑,但基本上看起来完全一样...
-
我如何注释?
标签: python python-3.x mypy