python / python/typeshed

AsyncOAuth2Client missing async context manager protocol (__aenter__, __aexit__)

Đang mở
#15,246 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

@robertuss12 đang làm issue này rồi.

Từ ngày 29/12/2025.

  • #15190 của @robertuss12 — đang mở
Ngôn ngữ chính
Python
Star
5.1k
Fork
2.1k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
82

Mô tả

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 URL: https://github.com/python/typeshed/issues/new

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi, đặc biệt là khai báo AsyncOAuth2Client quanh các dòng 26–48, và xem xét pull request #15190 được liên kết. Kiểm tra ví dụ async with bằng Pyright; được coi là hoàn thành khi stub biểu露 hành vi context manager trong runtime mà không có các lỗi protocol đó, đồng thời xử lý mối quan ngại có thể có về sync OAuth2Client nếu nằm trong phạm vi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.