Incompatibility with flask-babel
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia eseguendo il test fornito con pytest-flask installato e confrontalo con l’ambiente senza pytest-flask. Leggi il comportamento della fixture di contesto di pytest-flask insieme all’implementazione di get_locale di flask-babel nelle righe collegate. Il lavoro è completato quando richieste successive possono selezionare lingue diverse preservando il comportamento previsto del contesto della fixture.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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?
- Lingua principale
- Python
- Stelle
- 499
- Fork
- 93
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pytest-dev/pytest-flask
-
bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
pytest-dev/pytest-flask#190 · 1 commento · 1 reazione ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
pytest-dev/pytest-flask#175 · 3 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
pytest-dev/pytest-flask#163 · 1 commento · 1 reazione ·
-
feature request
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
pytest-dev/pytest-flask#153 · 1 commento ·
-
stale
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
pytest-dev/pytest-flask#103 ·
Tutte le issue di pytest-dev/pytest-flask
Issue simili
-
[Bug] @deck.gl/arcgis dist import resolves to unpublished @deck.gl/core source path (9.3.11, 9.4.0)Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
workflow: a tick's dispatch counts as 'only this step', and no review self-grants a round unattendedApertaworkflow
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
kristofdegrave/homeassistant-smart-charging#1505 ·
I maintainer di solito rispondono entro 1 giorno
-
New Submission: TropWATERApertametadata submission
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
canonical/content-cache-operator#163 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[submission]Apertasubmission
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
leanprover/lean-eval-submissions#1852 ·
I maintainer di solito rispondono entro 1 giorno