AsyncOAuth2Client missing async context manager protocol (__aenter__, __aexit__)
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
Problem
The AsyncOAuth2Client class in stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi is missing type annotations for the async context manager protocol (__aenter__ and __aexit__), causing type checkers to incorrectly flag valid async with usage as errors.
Expected Behavior
AsyncOAuth2Client should be usable as an async context manager since it inherits from httpx.AsyncClient at runtime:
from authlib.integrations.httpx_client import AsyncOAuth2Client
async with AsyncOAuth2Client(client_id="...", client_secret="...") as client:
response = await client.get("https://api.example.com/data")
Actual Behavior
Pyright reports errors:
error: Object of type "AsyncOAuth2Client" cannot be used with "async with" because it does not correctly implement __aenter__
Attribute "__aenter__" is unknown (reportGeneralTypeIssues)
error: Object of type "AsyncOAuth2Client" cannot be used with "with" because it does not correctly implement __aexit__
Attribute "__aexit__" is unknown (reportGeneralTypeIssues)
Root Cause
The stub file at line 26-27 shows:
# Inherits from httpx.AsyncClient
class AsyncOAuth2Client(_OAuth2Client):
The comment is correct but the type stub is incomplete. The class should expose the async context manager protocol from httpx.AsyncClient, but it's not properly modeled in the stubs.
Runtime Verification
At runtime, AsyncOAuth2Client does have these methods:
from authlib.integrations.httpx_client import AsyncOAuth2Client
print(hasattr(AsyncOAuth2Client, '__aenter__')) # True
print(hasattr(AsyncOAuth2Client, '__aexit__')) # True
Proposed Fix
Update stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi to explicitly declare the async context manager protocol:
from typing_extensions import Self
class AsyncOAuth2Client(_OAuth2Client):
SESSION_REQUEST_PARAMS: list[str]
client_auth_class = OAuth2ClientAuth
token_auth_class = OAuth2Auth
oauth_error_class = OAuthError
def __init__(
self,
client_id=None,
client_secret=None,
token_endpoint_auth_method=None,
revocation_endpoint_auth_method=None,
scope=None,
redirect_uri=None,
token=None,
token_placement="header",
update_token=None,
leeway=60,
**kwargs,
) -> None: ...
async def __aenter__(self) -> Self: ...
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: ...
async def request(self, method, url, withhold_token: bool = False, auth=..., **kwargs): ...
async def stream(self, method, url, withhold_token: bool = False, auth=..., **kwargs) -> Generator[Incomplete]: ...
async def ensure_active_token(self, token): ...
Similarly, OAuth2Client (the sync version) may need __enter__ and __exit__ methods.
Affected Versions
types-Authlib>=1.4.0(when httpx_client stubs were added in PR #15147)- Works correctly in
types-Authlib==1.3.0.20241229(no httpx_client stubs existed)
Related Files
stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi(line 26-48)
Labels to add: stubs: incomplete
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi 开始,尤其检查第 26–48 行附近的 AsyncOAuth2Client 声明,并审阅链接的 pull request #15190。使用 Pyright 检查 async with 示例;完成的标准是 stub 能够暴露运行时的 context-manager 行为,且不会出现这些 protocol 错误;如果属于范围,还需要处理可能存在的 sync OAuth2Client 问题。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100