← Back to Pull requests

Fix django.utils.translation.get_language() may return None #3858

Closed
unknown closed this pull request without merging on Nov 25, 2026 23:16 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 10:52 UTC
Author

Description

In trans_real.py if you use deactivate_all() the active language will be set to a version returning None on to_language(), this means after calling deactivate_all() subsequent calls to get_language() will return None.

deactivate_all() will also be used when you call override(None), which will also result in get_language() retuning None.

The current typing does only state that get_language() will return a str, which is clearly not the case. You can reproduce it returning None by running for example:

from django.utils import translation


with translation.override(None):
    print(translation.get_language())  # will be None

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_CODE which should be a string at all time.

Impact

We had production code failing because get_language() did in fact return None and we passed this into some function expecting a str. mypy should 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


  • I have read and agree to the AI Policy, removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result

  • No AI was harmed in creating this change. Only human intelligence was involved.

  • No AI was harmed in creating this PR's description. Only human intelligence was involved.
  • Diff: 280 lines (+210 / -70) across 3 files Reviewer: sobolevn Review duration: 744m
    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
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.6% (MEDIUM) · Higher = larger blast radius
    39.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.7% · Safe to merge
    15.7%