如果你愿意安装一个不很-官方扩展
typing,打字的扩展,您可以使用
Protocol,这应该是一个实现PEP-0544:
from typing_extensions import Protocolfrom typing import Anyclass GetItem(Protocol): def __getitem__(self: 'Getitem', key: Any) -> Any: passclass BadGetItem: def __getitem__(self, a: int, b: int) -> Any: passdef do_thing(arg: GetItem): passdo_thing(dict()) # OKdo_thing(BadGetItem()) # Fails with explanation of correct signaturedo_thing(1) # Fails



