Skip to content

Commit e05b84e

Browse files
kszmigielKacper Szmigiel
and
Kacper Szmigiel
authored
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 <[email protected]> Add __init__ to OrderedSet (#381) Issue 382 (#384) * WIP fix, pushed for testing * added _ prefix for internal types Co-authored-by: Kacper Szmigiel <[email protected]> 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 <[email protected]> * fix formatting and unused import * reformatted again Co-authored-by: Kacper Szmigiel <[email protected]>
1 parent 71751d3 commit e05b84e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

django-stubs/shortcuts.pyi

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, Callable, List, Mapping, Optional, Protocol, Sequence, Type, TypeVar, Union
1+
import sys
2+
from typing import Any, Callable, List, Mapping, Optional, overload, Protocol, Sequence, Type, TypeVar, Union
23

34
from django.db.models.base import Model
45
from django.http.response import (
@@ -10,6 +11,11 @@ from django.http.response import (
1011
from django.db.models import Manager, QuerySet
1112
from django.http import HttpRequest
1213

14+
if sys.version_info < (3, 8):
15+
from typing_extensions import Literal
16+
else:
17+
from typing import Literal
18+
1319
def render_to_response(
1420
template_name: Union[str, Sequence[str]],
1521
context: Optional[Mapping[str, Any]] = ...,
@@ -28,6 +34,15 @@ def render(
2834

2935
class SupportsGetAbsoluteUrl(Protocol): ...
3036

37+
@overload
38+
def redirect(
39+
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[True], **kwargs: Any
40+
) -> HttpResponsePermanentRedirect: ...
41+
@overload
42+
def redirect(
43+
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: Literal[False], **kwargs: Any
44+
) -> HttpResponseRedirect: ...
45+
@overload
3146
def redirect(
3247
to: Union[Callable, str, SupportsGetAbsoluteUrl], *args: Any, permanent: bool = ..., **kwargs: Any
3348
) -> Union[HttpResponseRedirect, HttpResponsePermanentRedirect]: ...

test-data/typecheck/test_shortcuts.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@
4949
reveal_type(test_context) # N: Revealed type is 'TypedDict('main.TestContext', {'user': Any})'
5050
reveal_type(render(HttpRequest(), '', test_context)) # N: Revealed type is 'django.http.response.HttpResponse'
5151
52+
- case: check_redirect_return_annotation
53+
main: |
54+
from django.shortcuts import redirect
55+
reveal_type(redirect(to = '', permanent = True)) # N: Revealed type is 'django.http.response.HttpResponsePermanentRedirect'
56+
reveal_type(redirect(to = '', permanent = False)) # N: Revealed type is 'django.http.response.HttpResponseRedirect'
57+
58+
var = True
59+
reveal_type(redirect(to = '', permanent = var)) # N: Revealed type is 'Union[django.http.response.HttpResponseRedirect, django.http.response.HttpResponsePermanentRedirect]'

0 commit comments

Comments
 (0)