我使用的是 python 3.10。我正在使用 PyCharm 的默认类型检查器和 MyPy。 这是我定义的协议:
class OnSubscribeFunc(Protocol):
def __call__(self, instrument: str, *, x: int) -> AsyncGenerator:
...
当创建一个像这样实现它的方法时:
class A:
async def subscribe(self, instrument: str, *, x: int):
yield ...
a: OnSubscribeFunc = A().subscribe # this apparently is where it gets it wrong
我收到此警告:
Expected type 'OnSubscribeFunc', got '(instrument: str, Any, x: int) -> AsyncGenerator' instead
如果我从我的实现中删除
*
,警告就会消失。我希望它是相反的,因为没有 *
允许实现具有非仅关键字参数,这可能不是我的协议的目标。
所以为了比较 - 这个实现没有给出警告:
class A:
async def subscribe(self, instrument: str, x: int):
yield ...
这对我来说没有任何意义,为什么它会这样,这是预期的还是我的类型检查器中的错误?