flask-log-request-id
flask-log-request-id copied to clipboard
Stop using deprecated `_app_ctx_stack` from Flask
Upon using the latest version of this package (0.10.2) in conjunction with the latest version of Flask (2.2), I'm getting the following warnings:
./usr/local/lib/python3.7/site-packages/flask_log_request_id/request_id.py:18: DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3.
from flask import _app_ctx_stack as stack # We do not support < Flask 0.9
/usr/local/lib/python3.7/site-packages/flask_log_request_id/request_id.py:20: DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3. Use 'g' to store data, or 'app_ctx' to access the current context.
if stack.top is None:
Could you please have a look at removing the use of this now deprecated construct?
Thanks
To remove the usage of _app_ctx_stack and address the deprecation warning, you'll need to modify the request_id.py file in the flask_log_request_id package. Here's how you can update the code:
Locate the request_id.py file in the flask_log_request_id package, which is typically located in the flask_log_request_id or similar folder inside your site-packages or virtual environment.
Open the request_id.py file in a text editor or IDE.
from flask import _app_ctx_stack as stack
Replace it with the following import statement:
from flask import g
Next, find the line of code where _app_ctx_stack is being used. It should look like this:
if stack.top is None:
Replace it with the following line of code using g:
if g.get("app_ctx") is None:
Save the modified request_id.py file.
After making these changes, you should no longer see the deprecation warnings related to _app_ctx_stack when using the updated package with Flask 2.2 or later.
Please note that modifying third-party packages is generally not recommended, as it may introduce compatibility issues or make it difficult to update the package in the future. It's better to reach out to the maintainer of the flask_log_request_id package and ask them to update it to use the recommended approach with g or app_ctx instead of _app_ctx_stack.
The comment above reads like something ChatGPT would generate...
@yangdanny97 That it exactly what it looks like. The issue still remains. Is there any chance this is going to be fixed in this repo one day?
Dies with an error in Flask 3.
This repo appears to be orphaned.
For someone who using this lib and flask > 2.2. Solution here: https://github.com/saggit/flask-log-request-id/commit/b234b490d60a1b8e555bfc7b97eac0720a86c947
Thank you @saggit for sharing the changes. However, there were some bugs .
- Calling
current_request_id()
outside the flask context throws errors. (It was not previously) -
current_request_id()
is throwing errors in the app context. (with server.app_context()
)
I fixed those bugs in my repo. It supports older Flask versions too. In case anyone needs this library for Flaskv2 or v3. changes: https://github.com/ritwickdey/flask-log-request-id/commit/ed9e3e3b4e616016d5ea9acde8ef38060cdb836c
PR: https://github.com/Workable/flask-log-request-id/pull/65