Fix django.utils.translation.get_language() may return None #3858
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 280 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.337
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3858"
general
weight: 0.35
sobolevn
Description
In trans_real.py if you use
deactivate_all()the active language will be set to a version returningNoneonto_language(), this means after callingdeactivate_all()subsequent calls toget_language()will returnNone.deactivate_all()will also be used when you calloverride(None), which will also result inget_language()retuningNone.The current typing does only state that
get_language()will return astr, which is clearly not the case. You can reproduce it returningNoneby running for example:This change will fix the typing to also contain None as a valid return type for both the translation module itself and trans_real. trans_null will always return
setting.LANGUAGE_CODEwhich should be a string at all time.Impact
We had production code failing because
get_language()did in fact returnNoneand we passed this into some function expecting astr.mypyshould have told us our code was broken, but didn't as the typing lacks this "little" detail. Although it happens very rarely the typing information should contain all possible cases. Would have saved us from having some failed requests on some edge cases. 😅Related issues
This issue has already been fixed before, see https://github.com/typeddjango/django-stubs/pull/633. However the fix has been reverted in a glorious "Large update" changing thousands of lines, see https://github.com/typeddjango/django-stubs/pull/909 and https://github.com/typeddjango/django-stubs/commit/f69e0639c74fee0e8ad3d165790ac9804ea4a3f2#diff-f3594bc4899f118305433e25e8d86452ca9571cd9b68ed51323f1a5763831300
I expect reverting the original fix was done by mistake.
AI Policy