Skip to content

Fix Deprecation Warnings for TemplateResponse and Jinja2Templates#575

Merged
jowilf merged 4 commits intojowilf:mainfrom
ptrstn:main
Aug 31, 2024
Merged

Fix Deprecation Warnings for TemplateResponse and Jinja2Templates#575
jowilf merged 4 commits intojowilf:mainfrom
ptrstn:main

Conversation

@ptrstn
Copy link
Contributor

@ptrstn ptrstn commented Aug 31, 2024

Resolves #574

These warnings should no longer appear:

DeprecationWarning: Extra environment options are deprecated. Use a preconfigured jinja2.Environment instead.
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)`.

See:

ptrstn and others added 4 commits August 31, 2024 22:31
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
- Kludex/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:
- Kludex/starlette#2159
- https://www.starlette.io/release-notes/#0280
@codecov
Copy link

codecov bot commented Aug 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (2540c51) to head (80e2e4a).
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #575   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        76    +1     
  Lines         5817      5897   +80     
=========================================
+ Hits          5817      5897   +80     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ptrstn

@jowilf jowilf merged commit 9e50bc9 into jowilf:main Aug 31, 2024
@jowilf jowilf added this to the Release 0.15.0 milestone Feb 2, 2025
@jowilf jowilf mentioned this pull request May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecation Warning for **env_options in Jinja2Templates with Starlette >=0.28.0

2 participants