← Back to Pull requests

Fix bogus name-defined error for cross-module as_manager overrides #3597

Merged
BenA-SA merged 2 files (43 lines) on Aug 16, 2026 22:19 UTC
View on GitHub
BenA-SA BenA-SA commented on Aug 14, 2026 19:58 UTC
Author

Since #3460, add_as_manager_to_queryset_class resolves an inherited as_manager override's return annotation with semanal_api.anal_type().

That resolves names against the current module. When the override lives in another module that mypy has not analyzed yet (consumer scheduled before the library inside the same SCC), the annotation is still an UnboundType, and any name private to the defining module - typically a TypeVar - fails to resolve, producing a spurious 'Name "..." is not defined' error reported against the consumer's file with the library's line number.

Suppress resolution errors from that call and bail out without generating a manager, restoring the pre-6.0.7 behaviour for the cross-module case while keeping #3460's fix for same-module overrides.

Related issues


Fixes #3596

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
  • Diff: 43 lines (+42 / -1) across 2 files Reviewer: UnknownPlatypus Review duration: 3020m
    All Vouch review confidence checks passed
    Residual risk 0.02 = Change Risk (0.27) × (1 − Review Confidence (0.93)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.27 · 43 lines diff across 2 files
    0.27
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.96 · 2 substantive comment(s)
    0.96
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    93%
    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.226
    Real PR Conversation Timeline
    3 items
    UnknownPlatypus UnknownPlatypus commented on code on Aug 14, 2026 20:40 UTC
    Code Review
    mypy_django_plugin/transformers/managers.py:1
    @@ -419,7 +419,12 @@ def _defer() -> None:
                 return
             # A user override's return annotation may still be unanalyzed here, so
             # resolve it explicitly (a no-op if already analyzed); defer if incomplete.
    -        base_as_manager_ret_type = semanal_api.anal_type(base_as_manager_type.ret_type)

    Isn't there already a flag in anal_type to suppress such error ?

    BenA-SA BenA-SA commented on code on Aug 15, 2026 06:13 UTC
    Code Review
    mypy_django_plugin/transformers/managers.py:1
    @@ -419,7 +419,12 @@ def _defer() -> None:
                 return
             # A user override's return annotation may still be unanalyzed here, so
             # resolve it explicitly (a no-op if already analyzed); defer if incomplete.
    -        base_as_manager_ret_type = semanal_api.anal_type(base_as_manager_type.ret_type)

    I tested this with the two error-related flags the plugin-facing anal_type exposes (allow_unbound_tvars and report_invalid_types), each on its own and both together. My suggested test case fails identically in every combination.

    I think that's because those flags never reach this error. It's a name lookup failure rather than an invalid type: TypeAnalyser.lookup_qualified calls api.lookup_qualified with suppress_errors=False hardcoded, so the Name "..." is not defined error is emitted by SemanticAnalyzer.name_not_defined before either flag is consulted. As far as I can tell, [report_invalid_types only gates the "Invalid type: try using Literal[...]" family of messages](https://github.com/python/mypy/blob/v2.3.0/mypy/typeanal.py#L1433-L1440), and allow_unbound_tvars applies to TypeVars that resolve but are not bound in scope.

    I think filter_errors() is the closest tool for the job, but happy to switch if there's a better mechanism I've missed.

    Assignees
    BenA-SA BenA-SA
    Labels
    risk: low confidence: 93.4% 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: 27.1% (LOW) · Higher = larger blast radius
    27.1%
    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: 93.4% · Lower = rushed or shallow review
    93.4%
    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: 2% · Safe to merge
    2%