You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found an incompatibility when using at the same time flask-babel 4.0.0 and pytest-flask 1.3.0.
I am not sure whose responsability it is, maybe both, maybe neither, so I post this issue on both bugtrackers.
Here is the link to the issue at pytest-flask.
In the following snippet, a dummy view translates a dummy string and returns the current lang code.
The lang code is dynamically set by a request argument lang.
In this test environment the first visit is successful and sets the lang to fr but the second visit fails and the lang is not set to uk but still is fr.
Note that this test fails if pytest-flask is installed in the environment, but passes if pytest-flask is not installed. In production, the behavior is OK too, so there is going on with pytest-flask.
With a little debugging, I can see that the locale_selector method is only called once.
That explains why the language stays to fr.
Looking closer, it seems that the get_locale method from flask-babel saves the loaded lang in the current context and reuses it on following calls.
On different requests the language would indeed be recomputed.
However, as far as I understand, pytest-flask loads a context for the unit tests (so session and g are accessible without loading a new context). That probably makes flask-babel not recomputing the locale, because the same context is used, and leads to my test failing.
Please correct me if my analysis is wrong. I don't know if there is a mis-usage from my side or if the two libraries do not belong together. If they don't, I hope we can find a solution by bringing every one in the same room :)
What do you think?
The text was updated successfully, but these errors were encountered:
This seems like an issue with pytest-flask. We could probably add a teardown handler to make this explicit, but g persisting between requests is a break with normal flask behaviour and will cause issues with other plugins, not just flask-babel.
I found an incompatibility when using at the same time flask-babel 4.0.0 and pytest-flask 1.3.0.
I am not sure whose responsability it is, maybe both, maybe neither, so I post this issue on both bugtrackers.
Here is the link to the issue at pytest-flask.
In the following snippet, a dummy view translates a dummy string and returns the current lang code.
The lang code is dynamically set by a request argument
lang
.In this test environment the first visit is successful and sets the lang to
fr
but the second visit fails and the lang is not set touk
but still isfr
.Note that this test fails if
pytest-flask
is installed in the environment, but passes ifpytest-flask
is not installed. In production, the behavior is OK too, so there is going on withpytest-flask
.With a little debugging, I can see that the
locale_selector
method is only called once.That explains why the language stays to
fr
.Looking closer, it seems that the
get_locale
method from flask-babel saves the loaded lang in the current context and reuses it on following calls.On different requests the language would indeed be recomputed.
flask-babel/flask_babel/__init__.py
Lines 257 to 258 in a754ead
However, as far as I understand,
pytest-flask
loads a context for the unit tests (sosession
andg
are accessible without loading a new context). That probably makesflask-babel
not recomputing the locale, because the same context is used, and leads to my test failing.Please correct me if my analysis is wrong. I don't know if there is a mis-usage from my side or if the two libraries do not belong together. If they don't, I hope we can find a solution by bringing every one in the same room :)
What do you think?
The text was updated successfully, but these errors were encountered: