← Back to Pull requests

Support `from m import *` #1314

Closed
unknown closed this pull request without merging on Nov 15, 2026 13:41 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 16:06 UTC
Author

from m import used to be rejected at parse time. It now binds m.__all__, or every name of m not starting with _, for bundled, host and file modules, at module level, in REPL feeds and in exec() snippets.
In a function or class body it raises CPython's SyntaxError: import
only allowed at module level.

How it works

The names are only known at runtime, so the compiler emits a loop driven by one new opcode, ImportStar, with a u8 mode:

LoadModule m (+ LoadSubmodule ...)   [m]
ImportStar NAMES; GetIter            [m, iter]
loop: ForIter end                    [m, iter, name]
ImportStar LOAD                      [m, iter, name, value]  (may suspend)
ImportStar BIND; Jump loop           [m, iter]
end: Pop                             []

  • LOAD is separate from BIND so a property such as os.environ can suspend for the host and resume.

  • BIND stores into the frame's locals dict, its globals dict, or a module global slot.

  • A slot-global binding interns the name at runtime and appends a slot, as exec() already does, so later feeds, exec() snippets and functions see it.
  • A module containing a star import no longer substitutes builtins at module scope, since the import may rebind them (from math import makes pow return a float).

  • __all__ is read as CPython reads it, by index from 0 until IndexError, so any sequence works (range, deque, Counter, a class with __getitem__).

  • Mappings and other values raise PySequence_GetItem's TypeError; that check is now shared with bisect.
  • Errors name the module by the __name__ in its namespace.

  • This also fixes the general module 'x' has no attribute 'y' error for a file module that rebinds __name__.
  • Bundled modules whose CPython counterpart defines __all__ now have one: their public names, less base64's MAXBINSIZE and MAXLINESIZE.

  • To set it in one place, each module's create_module now returns its Module and StandardLib::create allocates it.

    No heap/ changes and no unsafe.
    The opcode fingerprint in dump_format.rs is updated without a DUMP_VERSION bump, following #1173.
    Dumps from before this change load as before: their modules simply have no __all__.

    Other changes

  • ParseError::NotSupported is removed; the wildcard rejection was its only user.

  • repl_rejected_snippet_locations used from math import as its rejected snippet; it now uses type Alias = int.

  • The traceback test harness now honours mount-fs, so file-module error cases can run on CPython.
  • Remaining differences

    Documented under "Star imports" in limitations/modules.md:

  • A bundled module's __all__ lists only what Monty implements, in Monty's order, and is always a list.

  • __all__ is read in full before the first name is bound; this shows only through a __getitem__ with side effects, or when an earlier binding fails.

  • Binding past 65536 module globals raises SyntaxError.
  • Tests

  • import__star.py (replaces import__star_error.py), import__star_host.py, import__star_bundled.py, and three traceback cases.

  • repl_star_import_binds_names_for_later_feeds checks names bound in one feed are visible in the next, across a dump round trip.

  • On the final commit: make test-cases (1518 passed), the import__ cases under memory-model-checks and ref-count-return, make lint-rs, make lint-py, mdformat.

  • cargo nextest run -p monty -p monty-type-checking -p monty-types passed (1236) before the bundled __all__ change; it was not re-run after it.
  • Follow-ups

  • Once this merges, the vendored stat can drop 0002-stat-no-accelerator.patch.

  • The candidate scan shows bisect and stat now load from CPython's source; posixpath, pathlib and types move on to other blockers.
  • Bundled modules have no __name__.

  • An exception passing through a non-matching except reports the except body's line instead of the raising line.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    from m import used to be rejected at parse time; it now binds m.__all__, or every name of m not starting with _, for bundled, host, and file modules, at module level, in REPL feeds, and in exec() snippets. In a function or class body it raises CPython's SyntaxError: import only allowed at module level.

    Notable changes

  • A module with a star import no longer substitutes builtins at module scope, since the import may rebind them (pow after from math import *).

  • Bundled modules whose CPython counterpart defines __all__ now have one, listing only the names Monty implements.

  • Attribute errors on modules now use the module's __name__, which also fixes the message for file modules that rebind it.

  • Names bound by a star import are interned at runtime, so later REPL feeds and exec() snippets see them.

  • Reading __all__ is subject to the feed's time limit and growth checks, so an endless __all__ times out.

  • The dump version is now 16; older dumps load as before, their modules simply having no __all__.

  • Remaining differences from CPython are documented under "Star imports" in docs/limitations/modules.md.
  • <sup>Written for commit 30f8448a167894b07e1dfc84e928e8adaf0fdb7f. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1314?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: 650 lines (+487 / -163) across 8 files Reviewer: davidhewitt Review duration: 4175m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1314"
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%
    Repository