From 3d37d52a34c2989d0b5bb5cc825feb1a0b8d38ed Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 19 Jun 2023 09:03:25 +0200 Subject: [PATCH] Remove Jinja2Templates deprecation warning --- starlette/templating.py | 32 +++++++++++++------------------- tests/test_templates.py | 8 ++------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/starlette/templating.py b/starlette/templating.py index ec9ca193d..9b29186ae 100644 --- a/starlette/templating.py +++ b/starlette/templating.py @@ -1,5 +1,4 @@ import typing -import warnings from os import PathLike from starlette.background import BackgroundTask @@ -20,7 +19,11 @@ else: # pragma: nocover pass_context = jinja2.contextfunction # type: ignore[attr-defined] except ModuleNotFoundError: # pragma: nocover - jinja2 = None # type: ignore[assignment] + raise RuntimeError( + "Jinja2Templates requires the jinja2 package to be installed.\n" + "You can install this with:\n" + " $ pip install jinja2\n" + ) class _TemplateResponse(Response): @@ -75,7 +78,6 @@ def __init__( context_processors: typing.Optional[ typing.List[typing.Callable[[Request], typing.Dict[str, typing.Any]]] ] = None, - **env_options: typing.Any, ) -> None: ... @@ -93,34 +95,29 @@ def __init__( def __init__( self, directory: typing.Union[ - str, PathLike, typing.Sequence[typing.Union[str, PathLike]], None + str, PathLike[str], typing.Sequence[typing.Union[str, PathLike[str]]], None ] = None, *, context_processors: typing.Optional[ typing.List[typing.Callable[[Request], typing.Dict[str, typing.Any]]] ] = None, env: typing.Optional["jinja2.Environment"] = None, - **env_options: typing.Any, ) -> None: - if env_options: - warnings.warn( - "Extra environment options are deprecated. Use a preconfigured jinja2.Environment instead.", # noqa: E501 - DeprecationWarning, - ) - assert jinja2 is not None, "jinja2 must be installed to use Jinja2Templates" - assert directory or env, "either 'directory' or 'env' arguments must be passed" self.context_processors = context_processors or [] if directory is not None: - self.env = self._create_env(directory, **env_options) + self.env = self._create_env(directory) elif env is not None: self.env = env + else: + raise RuntimeError( + "Either 'directory' or 'env' must be passed to Jinja2Templates" + ) def _create_env( self, directory: typing.Union[ - str, PathLike, typing.Sequence[typing.Union[str, PathLike]] + str, PathLike[str], typing.Sequence[typing.Union[str, PathLike[str]]] ], - **env_options: typing.Any, ) -> "jinja2.Environment": @pass_context # TODO: Make `__name` a positional-only argument when we drop Python 3.7 @@ -130,10 +127,7 @@ def url_for(context: dict, __name: str, **path_params: typing.Any) -> URL: return request.url_for(__name, **path_params) loader = jinja2.FileSystemLoader(directory) - env_options.setdefault("loader", loader) - env_options.setdefault("autoescape", True) - - env = jinja2.Environment(**env_options) + env = jinja2.Environment(loader=loader, autoescape=True) env.globals["url_for"] = url_for return env diff --git a/tests/test_templates.py b/tests/test_templates.py index 1f1909f4b..7971284c9 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -129,7 +129,8 @@ async def page_b(request): def test_templates_require_directory_or_environment(): with pytest.raises( - AssertionError, match="either 'directory' or 'env' arguments must be passed" + RuntimeError, + match="Either 'directory' or 'env' must be passed to Jinja2Templates", ): Jinja2Templates() # type: ignore[call-overload] @@ -153,8 +154,3 @@ def test_templates_with_environment(tmpdir): templates = Jinja2Templates(env=env) template = templates.get_template("index.html") assert template.render({}) == "Hello" - - -def test_templates_with_environment_options_emit_warning(tmpdir): - with pytest.warns(DeprecationWarning): - Jinja2Templates(str(tmpdir), autoescape=True)