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:
75337a6050

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
This commit is contained in:
PIG208
2021-08-16 15:59:04 +08:00
committed by GitHub
parent a6a81797d3
commit 0d8dd85442
4 changed files with 7 additions and 9 deletions

View File

@@ -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]: ...

View File

@@ -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: ...

View File

@@ -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: ...

View File

@@ -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):