私はPython 3.5をMypyと一緒に使用して、スクリプトの基本的な静的検査を行っています。最近、私はOrderedDictを返すいくつかのメソッドをリファクタリングしましたが、キーと値の型を指定してリターンアノテーションを使用しようとしたときに、 "'type'オブジェクトが添字付きではないというエラーに遭遇しました。Mypy型アノテーションにOrderedDict K、V型を指定するにはどうすればよいですか?
削減例:
#!/usr/bin/env python3.5
from collections import OrderedDict
# this works
def foo() -> OrderedDict:
result = OrderedDict() # type: OrderedDict[str, int]
result['foo'] = 123
return result
# this doesn't
def foo2() -> OrderedDict[str, int]:
result = OrderedDict() # type: OrderedDict[str, int]
result['foo'] = 123
return result
print(foo())
そして、それを実行したときに、これは、Pythonの出力です:
Traceback (most recent call last):
File "./foo.py", line 12, in <module>
def foo2() -> OrderedDict[str, int]:
TypeError: 'type' object is not subscriptable
Mypyしかしコメントで型注釈に問題を持っていないし、私がしようとした場合、実際に警告が表示されます行うことresult[123] = 123
。
この原因は何ですか?
(mypyバージョン0.501)。 – max