这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@pnuckowski
Copy link
Owner

fix for #270

@pnuckowski pnuckowski requested a review from Copilot June 10, 2025 09:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for streaming async-iterable request bodies and a test to validate error propagation when a generator raises.

  • Introduce logic in _request_mock to consume async iterables in data and replace them with aggregated bytes.
  • Add test_async_generator_body_exception to ensure a generator exception bubbles up.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/test_async_generator_body.py New async test asserting generator errors are propagated
aioresponses/core.py Handle async-iterable data by iterating chunks into bytes
Comments suppressed due to low confidence (1)

tests/test_async_generator_body.py:12

  • [nitpick] Consider adding a complementary test case for successful streaming (no errors) to verify that chunks from an async generator are properly aggregated and sent as the request body.
async def data_generator():

try:
body_bytes = b""
async for chunk in data:
body_bytes += chunk
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accumulating bytes by repeated concatenation can lead to quadratic memory usage for large streams; consider appending chunks to a list and doing b''.join(chunks) after iteration.

Copilot uses AI. Check for mistakes.
Comment on lines +507 to +513
try:
body_bytes = b""
async for chunk in data:
body_bytes += chunk
kwargs['data'] = body_bytes
except Exception:
raise
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try/except that immediately re-raises the caught exception is redundant; you can remove the try/except block and let exceptions propagate naturally to simplify the code.

Suggested change
try:
body_bytes = b""
async for chunk in data:
body_bytes += chunk
kwargs['data'] = body_bytes
except Exception:
raise
body_bytes = b""
async for chunk in data:
body_bytes += chunk
kwargs['data'] = body_bytes

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants