aio-libs / aio-libs/aiohttp

Replace BasicAuth instance check with Protocol check

Ouverte
#4,690 1 commentaire 3 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
Python
Étoiles
16.5k
Forks
2.4k
Merge moyen
17 h 22 min
PR mergées (30 j)
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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.