を定義するには、私はそうのようなqueue.Queue
のサブクラスを持っている:mypy:どのように一般的なサブクラス
class SetQueue(queue.Queue):
"""Queue which will allow a given object to be put once only.
Objects are considered identical if hash(object) are identical.
"""
def __init__(self, maxsize=0):
"""Initialise queue with maximum number of items.
0 for infinite queue
"""
super().__init__(maxsize)
self.all_items = set()
def _put(self):
if item not in self.all_items:
super()._put(item)
self.all_items.add(item)
私は静的型チェックのためにmypyを使用しようとしています。
from typing import Generic, Iterable, Set, TypeVar
# Type for mypy generics
T = TypeVar('T')
class SetQueue(queue.Queue):
"""Queue which will allow a given object to be put once only.
Objects are considered identical if hash(object) are identical.
"""
def __init__(self, maxsize: int=0) -> None:
"""Initialise queue with maximum number of items.
0 for infinite queue
"""
super().__init__(maxsize)
self.all_items = set() # type: Set[T]
def _put(self, item: T) -> None:
if item not in self.all_items:
super()._put(item)
self.all_items.add(item)
mypy「は、一般的なタイプの欠落型パラメータを」と言って、クラス定義行に警告がスローされます。この場合、SetQueueこれは、これまで私の試みである一般的なオブジェクトのT.を取る必要があります。
どこかでGeneric[T]
が必要だと思っていますが、私が作ったすべての試みは構文エラーをスローします。ドキュメントのすべての例では、サブクラス化はGeneric[T]
ですが、他のオブジェクトからサブクラス化することはありません。
SetQueueの汎用タイプを定義する方法を知っている人はいますか?
'
queue.Queue
へのT
'SetQueue
を関連付けることができないので、私はその多重継承がうまくいきませんでした注意してください ''クラスSetQueue(queue.Queue、ジェネリック[T])との問題は何ですか? –質問を書いた後、私たちは複数の継承を使うべきかどうか疑問に思った。これは、既存のクラスをサブクラス化するクラス(型アノテーション自体を持たない)にジェネリック型を実装するための推奨された方法ですか? – blokeley