aio-libs / aio-libs/aiohttp

Replace BasicAuth instance check with Protocol check

未关闭
#4,690 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
enhancement
主要语言
Python
星标
16.5k
派生
2.4k
平均合并
17 小时 22 分钟
30 天内合并 PR
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 摘要。