Calling `Jinja2Template()` with both `directory` and `env` shouldn't be allowed. When both parameters were used, the passed `env` was silently ignored in favor of creating a new one with the provided `directory` and the deprecated `env_options`.
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"
+ assert bool(directory) ^ bool(
+ 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)
Jinja2Templates() # type: ignore[call-overload]
+def test_templates_require_directory_or_enviroment_not_both() -> None:
+ with pytest.raises(
+ AssertionError, match="either 'directory' or 'env' arguments must be passed"
+ ):
+ Jinja2Templates(directory="dir", env=jinja2.Environment())
+
+
def test_templates_with_directory(tmpdir: Path) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file: