Skip to content
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

Stop using deprecated _app_ctx_stack from Flask #58

Open
guneemwelloeux opened this issue Feb 28, 2023 · 9 comments
Open

Stop using deprecated _app_ctx_stack from Flask #58

guneemwelloeux opened this issue Feb 28, 2023 · 9 comments

Comments

@guneemwelloeux
Copy link

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

@bibek-p
Copy link

bibek-p commented Jun 6, 2023

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.

@yangdanny97
Copy link

The comment above reads like something ChatGPT would generate...

@guneemwelloeux
Copy link
Author

@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?

@SpacemanPaul
Copy link

SpacemanPaul commented Oct 20, 2023

Dies with an error in Flask 3.

This repo appears to be orphaned.

@saggit
Copy link

saggit commented Nov 29, 2023

For someone who using this lib and flask > 2.2.
Solution here: saggit@b234b49

ritwickdey added a commit to ritwickdey/flask-log-request-id that referenced this issue Mar 21, 2024
@ritwickdey
Copy link

ritwickdey commented Mar 21, 2024

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: ritwickdey@ed9e3e3

PR: #65

Edit: I released a new package with the fixes. More details here #65 (comment)

@manisalehi
Copy link

Hello everyone
I believe this issue arises when you install several packages that conflict with one another in my case my application was running all fine till when I installed email-validator I got this error
for me just deleting email_validtor and flask-sqlalchemy and then reinstalling flask-sqlalchmy resolved the problem
pip uninstall flask-sqlalchemy
pip uninstall email_valdiator
pip install flask-sqlalchemy

@jeevic
Copy link

jeevic commented Sep 14, 2024

Dies with an error in Flask 3.

This repo appears to be orphaned.

This is my fix code

def flask_ctx_get_request_id():
    """
    Get request id from flask's G object
    :return: The id or None if not found.
    """
    from flask import current_app  # We do not support < Flask 0.9
    if not has_app_context():
        raise ExecutedOutsideContext()

    g_object_attr = current_app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE']
    return g.get(g_object_attr, None)

@ritwickdey
Copy link

I released a new package with the fixes. More details here #65 (comment)

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

No branches or pull requests

8 participants