Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Incompatibility with flask-babel

オープン
#187 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
flask, python
領域
testing

調査の方向性

まず、pytest-flask をインストールした状態で提供されたテストを実行し、pytest-flask がない環境と比較します。リンク先の行にある flask-babel の get_locale の実装と併せて、pytest-flask のコンテキスト fixture の動作を読みます。連続するリクエストで異なる言語を選択でき、意図された fixture コンテキストの動作が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug

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.

https://github.com/python-babel/flask-babel/blob/a754eade39d9850693dd2b645ae8a2545df7fdf7/flask_babel/__init__.py#L257-L258

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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pytest-dev/pytest-flask のほかの issue

pytest-dev/pytest-flask の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。