← Back to Pull requests

Make PyO3 introspection optional when stubs are disabled #4423

Closed
unknown closed this pull request without merging on Nov 15, 2026 00:25 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 14:40 UTC
Author

Custom rust_pyo3_toolchain definitions currently require pyo3_introspection even when their extensions use stubs = False. Removing the attribute's mandatory constraint alone is insufficient: py_pyo3_library unconditionally depends on the stub generator, which resolves the introspection library during analysis before the implementation checks whether stubs are enabled.

Make pyo3_introspection optional and have pyo3_extension supply the stub generator dependency only when stub generation is enabled. For stubs = None, a config_setting selects the dependency using the existing experimental_stubgen flag. Explicit True or False continues to override that flag. The flag's default remains unchanged.

The stub generator becomes an optional executable attribute on the internal rule, with visibility allowing the macro to supply it from caller packages. If stubs are requested without an introspection library, analysis reports a clear error explaining how to supply the library or disable stubs.

This allows consumers to reuse an existing PyO3 dependency without introducing an unused introspection dependency. Toolchains that provide introspection retain their existing stub-generation behavior.

Testing

Ran bazel test //test/... from extensions/pyo3 on Linux x86-64: all 33 tests pass.

Added 12 analysis regression cases covering all combinations of:

  • Explicit enabled, explicit disabled, or inherited stub generation.

  • The global stub-generation flag enabled or disabled.

  • A Rust PyO3 toolchain with or without introspection.
  • The tests check whether a PyO3StubGen action is created and verify the diagnostic when enabled stubs require missing introspection. Disabled cases successfully analyze without that dependency. Existing tests also verify generated stub contents, Python imports and calls, runfiles, compilation modes, and wheel packaging.

    Diff: 236 lines (+177 / -59) across 2 files Reviewer: UebelAndre Review duration: 2024m
    All Vouch review confidence checks passed
    Residual risk 0.15 = Change Risk (0.37) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.37 · 236 lines diff across 2 files
    0.37
    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.327
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4423"
    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: 37.2% (MEDIUM) · Higher = larger blast radius
    37.2%
    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: 14.7% · Safe to merge
    14.7%