Skip to content

Commit a1d3aec

Browse files
authored
Fix return type for django.shortcuts.render (#1140)
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.
1 parent 74e31c7 commit a1d3aec

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

django-stubs/shortcuts.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class SupportsGetAbsoluteUrl(Protocol):
2121
def get_absolute_url(self) -> str: ...
2222

2323
@overload
24-
def redirect( # type: ignore
25-
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True] = ..., **kwargs: Any
24+
def redirect(
25+
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True], **kwargs: Any
2626
) -> HttpResponsePermanentRedirect: ...
2727
@overload
2828
def redirect(
2929
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[False] = ..., **kwargs: Any
3030
) -> HttpResponseRedirect: ...
3131
@overload
3232
def redirect(
33-
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool = ..., **kwargs: Any
33+
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool, **kwargs: Any
3434
) -> Union[HttpResponseRedirect, HttpResponsePermanentRedirect]: ...
3535

3636
_T = TypeVar("_T", bound=Model)

tests/typecheck/test_shortcuts.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from django.shortcuts import redirect
5555
reveal_type(redirect(to = '', permanent = True)) # N: Revealed type is "django.http.response.HttpResponsePermanentRedirect"
5656
reveal_type(redirect(to = '', permanent = False)) # N: Revealed type is "django.http.response.HttpResponseRedirect"
57+
reveal_type(redirect(to = '')) # N: Revealed type is "django.http.response.HttpResponseRedirect"
5758
5859
var = True
5960
reveal_type(redirect(to = '', permanent = var)) # N: Revealed type is "Union[django.http.response.HttpResponseRedirect, django.http.response.HttpResponsePermanentRedirect]"

0 commit comments

Comments
 (0)