【发布时间】:2019-11-29 23:40:34
【问题描述】:
我似乎不理解 Mypy 在以下情况下的行为。本题简化代码
import typing as t
...
self._store:t.Dict[str,str] = dict()
...
def set_kv(self, key:str, value:int)->t.Any:
assert isinstance(key, six.string_types)
assert isinstance(value, six.string_types)
with self.__lock.write():
self.__store[key] = value
self.__persist()
我通过运行以下命令使用 mypy 测试此代码
mypy docido_sdk/index/test.py --ignore-missing-imports --follow-imports=error --strict-optional
现在理想情况下,这应该在self.__store[key]= value 行引发错误。但事实并非如此。
当我删除assert isinstance(value, six.string_types) 时,它才会抛出错误。 isinstance 是下面给出的典型函数
def isinstance(__o: object, __t: Union[type, Tuple[Union[type, Tuple], ...]]) -> bool: ...
这是mypy的一个bug还是预期的行为,因为如果我理解正确isinstance应该不会影响mypy对value类型的理解。
【问题讨论】:
-
感谢guyz 的回复,我以为mypy 不知道six.string_types 是什么意思。两个答案都是正确的。
标签: python-3.x mypy