aio-libs / aio-libs/aiohttp

Replace BasicAuth instance check with Protocol check

オープン
#4,690 コメント 1 件 リアクション 3 件 担当者 0 名 GitHub で見る
enhancement
主要言語
Python
スター
16.5k
フォーク
2.4k
平均マージ
17時間 22分
マージ済み PR(30日)
212

説明

🐣 **Is your feature request related to a problem? Please describe.**

The server I am authenticating against uses bearer token for the authentication header. For that I like to use a custom auth class and use it with a dedicated `ClientSession` as the `auth` parameter.
Regarding `ClientSession` this is only a type hint issue but down in [`ClientRequest.update_auth`](https://github.com/aio-libs/aiohttp/blob/master/aiohttp/client_reqrep.py#L415) the instance check for `BasicAuth` brakes my approach.

💡 **Describe the solution you'd like**

It feels that the auth instance only needs to have an `encode` method to work. Because of that I'd propose defining a protocol for auth and do an instance check on that.

```python
from typing import Protocol, runtime_checkable

@runtime_checkable
class AuthProtocol(Protocol):
def encode(self) -> str:
...

class ClientRequest:
...
def update_auth(self):
...
if not isinstance(auth, Protocol):
...

def update_proxy(self):
...
if proxy_auth and not isinstance(proxy_auth, Protocol):
...
```

❓ **Describe alternatives you've considered**

The proposed solution only works with >= Python 3.8. Other alternatives that come to mind are:

- Define an ABC.
- Replacing the instance check with an attribute check.
- Just remove the instance check.

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

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

評価

この issue はまだ評価されていません。

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

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