PyCharm跨模块导入asynccontextmanager时出现参数检查误报
PyCharm 2023.3.1(专业版)asynccontextmanager跨模块导入参数检查误报
问题现象
当从其他模块导入用asynccontextmanager装饰的异步上下文管理器时,PyCharm会出现错误的参数检查提示,但代码实际运行完全正常。同模块内使用该装饰器则无此异常。
无参数上下文管理器的误报
同模块内使用无异常:
# a.py from contextlib import asynccontextmanager @asynccontextmanager async def cont_manager(): try: # 业务逻辑 yield finally: # 清理逻辑 ... async def get_man(): async with cont_manager() as con_man: # 使用上下文管理器 ...
跨模块导入时,IDE会标记async with cont_manager()的右括号,提示**"Parameter '_P' unfilled"**:
# a.py from b import cont_manager async def get_man(): async with cont_manager() as con_man: ... # b.py from contextlib import asynccontextmanager @asynccontextmanager async def cont_manager(): try: ... yield finally: ...
带参数上下文管理器的误报
带参数的上下文管理器跨模块导入时,IDE会出现双重误报:传入的合法参数被标记为**"unexpected argument",同时提示"unfulfilled param '_P'"**,但真正缺失的参数却未被检测提示:
# a.py from b import cont_man_params async with cont_man_params(param1='pppprrrrr1') as con_man: ... # b.py from contextlib import asynccontextmanager @asynccontextmanager async def cont_man_params(param1, param2): print(param1, param2) try: yield finally: ...
已尝试的无效解决方案
- 清除PyCharm缓存并重启
- 删除项目的
.idea目录后重新导入项目 - 回滚至之前确认无问题的代码版本
- 创建全新测试项目复现,问题依然存在
- 切换Python版本(3.11/3.12),问题未解决
官方问题状态
该问题已被JetBrains官方记录为已知Issue,编号为PY-65385。
内容的提问来源于stack exchange,提问作者prosody
相关产品推荐
相关产品推荐

