如何解决PyCharm中execute_workflow的类型提示错误?
PyCharm报类型错误但pyright无异议的问题
我编写了一段执行Temporal工作流的测试代码,PyCharm标记其存在类型错误,但pyright并未提出任何异议。
执行代码如下:
result = await workflow_client.execute_workflow( SayHelloWorkflow.run, req, id="say-hello-test", task_queue=task_queue )
PyCharm给出的类型错误提示:
Unexpected type(s): ((request: MyReq, Any) -> Coroutine[Any, Any, MyResp], MyReq, str, str)
Possible type(s): (MethodAsyncSingleParam[SelfType, ParamType, ReturnType], MyReq, str, str) (str, Any, str, str)
SayHelloWorkflow的定义代码:
from temporalio import workflow from datetime import timedelta @workflow.defn class SayHelloWorkflow: @workflow.run async def run( self, request: MyReq ) -> MyResp: return await workflow.execute_activity( "SayHello", request, start_to_close_timeout=timedelta(seconds=10), )
解决办法
- 推荐方案:传入Workflow类而非run方法
Temporal的execute_workflow支持直接传入Workflow类作为第一个参数,这是官方推荐写法,同时能让PyCharm正确识别类型:
result = await workflow_client.execute_workflow( SayHelloWorkflow, req, id="say-hello-test", task_queue=task_queue )
- 临时方案:添加类型忽略注释
如果暂时不想调整调用方式,可以给SayHelloWorkflow.run添加类型忽略注释,跳过PyCharm的类型检查:
result = await workflow_client.execute_workflow( SayHelloWorkflow.run, # type: ignore req, id="say-hello-test", task_queue=task_queue )
- 更新开发工具
确保PyCharm是最新版本,同时更新Python相关插件,问题可能源于PyCharm对@workflow.run装饰器生成的特殊类型识别滞后,而pyright对这类装饰器的类型处理更准确。
内容的提问来源于stack exchange,提问作者Archimedes Trajano
相关产品推荐
相关产品推荐

