python / python/typeshed

AsyncOAuth2Client missing async context manager protocol (__aenter__, __aexit__)

オープン
#15,246 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

@robertuss12 がすでに取り組んでいます。

2025年12月29日 から。

  • #15190 @robertuss12 による — オープン
主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
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 URL: https://github.com/python/typeshed/issues/new

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

stubs/Authlib/authlib/integrations/httpx_client/oauth2_client.pyi から始め、特に 26–48 行付近の AsyncOAuth2Client 宣言を確認し、リンクされている pull request #15190 をレビューしてください。Pyright を使って async with の例を確認します。完了の条件は、stub がこれらの protocol エラーなしで実行時の context-manager 動作を公開し、対象範囲に含まれる場合は sync OAuth2Client に関する可能性のある懸念にも対処されていることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。