← Back to Pull requests

Fix type checking for open builtin #943

Closed
unknown closed this pull request without merging on Oct 28, 2026 15:04 UTC
View on GitHub
unknown unknown commented on Sep 26, 2026 22:06 UTC
Author

https://github.com/pydantic/monty/issues/941

open() runs against mounted directories, but it failed type checking with unresolved-reference. update.py filters upstream builtins.pyi through ALLOWED_FUNCTIONS, and open was not on the list.
Path.open() resolved to Unknown, because the stub tree had no io module.

Changes

  • open: added to ALLOWED_FUNCTIONS, so upstream typeshed's open and Path.open signatures come through unchanged.

  • io stubs: upstream io.pyi and _io.pyi are added to COPY_FILES. io and _io are listed in VERSIONS for type checking only, like abc.

  • Exceptions: FileNotFoundError, FileExistsError, IsADirectoryError, NotADirectoryError and PermissionError are added to ALLOWED_CLASSES, so except FileNotFoundError: around open() type-checks.

  • Tests: new sections are appended to the ends of good_types.py, reveal_types.py and bad_types.py, so no existing snapshot entry moves.

  • A pytest writes through a read-write mount with type checking on.
  • Docs: limitations/modules.md and docs/type-checking.md list io among the modules the type checker resolves but the runtime cannot import.
  • Trade-offs

    Upstream's signatures accept any argument value the runtime might accept, so code that forwards encoding or buffering from a variable type-checks.
    As a result, these pass type checking and then fail at runtime:

  • literal non-default buffering, errors, newline, closefd or opener, and encodings other than UTF-8

  • integer file descriptors, and 'x' or '+' modes

  • file members Monty lacks, such as iterating a file, writelines(), fileno() and readline(size)

  • import io
  • Follow-ups

  • A narrowed io stub, if iterating files turns out to be common.

  • These pass type checking and fail at runtime:

  • - print(..., file=f);
    - json.load(f) and json.dump(obj, f);
    - e.filename and e.errno on the OSError subclasses;
    - except IOError:;
    - from typing import TextIO.

    <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Fixes type checking for the open builtin by vendoring upstream typeshed's io stubs, so open() and Path.open() resolve with overloads for text and binary modes and invalid argument types are reported before execution.

  • Vendors io.pyi and _io.pyi from typeshed and adds open to the allowed builtins.

  • Adds the OSError subclasses open() can raise to the allowed class list, so except FileNotFoundError: type-checks.

  • Registers io and _io as stub-only modules for type checking.

  • Covers valid and invalid open/Path.open() calls, revealed file types, and a write-through run with type checking enabled.

  • Updates docs to list the additional stub-only modules that resolve during checking but raise ModuleNotFoundError at runtime.
  • <sup>Written for commit ab478c1aecf8249dbf7d1dd5fcf86d90754db47d. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/943?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    Diff: 445 lines (+333 / -112) across 5 files Reviewer: DanielTemesgen Review duration: 45658m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 445 lines diff across 5 files
    0.45
    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.365
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #943"
    general weight: 0.35
    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: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    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: 17.9% · Safe to merge
    17.9%
    Repository