mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-17 17:35:59 +08:00
The return type for calling `shorcuts.render` without providing a value for the `permanent` kwarg was `HttpResponsePermanentRedirect`, while it should be `HttpResponseRedirect`. The reason is that the first two overloads of the type stub overlap for the case of using the default argument. While `mypy` does issue an error for this, it was previously ignored with the `# type: ignore` comment. As the first overload annotates the function as having the return type `HttpResponsePermanentRedirect`, this would make mypy assume that the return type is that instead of `HttpResponseRedirect`. Since calling `django.shortcuts.redirect` without providing an argument for `permanent` is the same as calling it with a `Literal[False]`, as the default value is a `False`, we can improve the stub by only specifying the option to use the default argument (`= ...`) in the second overload. This also removes the overlap in stub definitions, meaning that the `# type: ignore` can now be removed. This commit fixes #1138.
41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
from typing import Any, Callable, List, Mapping, Optional, Protocol, Sequence, Type, TypeVar, Union, overload
|
|
|
|
from django.db.models import Manager, QuerySet
|
|
from django.db.models.base import Model
|
|
from django.http import HttpRequest
|
|
from django.http.response import HttpResponse as HttpResponse
|
|
from django.http.response import HttpResponsePermanentRedirect as HttpResponsePermanentRedirect
|
|
from django.http.response import HttpResponseRedirect as HttpResponseRedirect
|
|
from typing_extensions import Literal
|
|
|
|
def render(
|
|
request: HttpRequest,
|
|
template_name: Union[str, Sequence[str]],
|
|
context: Optional[Mapping[str, Any]] = ...,
|
|
content_type: Optional[str] = ...,
|
|
status: Optional[int] = ...,
|
|
using: Optional[str] = ...,
|
|
) -> HttpResponse: ...
|
|
|
|
class SupportsGetAbsoluteUrl(Protocol):
|
|
def get_absolute_url(self) -> str: ...
|
|
|
|
@overload
|
|
def redirect(
|
|
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True], **kwargs: Any
|
|
) -> HttpResponsePermanentRedirect: ...
|
|
@overload
|
|
def redirect(
|
|
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[False] = ..., **kwargs: Any
|
|
) -> HttpResponseRedirect: ...
|
|
@overload
|
|
def redirect(
|
|
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool, **kwargs: Any
|
|
) -> Union[HttpResponseRedirect, HttpResponsePermanentRedirect]: ...
|
|
|
|
_T = TypeVar("_T", bound=Model)
|
|
|
|
def get_object_or_404(klass: Union[Type[_T], Manager[_T], QuerySet[_T]], *args: Any, **kwargs: Any) -> _T: ...
|
|
def get_list_or_404(klass: Union[Type[_T], Manager[_T], QuerySet[_T]], *args: Any, **kwargs: Any) -> List[_T]: ...
|
|
def resolve_url(to: Union[Callable, Model, str], *args: Any, **kwargs: Any) -> str: ...
|