Skip to content

docs(python): Change return type of context manager methods to Self - #646

Merged
0x676e67 merged 1 commit into
0x676e67:mainfrom
SausageMan99:fix/645-context-manager-self
Oct 12, 2026
Merged

0x676e67 merged 1 commit into
0x676e67:mainfrom
SausageMan99:fix/645-context-manager-self

Conversation

@SausageMan99

Copy link
Copy Markdown
Contributor

Closes #645.

The __enter__ / __aenter__ stubs of Client, Response, Streamer and WebSocket (both wreq and wreq.blocking) were annotated -> Any, so async with wreq.Client() as c: gave c no type and editors lost completion. The native methods all return the instance itself (coroutine::ready(..., slf) / PyRef<Self>), so typing.Self is accurate. Self is available since Python 3.11, which matches requires-python.

_RequestCoroutine.__aenter__ -> _T is 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:

MYPYPATH=python mypy --python-version 3.11 --disable-error-code empty-body check.py
before after
async with wreq.Client() as c Any wreq.wreq.Client
async with await c.get(...) as r Any wreq.wreq.Response
async with r.stream() as s / with r.stream() as s Any wreq.wreq.Streamer
async with await c.websocket(...) as ws Any wreq.wreq.WebSocket
with wreq.blocking.Client() as c (and its Response / WebSocket) Any wreq.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

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>
@0x676e67
0x676e67 merged commit da1028e into 0x676e67:main Oct 12, 2026
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.

__aenter__ method return types

2 participants