【发布时间】:2021-06-24 23:53:12
【问题描述】:
这是我的测试脚本:
class A:
def __init__(self, config: dict) -> None:
self.config = config
def test_func(a: int) -> None:
pass
main_config = {"a": "x"}
a = A(config=main_config)
# value direct
test_func(1) # line 14
test_func("b") # line 15
# value with wrong type from dict
test_func(main_config["a"]) # line 18
test_func(main_config.get("a")) # line 19
# value with wrong type from dict in class instance
test_func(a.config["a"]) # line 22
test_func(a.config.get("a")) # line 23
如果我用 mypy (0.910) 测试它,我会得到以下结果:
> mypy test.py
test.py:15: error: Argument 1 to "test_func" has incompatible type "str"; expected "int"
tests.py:18: error: Argument 1 to "test_func" has incompatible type "str"; expected "int"
tests.py:19: error: Argument 1 to "test_func" has incompatible type "Optional[str]"; expected "int"
tests.py:23: error: Argument 1 to "test_func" has incompatible type "Optional[Any]"; expected "int"
Found 4 errors in 1 file (checked 1 source file)
为什么 mypy 错过/不报告第 22 行的呼叫?
【问题讨论】:
-
这能回答你的问题吗? Why dict.get(key) instead of dict[key]?
-
@adirabargil 这是关于 mypy 对它们的解释的问题,而不是它们的功能。
-
好的,我取消了
标签: python mypy python-typing