Incompatibility with flask-babel
还没有人认领这个 Issue。
评估
调研方向
首先,在安装了 pytest-flask 的情况下运行提供的测试,并将其与未安装 pytest-flask 的环境进行比较。结合链接行中 flask-babel 的 get_locale 实现,阅读 pytest-flask 上下文 fixture 的行为。当连续的请求能够选择不同的语言,同时保持预期的 fixture 上下文行为时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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 flask-babel.
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.
import pytest
from flask import Flask
from flask import render_template_string
from flask import request
from flask import g
from flask_babel import Babel
def locale_selector():
if request.args.get("lang"):
g.lang = request.args["lang"]
return g.get("lang", "en")
@pytest.fixture
def app():
app = Flask(__name__)
Babel(app, locale_selector=locale_selector)
@app.route("/")
def index():
render_template_string("{% trans %}foobar{% endtrans %}")
return g.get("lang")
return app
def test_foobar(app):
client_app = app.test_client()
res = client_app.get("/?lang=fr")
assert res.text == "fr"
res = client_app.get("/?lang=en")
assert res.text == "en" # This assertion fails
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?
- 主要语言
- Python
- 星标
- 499
- 派生
- 93
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest-flask 的其他 Issue
-
bug
难度 3/5 1-2 天 新手友好度 35/100
pytest-dev/pytest-flask#190 · 1 条评论 · 1 个 reaction ·
-
bug
难度 2/5 1-3 小时 新手友好度 45/100
pytest-dev/pytest-flask#175 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
pytest-dev/pytest-flask#163 · 1 条评论 · 1 个 reaction ·
-
feature request
难度 4/5 3-5 天 新手友好度 45/100
pytest-dev/pytest-flask#153 · 1 条评论 ·
-
stale
难度 5/5 一周以上 新手友好度 20/100
pytest-dev/pytest-flask#103 ·
查看 pytest-dev/pytest-flask 的全部 Issue
相似的 Issue
-
namespace operations
难度 1/5 1 小时以内 新手友好度 82/100
EclipseFdn/open-vsx.org#13573 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
collective/icalendar#1854 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
rancher/rancher-ai-agent#412 ·
维护者通常 6 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
TUDelftGeodesy/DePSI#134 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
HenriquesLab/rxiv-maker#335 ·