-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecation Warning for **env_options in Jinja2Templates with Starlette >=0.28.0 #574
Comments
I think the issue lies in these lines: starlette-admin/starlette_admin/base.py Lines 192 to 199 in 5b183e0
The signature of Jinja2Templates looks like this: class Jinja2Templates:
@typing.overload
def __init__(
self,
directory: str | PathLike[str] | typing.Sequence[str | PathLike[str]],
*,
context_processors: list[typing.Callable[[Request], dict[str, typing.Any]]]
| None = None,
**env_options: typing.Any,
) -> None: ... So |
Something like this should do the job: from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
def _setup_templates(self) -> None:
env = Environment(
loader=ChoiceLoader(
[
FileSystemLoader(self.templates_dir),
PackageLoader("starlette_admin", "templates"),
]
),
extensions=["jinja2.ext.i18n"]
)
templates = Jinja2Templates(env=env) But raises another Warning: DeprecationWarning: The `name` is not the first parameter anymore. The first parameter should be the `Request` instance.
Replace `TemplateResponse(name, {"request": request})` by `TemplateResponse(request, name)`.
warnings.warn( After further examination I saw that the parameters of starlette-admin/starlette_admin/base.py Lines 489 to 499 in 5b183e0
and should rather look like this: async def _render_error(
self,
request: Request,
exc: Exception = HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR),
) -> Response:
assert isinstance(exc, HTTPException)
return self.templates.TemplateResponse(
request=request,
name="error.html",
context={"exc": exc},
status_code=exc.status_code,
) Also in all the other instantiations of TemplateResponse in base.py:
In auth.py:
And in views.py:
|
Hi @ptrstn, thank you for reporting this. Please feel free to submit a PR to fix it. |
Addresses jowilf#574 DeprecationWarning: The `name` is not the first parameter anymore. The first parameter should be the `Request` instance. Replace `TemplateResponse(name, {"request": request})` by `TemplateResponse(request, name)`. warnings.warn( Before Starlette 0.29.0, the name was the first parameter. Also, before that, in previous versions, the request object was passed as part of the key-value pairs in the context for Jinja2. See: - https://www.starlette.io/release-notes/#0290 - encode/starlette#2191 - https://github.com/encode/starlette/blob/c78c9aac17a4d68e0647252310044502f1b7da71/starlette/templating.py#L166-L178 - https://fastapi.tiangolo.com/reference/templating/#fastapi.templating.Jinja2Templates.TemplateResponse - https://fastapi.tiangolo.com/advanced/templates/#using-jinja2templates
Resolves jowilf#574 According to the Starlette 0.28.0 release notes, the **env_options parameter in Jinja2Templates has been deprecated in favor of the new env parameter. The relevant pull request #2159 explains this change. See: - encode/starlette#2159 - https://www.starlette.io/release-notes/#0280
I'm encountering a deprecation warning related to
Jinja2Templates
when runningpytest
tests withstarlette>=0.28.0
:This warning does not appear when installing
starlette==0.27.0
According to the Starlette 0.28.0 release notes, the
**env_options
parameter inJinja2Templates
has been deprecated in favor of the newenv
parameter. The relevant pull request #2159 explains this change.It seems likely that Starlette Admin (or other dependencies) are still using the deprecated
**env_options
, causing the warning when running tests with Starlette 0.28.0.Steps to Reproduce:
Project Structure:
Source Code:
app.py
:test_app.py
:Requirements Files:
requirements-deprecation-warning.txt
:requirements-no-warnings.txt
:Reproduce the Warning:
Result: Deprecation Warning
Result: All tests pass without warnings.
The text was updated successfully, but these errors were encountered: