そのクラスのインスタンスではなくクラス オブジェクトを期待する関数の引数に注釈を付ける適切な方法は何ですか?
以下の例では、some_class引数は型インスタンス (クラス) であると予想されますが、ここでの問題は、それtypeが広すぎることです:
def construct(some_class: type, related_data:Dict[str, Any]) -> Any:
...
some_class特定のタイプ オブジェクトのセットが必要な場合は、使用typeしてもまったく役に立ちません。モジュールには、これtypingを行う Class ジェネリックが必要な場合があります。
def construct(some_class: Class[Union[Foo, Bar, Baz]], related_data:Dict[str, Any]) -> Union[Foo, Bar, Baz]:
...
上記の例でsome_classは、 はFoo、BarまたはFazクラスであり、そのインスタンスではありません。some_class: Class[Foo]有効なケースであるため、クラス ツリー内の位置は重要ではありません。したがって、
# classes are callable, so it is OK
inst = some_class(**related_data)
また
# instances does not have __name__
clsname = some_class.__name__
また
# an operation that only Foo, Bar and Baz can perform.
some_class.a_common_classmethod()
mypy、pytype、PyCharm などには問題ないはずです。
現在の実装 (Python 3.6 以前) でこれを行うにはどうすればよいですか?