Skip to content

Commit a26f96a

Browse files
authored
django.contrib.auth: Relax ModelBackend -> BaseBackend (#2141)
`BaseBackend` is the base interface of django backends, `ModelBackend` has more specific signatures, such as `.authenticate()` including `username` and `password`, not all authentication backends use those. For instance, since the `authenticate()` method in BaseBackend already includes request (`HttpRequest`), it can read a `Bearer` token or cookie, and already accepts `**kwargs` for additional parameters. See also: - BaseBackend: https://github.com/django/django/blob/5.0.6/django/contrib/auth/backends.py#L8-L28 - ModelBackend: https://github.com/django/django/blob/5.0.6/django/contrib/auth/backends.py#L31-L160 - django.contrib.auth.authenticate(): https://github.com/django/django/blob/5.0.6/django/contrib/auth/__init__.py#L65-L93 - Django Docs for "Writing an authentication backend": https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#writing-an-authentication-backend
1 parent 5216eaf commit a26f96a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

django-stubs/contrib/auth/__init__.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any
22

3-
from django.contrib.auth.backends import ModelBackend
3+
from django.contrib.auth.backends import BaseBackend
44
from django.contrib.auth.base_user import AbstractBaseUser
55
from django.contrib.auth.models import AnonymousUser
66
from django.db.models.options import Options
@@ -16,15 +16,15 @@ BACKEND_SESSION_KEY: str
1616
HASH_SESSION_KEY: str
1717
REDIRECT_FIELD_NAME: str
1818

19-
def load_backend(path: str) -> ModelBackend: ...
20-
def get_backends() -> list[ModelBackend]: ...
19+
def load_backend(path: str) -> BaseBackend: ...
20+
def get_backends() -> list[BaseBackend]: ...
2121
def authenticate(request: HttpRequest | None = ..., **credentials: Any) -> AbstractBaseUser | None: ...
2222
async def aauthenticate(request: HttpRequest | None = ..., **credentials: Any) -> AbstractBaseUser | None: ...
2323
def login(
24-
request: HttpRequest, user: AbstractBaseUser | None, backend: type[ModelBackend] | str | None = ...
24+
request: HttpRequest, user: AbstractBaseUser | None, backend: type[BaseBackend] | str | None = ...
2525
) -> None: ...
2626
async def alogin(
27-
request: HttpRequest, user: AbstractBaseUser | None, backend: type[ModelBackend] | str | None = ...
27+
request: HttpRequest, user: AbstractBaseUser | None, backend: type[BaseBackend] | str | None = ...
2828
) -> None: ...
2929
def logout(request: HttpRequest) -> None: ...
3030
async def alogout(request: HttpRequest) -> None: ...

0 commit comments

Comments
 (0)