Conversation
`#1126` widened the return annotation of `Response.aiter_bytes()` and `Response.aiter_raw()` from `AsyncIterator[bytes]` to `AsyncGenerator[bytes, None]`, so that `contextlib.aclosing()` would accept them. That made overriding either method in a `Response` subclass a typing error, and made it a runtime error to return anything other than an async generator, since `aclosing()` calls `aclose()` unconditionally. Narrow both annotations back, and close the iterator only when it is actually an async generator. This is the same guard `aiter_raw()` already applies to `self.stream`. The sync counterparts are unaffected: `iter_bytes()` and `iter_raw()` were never widened, and neither uses `contextlib.closing()`. Closes #1158
|
Docs preview: |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d32f860e3d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
AsyncIterator[bytes] on aiter_bytes() and aiter_raw()There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
2 issues found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/httpx2/models/test_responses.py">
<violation number="1" location="tests/httpx2/models/test_responses.py:497">
P2: These rewritten tests only exercise async-generator overrides, so they pass without exercising the two behaviors this PR claims to restore/preserve: subclass overrides returning plain async iterators without `aclose()`, and custom iterators whose `aclose()` gets called on cleanup. The previous `PlainAsyncIterator` and `ClosableAsyncIterator` regression tests for those paths were removed here. At the current implementation, `aread()` and `aiter_bytes()` wrap the `aiter_bytes()`/`aiter_raw()` results in `contextlib.aclosing(...)`, which unconditionally awaits `.aclose()` and raises AttributeError on a plain async iterator (verified). Keep a regression test that overrides with a plain async iterator lacking `aclose()`, and one asserting a custom iterator's `aclose()` is invoked.</violation>
</file>
<file name="src/httpx2/httpx2/_models.py">
<violation number="1" location="src/httpx2/httpx2/_models.py:1059">
P2: When an `AsyncByteStream` returns a custom iterator with `aclose()`, `aiter_raw()` skips that cleanup because `AsyncGenerator` is not a structural check for custom iterators. Detect and await an exposed `aclose()` method instead.</violation>
</file>
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_aiter_bytes_override() -> None: |
There was a problem hiding this comment.
P2: These rewritten tests only exercise async-generator overrides, so they pass without exercising the two behaviors this PR claims to restore/preserve: subclass overrides returning plain async iterators without aclose(), and custom iterators whose aclose() gets called on cleanup. The previous PlainAsyncIterator and ClosableAsyncIterator regression tests for those paths were removed here. At the current implementation, aread() and aiter_bytes() wrap the aiter_bytes()/aiter_raw() results in contextlib.aclosing(...), which unconditionally awaits .aclose() and raises AttributeError on a plain async iterator (verified). Keep a regression test that overrides with a plain async iterator lacking aclose(), and one asserting a custom iterator's aclose() is invoked.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/httpx2/models/test_responses.py, line 497:
<comment>These rewritten tests only exercise async-generator overrides, so they pass without exercising the two behaviors this PR claims to restore/preserve: subclass overrides returning plain async iterators without `aclose()`, and custom iterators whose `aclose()` gets called on cleanup. The previous `PlainAsyncIterator` and `ClosableAsyncIterator` regression tests for those paths were removed here. At the current implementation, `aread()` and `aiter_bytes()` wrap the `aiter_bytes()`/`aiter_raw()` results in `contextlib.aclosing(...)`, which unconditionally awaits `.aclose()` and raises AttributeError on a plain async iterator (verified). Keep a regression test that overrides with a plain async iterator lacking `aclose()`, and one asserting a custom iterator's `aclose()` is invoked.</comment>
<file context>
@@ -523,49 +494,25 @@ async def test_aiter_raw_with_chunksize() -> None:
@pytest.mark.anyio
-async def test_aiter_bytes_override_with_plain_async_iterator() -> None:
+async def test_aiter_bytes_override() -> None:
class CustomResponse(httpx2.Response):
- def aiter_bytes(self, chunk_size: int | None = None) -> typing.AsyncIterator[bytes]:
</file context>
| @@ -972,11 +972,12 @@ async def aread(self) -> bytes: | |||
| Read and return the response content. | |||
There was a problem hiding this comment.
P2: When an AsyncByteStream returns a custom iterator with aclose(), aiter_raw() skips that cleanup because AsyncGenerator is not a structural check for custom iterators. Detect and await an exposed aclose() method instead.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/httpx2/httpx2/_models.py, line 1059:
<comment>When an `AsyncByteStream` returns a custom iterator with `aclose()`, `aiter_raw()` skips that cleanup because `AsyncGenerator` is not a structural check for custom iterators. Detect and await an exposed `aclose()` method instead.</comment>
<file context>
@@ -1066,7 +1056,7 @@ async def aiter_raw(self, chunk_size: int | None = None) -> typing.AsyncIterator
yield chunk
finally:
- if isinstance(stream, _AsyncClosable):
+ if isinstance(stream, AsyncGenerator):
await stream.aclose()
await self.aclose()
</file context>
Summary
AsyncIterator[bytes]as the return type ofResponse.aiter_bytes()andResponse.aiter_raw().contextlib.aclosing()for async generators while allowing plain async iterator overrides.Closes #1158.
Validation
uv run ruff format --check --diff src/httpx2/httpx2/_models.py tests/httpx2/models/test_responses.pyuv run ruff check src/httpx2/httpx2/_models.py tests/httpx2/models/test_responses.pyuv run mypy src/httpx2 tests/httpx2 src/httpcore2 tests/httpcore2uv run python scripts/unasync.py --checkGITHUB_ACTIONS=1 scripts/test && scripts/coverage- 1,998 passed, 1 skipped, 100% coverageAI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.