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

Fix KeyError exception when flask-babel is installed but not used #2485

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cjmayo
Copy link
Contributor

@cjmayo cjmayo commented Jul 28, 2024

flask-babel's gettext() expects a Babel object to have been created.

Otherwise an exception is raised.

  File "/usr/lib/python3.12/site-packages/flask_babel/__init__.py", line 46, in get_babel
    return app.extensions['babel']
           ~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'babel'

Not completely convinced by this, but at least it illustrates the issue.

flask-babel's gettext() expects a Babel object to have been created.

Otherwise an exception is raised.

  File "/usr/lib/python3.12/site-packages/flask_babel/__init__.py", line 46, in get_babel
    return app.extensions['babel']
           ~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'babel'
@cjmayo cjmayo marked this pull request as draft July 28, 2024 17:45
@cjmayo
Copy link
Contributor Author

cjmayo commented Jul 28, 2024

Indeed, it breaks applications where flask-babel is used.

@samuelhwilliams
Copy link
Contributor

Yeah I've been chewing over this one for a little while in the back of my head. It's a bit gnarly because it mutates the entire flask_admin.babel module based on what's in place at the time of the first import.

I think it would be better to refactor this so that the state of the flask app, flask_admin instance, etc aren't relevant at module import time. But haven't yet dug into this enough to work out what that work look like and/or how far the repercussions would spread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants