From 0d8dd854422a1a1c8ce18a39c66a7fb86da65b4e Mon Sep 17 00:00:00 2001 From: PIG208 <39874143+PIG208@users.noreply.github.com> Date: Mon, 16 Aug 2021 15:59:04 +0800 Subject: [PATCH] Minor fixes to improve django-stubs (#695) * Use `Sequence` instead of `Iterable` for `send_messages`. According to the documentation (https://docs.djangoproject.com/en/3.2/topics/email/#email-backends), `email_messages` is a list. Using `Iterable` will make it hard for subclasses to implement this method utilizing functions like `__len__`. While this still allows subclasses to accept `Iterable`. * Fix function signature of `authenticate` of `BaseBackend`. 1. BaseBackend no longer requires the username and password argument. They were removed 3 years ago in the commit below when `BaseBackend` is added: https://github.com/django/django/commit/75337a60509fdfdd321a5caf8e30d57fff6b9518 2. `request` is optional for `authenticate` method. According to django documentation, the authenticate method does not necessarily require the request object. https://docs.djangoproject.com/en/3.2/topics/auth/default/#authenticating-users * Tighten the type of `streaming_content` to `Iterator[bytes]`. It is an iterator of a bytestring according to the documentation: https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.StreamingHttpResponse.streaming_content * Fix function signature of `django.contrib.staticfiles.serve`. Since this `serve` function uses `django.views.static.serve` that accepts `HttpRequest` as its first argument, it is more reasonable to type it with `HttpRequest` instead of `WSGIRequest`. Related: https://github.com/django/django/blob/main/django/contrib/staticfiles/views.py#L39 --- django-stubs/contrib/auth/backends.pyi | 4 +--- django-stubs/contrib/staticfiles/views.pyi | 4 ++-- django-stubs/core/mail/backends/base.pyi | 4 ++-- django-stubs/http/response.pyi | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/django-stubs/contrib/auth/backends.pyi b/django-stubs/contrib/auth/backends.pyi index 74dcbba..9127221 100644 --- a/django-stubs/contrib/auth/backends.pyi +++ b/django-stubs/contrib/auth/backends.pyi @@ -10,9 +10,7 @@ _AnyUser = Union[Model, AnonymousUser] UserModel: Any class BaseBackend: - def authenticate( - self, request: HttpRequest, username: Optional[str] = ..., password: Optional[str] = ..., **kwargs: Any - ) -> Optional[AbstractBaseUser]: ... + def authenticate(self, request: Optional[HttpRequest], **kwargs: Any) -> Optional[AbstractBaseUser]: ... def get_user(self, user_id: int) -> Optional[AbstractBaseUser]: ... def get_user_permissions(self, user_obj: _AnyUser, obj: Optional[Model] = ...) -> Set[str]: ... def get_group_permissions(self, user_obj: _AnyUser, obj: Optional[Model] = ...) -> Set[str]: ... diff --git a/django-stubs/contrib/staticfiles/views.pyi b/django-stubs/contrib/staticfiles/views.pyi index 8d1ef04..0f5fbb0 100644 --- a/django-stubs/contrib/staticfiles/views.pyi +++ b/django-stubs/contrib/staticfiles/views.pyi @@ -1,6 +1,6 @@ from typing import Any -from django.core.handlers.wsgi import WSGIRequest +from django.http.request import HttpRequest from django.http.response import FileResponse -def serve(request: WSGIRequest, path: str, insecure: bool = ..., **kwargs: Any) -> FileResponse: ... +def serve(request: HttpRequest, path: str, insecure: bool = ..., **kwargs: Any) -> FileResponse: ... diff --git a/django-stubs/core/mail/backends/base.pyi b/django-stubs/core/mail/backends/base.pyi index 7aec2dd..6e73e69 100644 --- a/django-stubs/core/mail/backends/base.pyi +++ b/django-stubs/core/mail/backends/base.pyi @@ -1,5 +1,5 @@ import types -from typing import Any, Iterable, Optional, Type, TypeVar +from typing import Any, Optional, Sequence, Type, TypeVar from django.core.mail.message import EmailMessage @@ -13,4 +13,4 @@ class BaseEmailBackend: def __exit__( self, exc_type: Type[BaseException], exc_value: BaseException, traceback: types.TracebackType ) -> None: ... - def send_messages(self, email_messages: Iterable[EmailMessage]) -> int: ... + def send_messages(self, email_messages: Sequence[EmailMessage]) -> int: ... diff --git a/django-stubs/http/response.pyi b/django-stubs/http/response.pyi index 1c67e5a..ffa44e4 100644 --- a/django-stubs/http/response.pyi +++ b/django-stubs/http/response.pyi @@ -101,8 +101,8 @@ class HttpResponse(HttpResponseBase): class StreamingHttpResponse(HttpResponseBase): content: Any - streaming_content: Iterator[Any] - def __init__(self, streaming_content: Iterable[Any] = ..., *args: Any, **kwargs: Any) -> None: ... + streaming_content: Iterator[bytes] + def __init__(self, streaming_content: Iterable[bytes] = ..., *args: Any, **kwargs: Any) -> None: ... def getvalue(self) -> bytes: ... class FileResponse(StreamingHttpResponse):