Skip to content

Commit 8d26001

Browse files
kszmigielKacper Szmigiel
and
Kacper Szmigiel
authored
Issue 355 (#376)
* Mapping instead of Dict type annotation for context in render() with test * removed Union and Context * typo Co-authored-by: Kacper Szmigiel <[email protected]>
1 parent 570772f commit 8d26001

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

django-stubs/shortcuts.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Dict, List, Optional, Protocol, Sequence, Type, TypeVar, Union
1+
from typing import Any, Callable, List, Mapping, Optional, Protocol, Sequence, Type, TypeVar, Union
22

33
from django.db.models.base import Model
44
from django.http.response import (
@@ -12,15 +12,15 @@ from django.http import HttpRequest
1212

1313
def render_to_response(
1414
template_name: Union[str, Sequence[str]],
15-
context: Optional[Dict[str, Any]] = ...,
15+
context: Optional[Mapping[str, Any]] = ...,
1616
content_type: Optional[str] = ...,
1717
status: Optional[int] = ...,
1818
using: Optional[str] = ...,
1919
) -> HttpResponse: ...
2020
def render(
2121
request: HttpRequest,
2222
template_name: Union[str, Sequence[str]],
23-
context: Optional[Dict[str, Any]] = ...,
23+
context: Optional[Mapping[str, Any]] = ...,
2424
content_type: Optional[str] = ...,
2525
status: Optional[int] = ...,
2626
using: Optional[str] = ...,

test-data/typecheck/test_shortcuts.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@
3636
from django.db import models
3737
class MyUser(models.Model):
3838
pass
39+
40+
- case: check_render_function_arguments_annotations
41+
main: |
42+
from typing import Any
43+
from typing_extensions import TypedDict
44+
from django.shortcuts import render
45+
from django.http.request import HttpRequest
46+
47+
TestContext = TypedDict("TestContext", {"user": Any})
48+
test_context: TestContext = {"user": "test"}
49+
reveal_type(test_context) # N: Revealed type is 'TypedDict('main.TestContext', {'user': Any})'
50+
reveal_type(render(HttpRequest(), '', test_context)) # N: Revealed type is 'django.http.response.HttpResponse'
51+

0 commit comments

Comments
 (0)