Clarify that generator containing an async list comprehension becomes an async generator
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
import asyncio
async def foo():
print(x for x in [1]) # <generator object foo.<locals>.<genexpr> at 0x...>
print([a async for a in b] for x in [1]) # <async_generator object foo.<locals>.<genexpr> at 0x...>
asyncio.run(foo())
This happens only if the genexp contains an async listcomp inside of it:
[snip]
This makes sense because the inner listcomp is async and therefore must be evaluated within an async context. It's not clearly documented, though: https://docs.python.org/3.14/reference/expressions.html#generator-expressions says "If a generator expression contains either async for clauses or await expressions it is called an asynchronous generator expression. An asynchronous generator expression returns a new asynchronous generator object, which is an asynchronous iterator (see Asynchronous Iterators)." However, I wouldn't necessarily expect that to include async for expressions in a nested scope. Also, when a genexp includes an async genexp, the outer genexp doesn't become async.
So I'd recommend that you open a CPython bug report to have this clarified in the documentation.
Originally posted by @JelleZijlstra in https://github.com/microsoft/pyright/discussions/10827#discussioncomment-14179632
CPython versions tested on:
3.13
Operating systems tested on:
No response
Linked PRs
- gh-138105
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với phần “Generator expressions” trong tài liệu tham chiếu về biểu thức Python tại URL tài liệu được liên kết, rồi so sánh định nghĩa về các biểu thức sinh không đồng bộ của phần này với ví dụ list-comprehension không đồng bộ lồng nhau đã được báo cáo. Làm rõ khi nào biểu thức sinh bên ngoài trở thành không đồng bộ, bao gồm cả sự khác biệt so với một biểu thức bên ngoài chứa một biểu thức sinh không đồng bộ. Kiểm tra CPython issue liên quan, gh-138105, trước khi tiếp tục.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- documentation
- Loại issue
- Tài liệu
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100