Repository navigation
docs(python): Change return type of context manager methods to Self - #646
Merged
0x676e67 merged 1 commit intoOct 12, 2026
Merged
Conversation
The `__enter__` and `__aenter__` stubs of Client, Response, Streamer and WebSocket (async and blocking) were typed as `Any`, so a type checker lost the type of the bound name in `with`/`async with`. The native methods all return the instance itself, so `Self` is accurate. Closes 0x676e67#645 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #645.
The
__enter__/__aenter__stubs ofClient,Response,StreamerandWebSocket(bothwreqandwreq.blocking) were annotated-> Any, soasync with wreq.Client() as c:gavecno type and editors lost completion. The native methods all return the instance itself (coroutine::ready(..., slf)/PyRef<Self>), sotyping.Selfis accurate.Selfis available since Python 3.11, which matchesrequires-python._RequestCoroutine.__aenter__ -> _Tis left as is, since it already yields the awaited result.Verification — there is no type checker in CI, so I checked with mypy on a small script using
reveal_type:async with wreq.Client() as cAnywreq.wreq.Clientasync with await c.get(...) as rAnywreq.wreq.Responseasync with r.stream() as s/with r.stream() as sAnywreq.wreq.Streamerasync with await c.websocket(...) as wsAnywreq.wreq.WebSocketwith wreq.blocking.Client() as c(and itsResponse/WebSocket)Anywreq.blocking.*No runtime code changes.
This change was prepared with the help of an AI assistant (Claude) and reviewed by me before opening.
🤖 Generated with Claude Code