DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.4
macnewbold opened this issue · comments
Recently we started getting this from flask_ldap3_login/init.py where stack
is used, where stack
is defined by:
try:
from flask import _app_ctx_stack as stack
except ImportError: # pragma: no cover
from flask import _request_ctx_stack as stack
DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.4. Use 'g' to store data, or 'app_ctx' to access the current context.
This is occurring with Flask 2.3.2 currently, but was originally deprecated in 2.2.0 on 2022-08-01.
My initial analysis of the four places in __init__.py
that use stack
is that the context is being used for storing ldap3_manager_connections
and ldap3_manager_main_connection
and that these should probably switch to using g
to store data instead. The use of g
may have implications on flask version support, but since I'm seeing it mentioned in Flask change logs clear back to 0.10 and earlier, over ten years ago, I think it's probably safe to use.
With Flask 3.0.0, you get an exception:
Traceback (most recent call last):
[...]
File "/.../lib/python3.10/site-packages/flask_ldap3_login/__init__.py", line 8, in <module>
from flask import _request_ctx_stack as stack
ImportError: cannot import name '_request_ctx_stack' from 'flask' (/.../lib/python3.10/site-packages/flask/__init__.py)
expect the release.
@jiamliang @rcw-2 which version of the library are you using? Have you tested 1.0.0aX version? I can release a new alpha of 1.0.0 if that works for you. The patch in the PR above cannot be cleanly applied to 0.9 so not possible to release it for 0.9 without some merge conflict resolution.
@jiamliang @rcw-2 which version of the library are you using? Have you tested 1.0.0aX version? I can release a new alpha of 1.0.0 if that works for you. The patch in the PR above cannot be cleanly applied to 0.9 so not possible to release it for 0.9 without some merge conflict resolution.
i have download flask_ldap3_login-1.0.0a2-py3-none-any.whl and install by running:
pip install flask_ldap3_login-1.0.0a2-py3-none-any.whl
it looks ok:
Installing collected packages: flask-ldap3-login
Successfully installed flask-ldap3-login-1.0.0a2
# pip show flask_ldap3_login
Name: flask-ldap3-login
Version: 1.0.0a2
Summary: LDAP Support for Flask
Home-page: https://github.com/nickw444/flask-ldap3-login
Author: Nick Whyte
Author-email: nick@nickwhyte.com
License: UNKNOWN
Location: /work/nlu-py/lib/python3.11/site-packages
Requires: Flask, Flask-wtf, ldap3, WTForms
Required-by:
(nlu-py) root@a51c30e62d11:/work#
when i start my scripts, it failed:
(nlu-py) root@a51c30e62d11:/work# python app.py
Traceback (most recent call last):
File "/work/nlu-py/lib/python3.11/site-packages/flask_ldap3_login/__init__.py", line 8, in <module>
from flask import _app_ctx_stack as stack
ImportError: cannot import name '_app_ctx_stack' from 'flask' (/work/nlu-py/lib/python3.11/site-packages/flask/__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/work/app.py", line 9, in <module>
from flask_ldap3_login import LDAP3LoginManager, AuthenticationResponseStatus
File "/work/nlu-py/lib/python3.11/site-packages/flask_ldap3_login/__init__.py", line 10, in <module>
from flask import _request_ctx_stack as stack
ImportError: cannot import name '_request_ctx_stack' from 'flask' (/work/nlu-py/lib/python3.11/site-packages/flask/__init__.py)
Currently, I am running version 0.9.18 of flask_ldap3_login. I've made some minor modifications to the flask_ldap3_login/__init__.py
file to ensure it runs well in my environment.
L5~L8:
from:
try:
from flask import _app_ctx_stack as stack
except ImportError: # pragma: no cover
from flask import _request_ctx_stack as stack
to:
from flask import current_app, has_app_context
L184-L185 and
L215-L216
from:
ctx = stack.top
if ctx is not None:
to:
if has_app_context():
ctx = current_app.app_context()
L206~L208:
from:
ctx = stack.top
if ctx is not None and connection in ctx.ldap3_manager_connections:
ctx.ldap3_manager_connections.remove(connection)
to:
if has_app_context():
ctx = current_app.app_context()
if hasattr(ctx, 'ldap3_manager_connections'):
if connection in ctx.ldap3_manager_connections:
ctx.ldap3_manager_connections.remove(connection)
L723~L724:
from:
ctx = stack.top
if ctx is None:
to:
if not has_app_context():
My modifications have made it work, but I actually don't understand the specific logic in the code. Therefore, I am hoping for an official release that would "reliably" fix this issue.
Thanks all.
@jiamliang @rcw-2 which version of the library are you using? Have you tested 1.0.0aX version? I can release a new alpha of 1.0.0 if that works for you. The patch in the PR above cannot be cleanly applied to 0.9 so not possible to release it for 0.9 without some merge conflict resolution.
I use 0.9.18. with Flask<3 and Werkzeug<3. 1.0.0a2 works too, with the same restrictions for Flask and Werkzeug.
I don't have a reason to stick with 0.9, so a new 1.0.0a version (and maybe a 1.0 release soon) would be fine for me.
root@a51c30e62d11:/work# pip freeze
blinker==1.7.0
certifi==2023.11.17
cffi==1.16.0
click==8.1.7
cryptography==41.0.7
DateTime==5.4
elastic-transport==8.11.0
elasticsearch==8.12.0
Flask==3.0.0
Flask-Cors==4.0.0
flask-ldap3-login==0.9.18
Flask-SQLAlchemy==3.1.1
Flask-WTF==1.2.1
greenlet==3.0.3
itsdangerous==2.1.2
Jinja2==3.1.2
ldap3==2.9.1
MarkupSafe==2.1.3
pyasn1==0.5.1
pycparser==2.21
pycryptodome==3.19.1
PyMySQL==1.1.0
pytz==2023.3.post1
SQLAlchemy==2.0.25
typing_extensions==4.9.0
urllib3==2.1.0
Werkzeug==3.0.1
WTForms==3.1.2
zope.interface==6.1
Is there an update on progress for this please? The flask_ldap3_login-1.0.0a2-py3-none-any.whl Doesn't work for me.
Thnx
Is there an update on progress for this please? The flask_ldap3_login-1.0.0a2-py3-none-any.whl Doesn't work for me. Thnx
That doesn't work because the change that is merged into the master branch on issue #115 aren't include on that release. This issue resolves if you build from master though:
git clone https://github.com/nickw444/flask-ldap3-login.git
cd flask-ldap3-login
pip install .
I haven't got much further than running the program, but it seems okay so far.
Great, thanks.
Do we have an expected date for an updated release please?
I don't think I can give an expected date - I don't get paid to work on this project, nor do I use it personally, so finding time and motivation to work on it is difficult. Currently the CI pipeline is broken (which also handles publishing of new versions) so it needs to be fixed first: #116
1.0.2a1 is now released: https://github.com/nickw444/flask-ldap3-login/releases/tag/1.0.2a1