Skip to content

fix: consistent mangling for private imports#9309

Closed
dmadisetti wants to merge 8 commits intomainfrom
dm/import-consistency
Closed

fix: consistent mangling for private imports#9309
dmadisetti wants to merge 8 commits intomainfrom
dm/import-consistency

Conversation

@dmadisetti
Copy link
Copy Markdown
Collaborator

📝 Summary

A scoping change introduced in #8762 changed _ prefixed imports by turning then into privates.
While this is consistent with the remainder of marimo's private pattern, it is technically a breaking change as of 0.22.0, and the edge case was not correctly handled. The change lead to a bug where imports referenced in scope were not correctly addressed. This PR handles the mangling behavior for these import cases.

An alternative PR, for retaining the behavior, is here: #9308 but this requires ref tracking a little differently such that the _ pattern is properly placed into globals.

closes #9151

@dmadisetti dmadisetti requested review from akshayka and Copilot April 21, 2026 19:13
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 21, 2026

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

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Apr 22, 2026 0:00am

Request Review

@dmadisetti dmadisetti changed the title fix: consistent mangling for imports fix: consistent mangling for private imports Apr 21, 2026
Copy link
Copy Markdown
Contributor

@mscolnick mscolnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also test renaming the private import?

Comment thread marimo/_ast/visitor.py
# Previously we did not mangle. So this is technically breaking
# from 0.22.0 (#8762) but now consistent with private naming
# conventions.
mangled = self._if_local_then_mangle(basename)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this require actual code changes? I thought it was just to add a verification test?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation is broken, The test I put in caught the edge case where a scoped import no longer works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, maybe worth thinking through some other edge cases then (lazy imports etc)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression introduced by private-name mangling changes (#8762) where underscore-prefixed imports referenced within scope (e.g., inside nested functions) could be mis-addressed, leading to runtime NameErrors. The PR updates the AST visitor’s import-alias handling and adds a runtime regression test to validate the intended mangling behavior.

Changes:

  • Update _get_alias_name() to mangle underscore-prefixed imports even when no explicit as alias is provided (by rewriting the AST alias name).
  • Add a runtime test covering underscore-prefixed from ... import ... usage and ensuring such imports remain private and don’t leak into globals.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/_runtime/test_runtime.py Adds a regression test verifying behavior for underscore-prefixed imports across cells/scopes.
marimo/_ast/visitor.py Changes AST import alias resolution to apply private-name mangling for non-aliased imports.

Comment thread marimo/_ast/visitor.py
Copy link
Copy Markdown
Contributor

@akshayka akshayka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the PR description more specific?

and the edge case was not correctly handled.

Which edge case?

The change lead to a bug where imports referenced in scope were not correctly addressed. This PR handles the mangling behavior for these import cases.

What do you mean by not correctly addressed? How does this PR fix it?

Comment thread marimo/_ast/visitor.py
Comment on lines +302 to +303
if mangled != basename:
node.asname = mangled
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this branch. Isn't node.asname None? Why are we assigning the mangled basename to it? Should it be name.name = mangled?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't rename a module but:

import _private as _mangled_private is fine

I do think I'm missing the

import _private.submodule

case though. Will revise and add a comment.

@akshayka
Copy link
Copy Markdown
Contributor

Can you make the PR description more specific?

and the edge case was not correctly handled.

Which edge case?

The change lead to a bug where imports referenced in scope were not correctly addressed. This PR handles the mangling behavior for these import cases.

What do you mean by not correctly addressed? How does this PR fix it?

@dylan can you address these comments? This PR is subtle enough that I would like the intended behavior to be very clear.

@dmadisetti
Copy link
Copy Markdown
Collaborator Author

Closing out for simpler method

@dmadisetti dmadisetti closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

import of internal modules is not working any more >0.21.1

4 participants