aio-libs / aio-libs/aiohttp

Replace BasicAuth instance check with Protocol check

Đang mở
#4,690 1 bình luận 3 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
17 giờ 22 phút
Pull request đã merge (30 ngày)
212

Mô tả

🐣 **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.

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

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

Đánh giá

Issue này chưa được đánh giá.

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.