使用仅关键字参数的Python协议需要实现具有不同的签名

问题描述 投票:0回答:1

我使用的是 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 ...

这对我来说没有任何意义,为什么它会这样,这是预期的还是我的类型检查器中的错误?

python python-3.x pycharm python-typing
1个回答
0
投票

这是 PyCharm 的已知错误MypyPyright 都接受您的代码。

type: ignore
# noqa
放在那里并继续。

© www.soinside.com 2019 - 2024. All rights reserved.