From e05b84e32da70609b96470f4f51ed7be645c6951 Mon Sep 17 00:00:00 2001 From: Kacper Date: Wed, 3 Jun 2020 22:46:30 +0200 Subject: [PATCH] Issue 378 (#387) * proper redirect return type annotations made with Literal * Mapping instead of Dict type annotation for context in render() with test * removed Union and Context * typo Co-authored-by: Kacper Szmigiel Add __init__ to OrderedSet (#381) Issue 382 (#384) * WIP fix, pushed for testing * added _ prefix for internal types Co-authored-by: Kacper Szmigiel Fix parameter types for assertJSONEqual/NotEqual (#385) Add get_supported_language_variant (#386) Issue 309 (#383) * added tags for user models * type test for HttpRequest.user * test for User and AnonymousUser tags * httrequest test fix * checking python version fix for readibility * Rewrite version check for readability * Annotate is_authenticated/is_anonymous with Literal-type * Add auth in INSTALLED_APPS in test * Fix wrong type assertion in test * Fix misconception of how branch-testing works * Remove user from WSGIRequest * Change HttpRequest-transformer to set user-type to include AnonymousUser * Add check for anonymous_user_info=None to appease mypy * Isort transformers/request * Remove trailing whitespace * Remove unused import Co-authored-by: Kacper Szmigiel * fix formatting and unused import * reformatted again Co-authored-by: Kacper Szmigiel --- django-stubs/shortcuts.pyi | 17 ++++++++++++++++- test-data/typecheck/test_shortcuts.yml | 8 ++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/django-stubs/shortcuts.pyi b/django-stubs/shortcuts.pyi index baae36d..736ee26 100644 --- a/django-stubs/shortcuts.pyi +++ b/django-stubs/shortcuts.pyi @@ -1,4 +1,5 @@ -from typing import Any, Callable, List, Mapping, Optional, Protocol, Sequence, Type, TypeVar, Union +import sys +from typing import Any, Callable, List, Mapping, Optional, overload, Protocol, Sequence, Type, TypeVar, Union from django.db.models.base import Model from django.http.response import ( @@ -10,6 +11,11 @@ from django.http.response import ( from django.db.models import Manager, QuerySet from django.http import HttpRequest +if sys.version_info < (3, 8): + from typing_extensions import Literal +else: + from typing import Literal + def render_to_response( template_name: Union[str, Sequence[str]], context: Optional[Mapping[str, Any]] = ..., @@ -28,6 +34,15 @@ def render( class SupportsGetAbsoluteUrl(Protocol): ... +@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]: ... diff --git a/test-data/typecheck/test_shortcuts.yml b/test-data/typecheck/test_shortcuts.yml index dc11929..a06dcf6 100644 --- a/test-data/typecheck/test_shortcuts.yml +++ b/test-data/typecheck/test_shortcuts.yml @@ -49,3 +49,11 @@ reveal_type(test_context) # N: Revealed type is 'TypedDict('main.TestContext', {'user': Any})' reveal_type(render(HttpRequest(), '', test_context)) # N: Revealed type is 'django.http.response.HttpResponse' +- case: check_redirect_return_annotation + main: | + 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' + + var = True + reveal_type(redirect(to = '', permanent = var)) # N: Revealed type is 'Union[django.http.response.HttpResponseRedirect, django.http.response.HttpResponsePermanentRedirect]'