Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
from_string()
and get_template()
return backend-specific tem…
…plate (#2400) * Annotate `context` argument of `make_context()` The implementation of `make_context()` explicitly rejects everything other than `dict` or `None` by raising `TypeError`. * Drop `Context` from allowed types of template render method [DEP 182](https://github.com/django/deps/blob/main/final/0182-multiple-template-engines.rst#backends-api) states: "If `context` is provided, it must be a `dict`.". While the `Jinja2` and `TemplateStrings` backends support arbitrary mappings, `DjangoTemplates` calls `make_context()`, which only supports `dict` (and `None`). * Do not require template render method to return a `SafeString` Only the Django Template Language backend outputs `SafeString`. * Provide arguments types for Jinja2 template render method All of the argument types are Django types, so there is no need to avoid annotating them. Only the Jinja2 native template type is unknown to us. * Allow non-string values in dummy template render context The implementation of the `render()` method uses the standard library method `string.Template.safe_substitute()` to do the actual formatting, which accepts other value types than `str` and simply calls `__str__()` on them. The `safe_substitute()` method is annotated in typeshed with `object` as the value type for the mapping. However, as `_EngineTemplate` uses `Any` instead, I went with that. * Make it explicit that the dummy template class adheres to the protocol * Make `from_string()` and `get_template()` return backend-specific template * Revert "Make it explicit that the dummy template class adheres to the protocol" This reverts commit 509e368. Inheriting from both `string.Template` and `typing.Protocol` causes mypy to report a metaclass conflict on Python 3.8.
- Loading branch information