例如,我有一段代码如下:
from typing import Type, TypeVar, cast
class SuperClass:
pass
T = TypeVar('T', bound=SuperClass)
def cast_to(obj: SuperClass, cast_to: Type[T] = SuperClass) -> T:
return cast(cast_to, obj)
我把它保存在type_check.py
. 如果我在其上运行 mypy,则会收到以下错误消息:
type_check.py:10: error: Incompatible default for argument "cast_to" (default has type "Type[SuperClass]", argument has type "Type[T]")
type_check.py:11: error: Invalid type "cast_to"
根据我对bound
in的理解TypeVar
,只要 aT
是 的子类SuperClass
,应该没问题。但是为什么 mypy 在这里抛出错误呢?谢谢!