Skip to content

fix: allow imports to be '_' prefixed#9308

Draft
dmadisetti wants to merge 2 commits into
mainfrom
dm/alternative-import
Draft

fix: allow imports to be '_' prefixed#9308
dmadisetti wants to merge 2 commits into
mainfrom
dm/alternative-import

Conversation

@dmadisetti

Copy link
Copy Markdown
Member

No description provided.

@vercel

vercel Bot commented Apr 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Apr 21, 2026 7:11pm

Request Review

akshayka pushed a commit that referenced this pull request Jul 23, 2026
## 📝 Summary

Replaces #9308 and #9309. Closes #9151, closes #10223.

A scoping change introduced in #8762 (0.22.0) made `_`-prefixed imports
private. The intent is consistent with marimo's private-variable
pattern, but three edge cases were mishandled:

1. References to **no-alias underscore imports** (`from pkg import
_camera`) were mangled inside nested scopes (function bodies,
decorators, class bases), producing `NameError: name '_cell_<id>_camera'
is not defined` (#9151). These imports are deliberately defined under
their **raw** name — the user has no control over the upstream symbol —
so their references must stay raw everywhere.
2. **Content hashing** failed for underscore-aliased module imports used
by cached functions in the same cell (`import marimo as _mo` +
`@_mo.lru_cache` → `cannot pickle 'module' object`).
3. One cell's raw underscore import could **leak into other cells**
through shared globals — but only when the importing cell happened to
run first: order-dependent behavior with no reactive edge (the failure
mode behind #10223's confusion).

## Intended behavior

| Form | Defines | Same cell (incl. nested scopes) | Other cells |
|---|---|---|---|
| `import foo` | `foo`, public | works | shared, reactive |
| `import foo as _f` / `import a.b as _f` | mangled `_f`, cell-local |
works | NameError + hint |
| `from a.b import _c` | raw `_c`, cell-local | works *(was NameError in
nested scopes, #9151)* | NameError + hint *(was an order-dependent
leak)* |
| `import _private.submodule` | raw `_private`, cell-local | works |
NameError + hint |
| `from ibis import _` | raw `_`, cell-local | works | NameError + hint
suggesting `from ibis import _ as lib` (#10223) |
| `_x = ...` | mangled `_x`, cell-local | works | NameError + hint *(was
a bare NameError exposing the mangled name)* |

Cell-local imports never trigger `MultipleDefinitionError`: two cells
may both `import numpy as _np` or `from pkg import _c`. Mangling imports
wholesale (#9309's approach) was ruled out because `import
_private.submodule` has no resolvable local name to mangle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant