8000 调用模块的过程中存在`asynchronous comprehension outside async function`的问题 · Issue #19 · TrueRou/maimai.py · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

调用模块的过程中存在asynchronous comprehension outside async function的问题 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shinyashen opened this issue Jun 11, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@shinyashen
Copy link

环境信息
Python 版本: 3.10.18
操作系统: Linux
调用环境: 在HoshinoBot (基于 NoneBot 的 QQ 机器人框架)的插件中作为模块调用
问题文件: maimai_py/providers/local/__init__.py

问题描述
在插件代码文件中导入 maimai_py 模块后运行bot时,遇到以下错误:

ERROR: asynchronous comprehension outside of an asynchronous function (File "/maimai_py/providers/local/__init__.py", line 94)
SyntaxError: asynchronous comprehension outside of an asynchronous function

错误发生在 LocalProvider.get_areas 方法中,具体是在尝试在列表推导式中使用 await 调用:

songs=[
    AreaSong(
        id=s.id if (s := await maimai_songs.by_title(song["title"])) else None,
        # ...
    )
    for song in item["songs"]
]

可能的原因
试着让ai分析了一下,得到的回答如下:

问题在于 Python 不允许在同步上下文(如列表推导式)中使用 await 关键字。虽然 get_areas 是异步函数,但内部的列表推导式仍然是同步执行上下文。
具体来说:
- Python 3.10 不允许在列表推导式中使用 await
- 这会导致语法错误,因为编译器无法正确处理这种结构
- 错误发生在模块导入阶段,因此会阻止整个模块加载

ai给出的解决方案是把返回值的列表推导式改为显式的异步循环,我按照ai给出的方案修改了包代码确实可以正常运行了

@TrueRou TrueRou added the bug Something isn't working label Jun 12, 2025
@TrueRou TrueRou self-assigned this Jun 12, 2025
@TrueRou
Copy link
Owner
TrueRou commented Jun 12, 2025

附加原Issues: python/cpython#77527

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants
0