Replace BasicAuth instance check with Protocol check
- Dominant language
- Python
- Stars
- 16.5k
- Forks
- 2.4k
- Avg merge
- 17h 22m
- Merged PRs (30d)
- 212
Description
🐣 **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.
Contributor guide
Assessment
This issue has not been assessed yet.