Skip to content

fix: pretty format hidden variable behavior in stack traces#9660

Merged
dmadisetti merged 7 commits into
mainfrom
dm/pretty-locals
May 26, 2026
Merged

fix: pretty format hidden variable behavior in stack traces#9660
dmadisetti merged 7 commits into
mainfrom
dm/pretty-locals

Conversation

@dmadisetti

Copy link
Copy Markdown
Member

📝 Summary

This PR renames "hidden" variables like _a (saved in globals as _cell_<id>_<variable>) in stack traces to the expected variable name.

image

closes #9623

Copilot AI review requested due to automatic review settings May 21, 2026 23:54
@vercel

vercel Bot commented May 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 May 26, 2026 6:27pm

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

1 issue found across 8 files

Architecture diagram
sequenceDiagram
    participant User
    participant UI as Frontend UI
    participant Traceback as Traceback/Traceback Renderer
    participant MarimoError as MarimoErrorOutput
    participant MangledChip as MangledLocalChip
    participant LocalVars as local-variables Utils
    participant Runtime as Python Runtime
    participant Variables as _ast/variables
    participant Pytest as _runtime/pytest

    Note over User,Pytest: Error display flow for mangled cell-local variables

    User->>UI: Interacts with notebook cell
    UI->>Runtime: Execute cell code
    Runtime->>Variables: Compile cell (rewrites `_a` → `_cell_Hbol_a`)
    Runtime-->>UI: Returns error with mangled name (e.g., `name '_cell_Hbol_a' is not defined`)

    alt Standard output/message
        UI->>MarimoError: Render NameError message
        MarimoError->>LocalVars: splitMangledLocals(msg)
        LocalVars-->>MarimoError: Segments [text, {cellId, name}, text]
        MarimoError->>MangledChip: renderMangledSegments(segments)
        MangledChip->>MangledChip: Render variable name + tooltip
        MangledChip-->>MarimoError: React elements
        MarimoError-->>UI: Display demangled variable with link
    end

    alt Traceback display
        UI->>Traceback: Render full traceback
        Traceback->>LocalVars: replaceMangledLocal(domNode)
        LocalVars->>LocalVars: containsMangledLocal(text)
        alt Has mangled name
            LocalVars->>LocalVars: splitMangledLocals(text)
            LocalVars-->>Traceback: Segments
            Traceback->>MangledChip: renderMangledSegments(segments)
            MangledChip-->>Traceback: React elements
        else No mangled name
            LocalVars-->>Traceback: Return unchanged
        end
        Traceback-->>UI: Display demangled traceback
    end

    alt Pytest output
        Runtime->>Pytest: Run tests, capture report
        Pytest->>Variables: demangle_locals_in_text(lines)
        Variables-->>Pytest: Demangled exception/source lines
        Pytest->>Variables: demangle_locals_in_text(reprcrash.message)
        Variables-->>Pytest: Demangled summary message
        Pytest-->>Runtime: Rewritten longrepr
        Runtime-->>UI: Display demangled test output
    end

    Note over User,UI: User sees friendly variable name (e.g., `_a`) with tooltip showing defining cell
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread frontend/src/utils/local-variables.ts Outdated

Copilot AI left a comment

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.

Pull request overview

This PR improves the readability of stack traces and error output by converting compiler-mangled “cell-local” variable names (e.g. _cell_<id>_a) back into the user-facing underscore-prefixed names (e.g. _a) in both backend pytest output and the frontend error/traceback UI.

Changes:

  • Added a Python demangling helper (demangle_locals_in_text) and applied it to pytest longrepr formatting.
  • Added a frontend utility to detect/split mangled locals and render them as user-friendly chips linking to the defining cell.
  • Added unit tests on both Python and frontend sides to cover basic demangling/splitting behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/_ast/test_variables.py Adds Python unit tests for demangling mangled locals in free-form text.
marimo/_runtime/pytest.py Rewrites pytest longrepr traceback output to use marimo URIs and demangle locals.
marimo/_ast/variables.py Introduces demangle_locals_in_text() via a compiled regex.
frontend/src/utils/local-variables.ts Adds frontend parsing helpers to detect/split mangled locals for display.
frontend/src/utils/tests/local-variables.test.ts Adds vitest coverage for frontend mangled-local parsing/splitting.
frontend/src/components/editor/output/MarimoTracebackOutput.tsx Demangles mangled locals inside traceback HTML and the header message.
frontend/src/components/editor/output/MarimoErrorOutput.tsx Demangles NameError messages for underscore-prefixed locals in the error UI.
frontend/src/components/editor/errors/mangled-local-chip.tsx Adds UI rendering for mangled locals as chips with tooltips and cell links.
Comment thread marimo/_ast/variables.py
Comment thread tests/_ast/test_demangle_locals.py
Comment thread frontend/src/utils/local-variables.ts Outdated
Comment thread frontend/src/utils/__tests__/local-variables.test.ts Outdated
@dmadisetti

Copy link
Copy Markdown
Member Author

Created #9671 for flaky tests

Comment thread frontend/src/components/editor/errors/mangled-local-chip.tsx Outdated
Comment thread frontend/src/components/editor/output/MarimoTracebackOutput.tsx Outdated
@dmadisetti dmadisetti requested a review from kirangadhave May 26, 2026 18:26
@dmadisetti

Copy link
Copy Markdown
Member Author

Good suggestion @kirangadhave !

@kirangadhave kirangadhave left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🚀

@dmadisetti dmadisetti merged commit ddf04cd into main May 26, 2026
44 checks passed
@dmadisetti dmadisetti deleted the dm/pretty-locals branch May 26, 2026 18:47
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.9-dev6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

3 participants