From a1d3aec3a2ed846cbabf9f9aa44721fcc8292522 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Tue, 30 Aug 2022 14:05:39 +0200 Subject: [PATCH] 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. --- django-stubs/shortcuts.pyi | 6 +++--- tests/typecheck/test_shortcuts.yml | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/django-stubs/shortcuts.pyi b/django-stubs/shortcuts.pyi index 3e43b7d..ad3f2c7 100644 --- a/django-stubs/shortcuts.pyi +++ b/django-stubs/shortcuts.pyi @@ -21,8 +21,8 @@ class SupportsGetAbsoluteUrl(Protocol): def get_absolute_url(self) -> str: ... @overload -def redirect( # type: ignore - to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True] = ..., **kwargs: Any +def redirect( + to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True], **kwargs: Any ) -> HttpResponsePermanentRedirect: ... @overload def redirect( @@ -30,7 +30,7 @@ def redirect( ) -> HttpResponseRedirect: ... @overload def redirect( - to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool = ..., **kwargs: Any + to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool, **kwargs: Any ) -> Union[HttpResponseRedirect, HttpResponsePermanentRedirect]: ... _T = TypeVar("_T", bound=Model) diff --git a/tests/typecheck/test_shortcuts.yml b/tests/typecheck/test_shortcuts.yml index 976656b..99769fe 100644 --- a/tests/typecheck/test_shortcuts.yml +++ b/tests/typecheck/test_shortcuts.yml @@ -54,6 +54,7 @@ from django.shortcuts import redirect reveal_type(redirect(to = '', permanent = True)) # N: Revealed type is "django.http.response.HttpResponsePermanentRedirect" reveal_type(redirect(to = '', permanent = False)) # N: Revealed type is "django.http.response.HttpResponseRedirect" + reveal_type(redirect(to = '')) # N: Revealed type is "django.http.response.HttpResponseRedirect" var = True reveal_type(redirect(to = '', permanent = var)) # N: Revealed type is "Union[django.http.response.HttpResponseRedirect, django.http.response.HttpResponsePermanentRedirect]"