From 64cbb0f70e0d74459982a010b79fb400288adf8c Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Sat, 23 May 2020 12:47:39 +0200 Subject: [PATCH] Allow template render to string helper functions to accept Context (#372) * Add failing test case for render_to_string * Allow Context objects in template render functions --- django-stubs/shortcuts.pyi | 5 +++-- django-stubs/template/loader.pyi | 3 ++- test-data/typecheck/template/test_loader.yml | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test-data/typecheck/template/test_loader.yml diff --git a/django-stubs/shortcuts.pyi b/django-stubs/shortcuts.pyi index 8ac4501..c9b8acb 100644 --- a/django-stubs/shortcuts.pyi +++ b/django-stubs/shortcuts.pyi @@ -9,10 +9,11 @@ from django.http.response import ( from django.db.models import Manager, QuerySet from django.http import HttpRequest +from django.template.context import Context def render_to_response( template_name: Union[str, Sequence[str]], - context: Optional[Dict[str, Any]] = ..., + context: Optional[Union[Context, Dict[str, Any]]] = ..., content_type: Optional[str] = ..., status: Optional[int] = ..., using: Optional[str] = ..., @@ -20,7 +21,7 @@ def render_to_response( def render( request: HttpRequest, template_name: Union[str, Sequence[str]], - context: Optional[Dict[str, Any]] = ..., + context: Optional[Union[Context, Dict[str, Any]]] = ..., content_type: Optional[str] = ..., status: Optional[int] = ..., using: Optional[str] = ..., diff --git a/django-stubs/template/loader.pyi b/django-stubs/template/loader.pyi index 0cd3214..f52fccc 100644 --- a/django-stubs/template/loader.pyi +++ b/django-stubs/template/loader.pyi @@ -1,5 +1,6 @@ from typing import Any, Dict, List, Optional, Union from . import engines as engines # noqa: F401 +from .context import Context from django.http.request import HttpRequest from django.template.exceptions import TemplateDoesNotExist as TemplateDoesNotExist # noqa: F401 @@ -8,7 +9,7 @@ def get_template(template_name: str, using: Optional[str] = ...) -> Any: ... def select_template(template_name_list: Union[List[str], str], using: Optional[str] = ...) -> Any: ... def render_to_string( template_name: Union[List[str], str], - context: Optional[Dict[str, Any]] = ..., + context: Optional[Union[Context, Dict[str, Any]]] = ..., request: Optional[HttpRequest] = ..., using: Optional[str] = ..., ) -> str: ... diff --git a/test-data/typecheck/template/test_loader.yml b/test-data/typecheck/template/test_loader.yml new file mode 100644 index 0000000..3f2ce16 --- /dev/null +++ b/test-data/typecheck/template/test_loader.yml @@ -0,0 +1,15 @@ +- case: loader_render_to_string_accepts_context + main: | + from django.http.request import HttpRequest + from django.shortcuts import render, render_to_response + from django.template.loader import render_to_string + from django.template import Context, RequestContext + + request = HttpRequest() + request_context = RequestContext(request, {'foo': 'bar'}) + + render_to_string('foobar', {'foo': 'bar'}) + render_to_string('foobar', Context({'foo': 'bar'})) + render_to_string('foobar', request_context) + render(request, '403.html', request_context, status=403) + render_to_response('403.html', request_context)