From 69d63e7b2df088f4beae5b7ff584cda5c9e6b4d1 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 08:38:36 -0400 Subject: [PATCH 1/9] template.base: Template.render returns SafeText --- django-stubs/template/base.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-stubs/template/base.pyi b/django-stubs/template/base.pyi index ab3747fc6..d1c69114c 100644 --- a/django-stubs/template/base.pyi +++ b/django-stubs/template/base.pyi @@ -67,7 +67,7 @@ class Template: self, context: Context | dict[str, Any] | None = ..., request: HttpRequest | None = ..., - ) -> Any: ... + ) -> SafeText: ... def compile_nodelist(self) -> NodeList: ... def get_exception_info( self, exception: Exception, token: Token From 195c6dce1ddaa088a9d7bb22ab782a8d8f43c798 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 08:56:31 -0400 Subject: [PATCH 2/9] template.backends.base: Add BaseTemplate protocol Based on DEP 182 --- django-stubs/template/backends/base.pyi | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/django-stubs/template/backends/base.pyi b/django-stubs/template/backends/base.pyi index 3f83f2972..3f9399213 100644 --- a/django-stubs/template/backends/base.pyi +++ b/django-stubs/template/backends/base.pyi @@ -1,7 +1,16 @@ from collections.abc import Iterator, Mapping -from typing import Any +from typing import Any, Protocol -from django.template.base import Template +from django.http.request import HttpRequest +from django.utils.safestring import SafeText + +# Source: https://github.com/django/deps/blob/main/final/0182-multiple-template-engines.rst#backends-api +class BaseTemplate(Protocol): + def render( + self, + context: Mapping[str, Any] | None = ..., + request: HttpRequest | None = ..., + ) -> SafeText | str: ... class BaseEngine: name: str = ... @@ -9,9 +18,9 @@ class BaseEngine: app_dirs: bool = ... def __init__(self, params: Mapping[str, Any]) -> None: ... @property - def app_dirname(self) -> str | None: ... - def from_string(self, template_code: str) -> Template: ... - def get_template(self, template_name: str) -> Template | None: ... + def app_dirname(self) -> str: ... + def from_string(self, template_code: str) -> BaseTemplate: ... + def get_template(self, template_name: str) -> BaseTemplate: ... @property def template_dirs(self) -> tuple[str]: ... def iter_template_filenames(self, template_name: str) -> Iterator[str]: ... From a44e78690a5c5f236e1d2c4b3a99c7893da03ffa Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:06:27 -0400 Subject: [PATCH 3/9] template.backends.django: Improve types --- django-stubs/template/backends/django.pyi | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/django-stubs/template/backends/django.pyi b/django-stubs/template/backends/django.pyi index 24bdd115d..bbce64c6e 100644 --- a/django-stubs/template/backends/django.pyi +++ b/django-stubs/template/backends/django.pyi @@ -1,9 +1,12 @@ -from collections.abc import Iterator -from typing import Any +from collections.abc import Iterator, Mapping +from typing import Any, NoReturn +from typing_extensions import override +from django.http.request import HttpRequest from django.template import base from django.template.engine import Engine from django.template.exceptions import TemplateDoesNotExist +from django.utils.safestring import SafeText from .base import BaseEngine @@ -13,18 +16,27 @@ class DjangoTemplates(BaseEngine): def get_templatetag_libraries( self, custom_libraries: dict[str, str] ) -> dict[str, str]: ... + @override + def from_string(self, template_code: str) -> Template: ... + @override + def get_template(self, template_name: str) -> Template: ... class Template: template: base.Template - backend: BaseEngine - def __init__(self, template: base.Template, backend: BaseEngine) -> None: ... + backend: DjangoTemplates + def __init__(self, template: base.Template, backend: DjangoTemplates) -> None: ... @property def origin(self) -> base.Origin: ... - def render(self, context: Any = ..., request: Any = ...) -> str: ... + def render( + self, + context: Mapping[str, Any] | None = ..., + request: HttpRequest | None = ..., + ) -> SafeText: ... def copy_exception( exc: TemplateDoesNotExist, backend: DjangoTemplates | None = ... ) -> TemplateDoesNotExist: ... -def reraise(exc: TemplateDoesNotExist, backend: DjangoTemplates) -> Any: ... +def reraise(exc: TemplateDoesNotExist, backend: DjangoTemplates) -> NoReturn: ... +def get_template_tag_modules() -> Iterator[tuple[str, str]]: ... def get_installed_libraries() -> dict[str, str]: ... def get_package_libraries(pkg: Any) -> Iterator[str]: ... From cc521bc43f793984ee7ad1d70214a6cf7c4d6140 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:07:14 -0400 Subject: [PATCH 4/9] template.backends.dummy: Improve types --- django-stubs/template/backends/dummy.pyi | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/django-stubs/template/backends/dummy.pyi b/django-stubs/template/backends/dummy.pyi index 573498eb0..e1d63e5e0 100644 --- a/django-stubs/template/backends/dummy.pyi +++ b/django-stubs/template/backends/dummy.pyi @@ -1,20 +1,21 @@ import string +from collections.abc import Mapping from typing import Any +from typing_extensions import override from django.http.request import HttpRequest from .base import BaseEngine class TemplateStrings(BaseEngine): - template_dirs: tuple[str] - def __init__( - self, params: dict[str, dict[Any, Any] | list[Any] | bool | str] - ) -> None: ... + @override + def from_string(self, template_code: str) -> Template: ... + @override + def get_template(self, template_name: str) -> Template: ... class Template(string.Template): - template: str def render( self, - context: dict[str, str] | None = ..., + context: Mapping[str, Any] | None = ..., request: HttpRequest | None = ..., ) -> str: ... From c84fcd511853a3684a5f1e5d465a04d0e86d1b86 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:07:40 -0400 Subject: [PATCH 5/9] template.backends.jinja2: Improve types --- django-stubs/template/backends/jinja2.pyi | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/django-stubs/template/backends/jinja2.pyi b/django-stubs/template/backends/jinja2.pyi index 709d4a92a..c51bb8bc7 100644 --- a/django-stubs/template/backends/jinja2.pyi +++ b/django-stubs/template/backends/jinja2.pyi @@ -1,16 +1,34 @@ -from collections.abc import Callable +from collections.abc import Callable, Mapping from typing import Any +from typing_extensions import override +from django.http.request import HttpRequest +from django.template import base from django.template.exceptions import TemplateSyntaxError +from django.utils.safestring import SafeText from .base import BaseEngine class Jinja2(BaseEngine): context_processors: list[str] = ... - def __init__(self, params: dict[str, Any]) -> None: ... + @override + def from_string(self, template_code: str) -> Template: ... + @override + def get_template(self, template_name: str) -> Template: ... @property def template_context_processors(self) -> list[Callable[..., Any]]: ... +class Template: + template: base.Template + backend: Jinja2 + origin: Origin + def __init__(self, template: base.Template, backend: Jinja2) -> None: ... + def render( + self, + context: Mapping[str, Any] | None = ..., + request: HttpRequest | None = ..., + ) -> SafeText: ... + class Origin: name: str = ... template_name: str | None = ... From 065e0d0395f742e2f48580ff7404c2a6c9c90a0f Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:07:55 -0400 Subject: [PATCH 6/9] template.backends.utils: Improve types --- django-stubs/template/backends/utils.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/django-stubs/template/backends/utils.pyi b/django-stubs/template/backends/utils.pyi index c71527866..2b54c2ba9 100644 --- a/django-stubs/template/backends/utils.pyi +++ b/django-stubs/template/backends/utils.pyi @@ -1,9 +1,9 @@ -from typing import Any +from collections.abc import Callable from django.http.request import HttpRequest from django.utils.safestring import SafeText def csrf_input(request: HttpRequest) -> SafeText: ... -csrf_input_lazy: Any -csrf_token_lazy: Any +csrf_input_lazy: Callable[[HttpRequest], SafeText] +csrf_token_lazy: Callable[[HttpRequest], SafeText] From 86cc77302611a8851c15ca89b9f741d1422ac931 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:08:27 -0400 Subject: [PATCH 7/9] template.loader: Make use of protocol class --- django-stubs/template/loader.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/django-stubs/template/loader.pyi b/django-stubs/template/loader.pyi index fc20d0413..a7ab32e97 100644 --- a/django-stubs/template/loader.pyi +++ b/django-stubs/template/loader.pyi @@ -1,20 +1,21 @@ from typing import Any from django.http.request import HttpRequest -from django.template.backends.django import Template +from django.template.backends.base import BaseTemplate from django.template.exceptions import ( # noqa: F401 TemplateDoesNotExist as TemplateDoesNotExist, ) +from django.utils.safestring import SafeText from . import engines as engines # noqa: F401 -def get_template(template_name: str, using: str | None = ...) -> Template: ... +def get_template(template_name: str, using: str | None = ...) -> BaseTemplate: ... def select_template( template_name_list: list[str] | str, using: str | None = ... -) -> Template: ... +) -> BaseTemplate: ... def render_to_string( template_name: list[str] | str, context: dict[str, Any] | None = ..., request: HttpRequest | None = ..., using: str | None = ..., -) -> str: ... +) -> SafeText: ... From 4be8753c5a25cc9e9b1dd43cff9712b1acd4d4bb Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:10:14 -0400 Subject: [PATCH 8/9] forms.renderers: Improve types Use protocol class Add missing classes --- django-stubs/forms/renderers.pyi | 39 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/django-stubs/forms/renderers.pyi b/django-stubs/forms/renderers.pyi index d86022aec..67ba3b3ad 100644 --- a/django-stubs/forms/renderers.pyi +++ b/django-stubs/forms/renderers.pyi @@ -1,27 +1,40 @@ +from collections.abc import Mapping from typing import Any +from typing_extensions import override -from django.template import Template -from django.template.backends.base import BaseEngine +from django.http.request import HttpRequest +from django.template.backends.base import BaseEngine, BaseTemplate +from django.template.backends.django import Template as DjangoTemplate +from django.template.backends.jinja2 import Template as Jinja2Template +from django.utils.safestring import SafeText -ROOT: Any - -def get_default_renderer() -> DjangoTemplates: ... +def get_default_renderer() -> BaseRenderer: ... class BaseRenderer: - def get_template(self, template_name: str) -> Any: ... + form_template_name: str + formset_template_name: str + def get_template(self, template_name: str) -> BaseTemplate: ... def render( - self, template_name: str, context: dict[str, Any], request: None = ... - ) -> str: ... + self, + template_name: str, + context: Mapping[str, Any], + request: HttpRequest | None = ..., + ) -> SafeText: ... class EngineMixin: - def get_template(self, template_name: str) -> Any: ... + backend: BaseEngine + def get_template(self, template_name: str) -> BaseTemplate: ... + @property def engine(self) -> BaseEngine: ... class DjangoTemplates(EngineMixin, BaseRenderer): - backend: Any = ... + @override + def get_template(self, template_name: str) -> DjangoTemplate: ... class Jinja2(EngineMixin, BaseRenderer): - backend: Any = ... + @override + def get_template(self, template_name: str) -> Jinja2Template: ... -class TemplatesSetting(BaseRenderer): - def get_template(self, template_name: str) -> Template: ... +class DjangoDivFormRenderer(DjangoTemplates): ... +class Jinja2DivFormRenderer(Jinja2): ... +class TemplatesSetting(BaseRenderer): ... From fd838600f4c1e84bbfe5cf1ee61b1bba3d375c50 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:24:12 -0400 Subject: [PATCH 9/9] Mark BaseTemplate as private --- django-stubs/forms/renderers.pyi | 6 +++--- django-stubs/template/backends/base.pyi | 6 +++--- django-stubs/template/loader.pyi | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/django-stubs/forms/renderers.pyi b/django-stubs/forms/renderers.pyi index 67ba3b3ad..32e6c096d 100644 --- a/django-stubs/forms/renderers.pyi +++ b/django-stubs/forms/renderers.pyi @@ -3,7 +3,7 @@ from typing import Any from typing_extensions import override from django.http.request import HttpRequest -from django.template.backends.base import BaseEngine, BaseTemplate +from django.template.backends.base import BaseEngine, _BaseTemplate from django.template.backends.django import Template as DjangoTemplate from django.template.backends.jinja2 import Template as Jinja2Template from django.utils.safestring import SafeText @@ -13,7 +13,7 @@ def get_default_renderer() -> BaseRenderer: ... class BaseRenderer: form_template_name: str formset_template_name: str - def get_template(self, template_name: str) -> BaseTemplate: ... + def get_template(self, template_name: str) -> _BaseTemplate: ... def render( self, template_name: str, @@ -23,7 +23,7 @@ class BaseRenderer: class EngineMixin: backend: BaseEngine - def get_template(self, template_name: str) -> BaseTemplate: ... + def get_template(self, template_name: str) -> _BaseTemplate: ... @property def engine(self) -> BaseEngine: ... diff --git a/django-stubs/template/backends/base.pyi b/django-stubs/template/backends/base.pyi index 3f9399213..95dd07b04 100644 --- a/django-stubs/template/backends/base.pyi +++ b/django-stubs/template/backends/base.pyi @@ -5,7 +5,7 @@ from django.http.request import HttpRequest from django.utils.safestring import SafeText # Source: https://github.com/django/deps/blob/main/final/0182-multiple-template-engines.rst#backends-api -class BaseTemplate(Protocol): +class _BaseTemplate(Protocol): def render( self, context: Mapping[str, Any] | None = ..., @@ -19,8 +19,8 @@ class BaseEngine: def __init__(self, params: Mapping[str, Any]) -> None: ... @property def app_dirname(self) -> str: ... - def from_string(self, template_code: str) -> BaseTemplate: ... - def get_template(self, template_name: str) -> BaseTemplate: ... + def from_string(self, template_code: str) -> _BaseTemplate: ... + def get_template(self, template_name: str) -> _BaseTemplate: ... @property def template_dirs(self) -> tuple[str]: ... def iter_template_filenames(self, template_name: str) -> Iterator[str]: ... diff --git a/django-stubs/template/loader.pyi b/django-stubs/template/loader.pyi index a7ab32e97..44b92268d 100644 --- a/django-stubs/template/loader.pyi +++ b/django-stubs/template/loader.pyi @@ -1,7 +1,7 @@ from typing import Any from django.http.request import HttpRequest -from django.template.backends.base import BaseTemplate +from django.template.backends.base import _BaseTemplate from django.template.exceptions import ( # noqa: F401 TemplateDoesNotExist as TemplateDoesNotExist, ) @@ -9,10 +9,10 @@ from django.utils.safestring import SafeText from . import engines as engines # noqa: F401 -def get_template(template_name: str, using: str | None = ...) -> BaseTemplate: ... +def get_template(template_name: str, using: str | None = ...) -> _BaseTemplate: ... def select_template( template_name_list: list[str] | str, using: str | None = ... -) -> BaseTemplate: ... +) -> _BaseTemplate: ... def render_to_string( template_name: list[str] | str, context: dict[str, Any] | None = ...,