为什么 mypy 不接受 list[str] 作为 list[Optional[str]]?
Why does mypy not accept a list[str] as a list[Optional[str]]?
示例 1:
from typing import List, Optional
def myfunc() -> List[Optional[str]]:
some_list = [x for x in "abc"]
return some_list
Mypy complains 在示例 1 中:
Incompatible return value type (got "List[str]", expected
"List[Optional[str]]")
但是,这个例子gets no complaint:
示例 2:
def myfunc() -> List[Optional[str]]:
some_list = [x for x in "abc"]
return list(some_list)
不一致行为的解释是什么?
示例 1:
from typing import List, Optional
def myfunc() -> List[Optional[str]]:
some_list = [x for x in "abc"]
return some_list
Mypy complains 在示例 1 中:
Incompatible return value type (got "List[str]", expected "List[Optional[str]]")
但是,这个例子gets no complaint:
示例 2:
def myfunc() -> List[Optional[str]]:
some_list = [x for x in "abc"]
return list(some_list)
不一致行为的解释是什么?