aio-libs / aio-libs/aiohttp

Replace BasicAuth instance check with Protocol check

Abierto
#4,690 1 comentario 3 reacciones 0 asignados Ver en GitHub
enhancement
Lenguaje dominante
Python
Estrellas
16.5k
Forks
2.4k
Merge medio
17 h 22 min
PR fusionados (30 d)
212

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.