Incompatibility with flask-babel
まだ誰も着手していません。
評価
調査の方向性
まず、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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pytest-dev/pytest-flask のほかの issue
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
pytest-dev/pytest-flask#190 · コメント 1 件 · リアクション 1 件 ·
-
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 件 ·
-
feature request
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
pytest-dev/pytest-flask#153 · コメント 1 件 ·
-
stale
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
pytest-dev/pytest-flask#103 ·
pytest-dev/pytest-flask の issue をすべて見る
似ている issue
-
bug status/needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
prowler-cloud/prowler#12887 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area: desktop platform: macos priority: p3 status: ready type: enhancement
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
use-agent-os/agent-os#3484 ·
メンテナーはふだん 2 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
open-telemetry/opentelemetry-python-contrib#5113 · コメント 2 件 · リアクション 2 件 ·
メンテナーはふだん 1 日以内に返信
-
external
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
langchain-ai/docs#6255 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信