0
私はPythonプログラムに型ヒントを付けるのにいくつか問題があります。 これはPython 3.5からです。この例ではPythonのヒントタイプ
:私はmypyを実行したときに
# -*- coding: utf-8 -*-
import collections
import typing
XV = typing.TypeVar('XV')
class Coll(collections.OrderedDict[str, XV]):
def sorted(self) -> collections.OrderedDict[str, XV]:
dict_sorted = collections.OrderedDict() # type: collections.OrderedDict[str,XV]
for key in sorted(self.keys()):
dict_sorted[key] = self[key]
return dict_sorted
def __str__(self) -> str:
retour = "" # type:str
if len(self) == 0:
return ""
test = self.sorted() # type: collections.OrderedDict[str,XV]
for l in test:
if retour:
retour += "\n{0!s}".format(self[l])
else:
retour = "{0!s}".format(self[l])
return retour
def __repr__(self) -> str:
return self.__str__()
は、私は次のようにあります
example.py:8: error: Invalid type "example.XV"
example.py: note: In function "__str__":
example.py:20: error: Invalid type "example.XV"
私はこれらのエラーを持っている理由私は理解していないものがあります。
'collections.OrderedDict'は型パラメータをとりません。 – user2357112