【发布时间】:2017-06-19 19:20:41
【问题描述】:
我在 Python 3.5 中编写了这段代码:
from collections import namedtuple
attributes = ('content', 'status')
Response = namedtuple('Response', attributes)
当我运行 Mypy 类型检查器来分析此代码时,它引发了错误:
错误:列表或元组文字应作为
namedtuple()的第二个参数
我尝试在attributes 变量中添加类型注释:
from typing import Tuple
attributes = ('content', 'status') # type: Tuple[str, str]
但它并没有解决引发的错误。
【问题讨论】:
-
看起来像 mypy 的问题,因为 namedtuple 可以按原样使用。尝试解压元组,例如...
a, b = Response('some content', 'some attr')-->print('%s; %s' % (a, b)).
标签: python python-3.x type-hinting mypy python-typing