【发布时间】:2021-08-25 09:10:10
【问题描述】:
所以,我从一开始就使用 mypy 来学习如何使用类型检查在 python 中编码。我正在使用此代码进行训练:
def stars(*args: int, **kwargs: float) -> None:
for arg in args:
print(arg)
for key, value in kwargs:
print(key, value)
stars(1.3,1.3)
我收到此类型错误:
learning_mypy.py:6: error: Unpacking a string is disallowed
learning_mypy.py:7: error: Cannot determine type of 'key'
learning_mypy.py:7: error: Cannot determine type of 'value'
learning_mypy.py:9: error: Argument 1 to "stars" has incompatible type "float"; expected "int"
learning_mypy.py:9: error: Argument 2 to "stars" has incompatible type "float"; expected "int"
Found 5 errors in 1 file (checked 1 source file)
所以我的问题是:
- 为什么会出现错误
mypy.py:6? - 如何定义
key和value的值类型? - 为什么出现错误
mypy.py:9?
【问题讨论】:
-
star(1.3:1.3)是无效的语法。你的意思是放别的东西吗? -
对不起,是星星(1.3,1.3)
标签: python type-hinting mypy typechecking python-typing