Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: strict mode NameError fix + cleaner exceptions #1829

Merged
merged 10 commits into from
Jul 25, 2024

Conversation

dmadisetti
Copy link
Collaborator

📝 Summary

Previous Exceptions:

image

vs

image

Also clean up the name error for strict execution.

Your PR will be reviewed more quickly if you can figure out the right person to tag with @ -->
@akshayka OR @mscolnick

Copy link

vercel bot commented Jul 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marimo-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2024 1:10am
marimo-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2024 1:10am

mscolnick
mscolnick previously approved these changes Jul 19, 2024
Copy link
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.

awesome 😎

(cell.last_expr, glbls)
)
# fmt: on
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be except BaseException as e:. Some libraries throw BaseExceptions (polars is one), and we need to communicate that exception info back to the user instead of hiding it in an "Unknown Error".

Copy link
Contributor

Choose a reason for hiding this comment

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

Taking a step back, related to my comment above — instead of handling a special MarimoBaseException, would it be possible to just rewrite the traceback and hide marimo internals?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could just modify the traceback by throwing out all frames preceding the marimo cell's execution (ie lines before File ...__marimo__cell...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! This is the source of the failing test. Fixed. Re-stack trace manipulation, see other comment.

I think that MarimoBaseException is probably not correctly named. It's really the MarimoExecutionException or CellRuntimeException, and provides the context that it was a evaluation cell that failed. Being able to unwrap the exception for a cleaner stack trace is definitely a plus, but not the only positive since it's an indication the failure came from a cell failure.

LMK if this context changes your thoughts/ if you have thoughts for a better name

Comment on lines 166 to 194
if _is_coroutine(cell.body):
# fmt: off
(
await marimo_output_evaluation
(cell.body, glbls)
)
# fmt: on
else:
# fmt: off
(
marimo_body_execution
(cell.body, glbls)
)
# fmt: on

if _is_coroutine(cell.last_expr):
# fmt: off
return (
await marimo_output_evaluation
(cell.last_expr, glbls)
)
# fmt: on
else:
# fmt: off
return (
marimo_output_evaluation
(cell.last_expr, glbls)
)
# fmt: on
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of formatting our source code to look good in a traceback, would it be possible to just modify the traceback in cell_runner to hide marimo internals? This seems difficult to maintain.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think modifying the stack trace, especially with different traces between script mode and notebook mode might introduce more code debt than this.

However, I do agree, that this is a little gimmicky (esp, since lint has to be manually disabled) and not needed.

The current trace of

    return eval(cell.last_expr, glbls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is fine, and I think it'd be better to revert to this than introduce stack trace manipulation.
An intermediate is without the formatting

return marimo_output_evaluation(cell.last_expr, glbls)

But at that point, the eval makes more sense. LMK what you think

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, makes sense. Reverting to eval/exec without special formatting sounds good to me.

marimo/_runtime/runner/cell_runner.py Outdated Show resolved Hide resolved
@dmadisetti
Copy link
Collaborator Author

Also added the name error catching for normal mode:

image

With other regular NameErrors passing through:

image

image

MarimoRef NameErrors in Strict Mode are a bit more verbose, since they should not occur:

image

with a potentially detected NameError preventing execution:

image

& edgecase for self definition

image

@dmadisetti dmadisetti changed the title Dm/exception clean Strict Mode Name Error fix + cleaner exceptions Jul 19, 2024
@dmadisetti dmadisetti changed the title Strict Mode Name Error fix + cleaner exceptions improvement: strict mode NameError fix + cleaner exceptions Jul 19, 2024
Copy link
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.

Few more comments. I do think that wrapping exceptions in MarimoBaseException complicates things.

I've seen approaches for directly modifying tracebacks, e.g. discussed here: https://discuss.python.org/t/dropping-frames-from-a-traceback/10385. I know I asked before, but couldn't it be feasible to just omit frames that precede the first file with name *__marimo__cell_* in CellRunner? I guess I don't see how that introduces tech debt.

marimo/_runtime/executor.py Outdated Show resolved Hide resolved
marimo/_runtime/runner/cell_runner.py Outdated Show resolved Hide resolved
marimo/_runtime/executor.py Outdated Show resolved Hide resolved
marimo/_runtime/executor.py Outdated Show resolved Hide resolved
marimo/_runtime/runner/cell_runner.py Show resolved Hide resolved
marimo/_runtime/runner/cell_runner.py Show resolved Hide resolved
marimo/_runtime/runner/cell_runner.py Outdated Show resolved Hide resolved
marimo/_runtime/runner/cell_runner.py Outdated Show resolved Hide resolved
tests/_ast/test_app.py Outdated Show resolved Hide resolved
@dmadisetti
Copy link
Collaborator Author

Not the most dramatic change, but I think it will pair very nicely with #1799

Copy link
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.

This looks great!! I like the distinction between runtime errors and marimo internal errors you've introduced.

I noticed one test is failing. Do you think that's unrelated? I would guess so ...

@akshayka
Copy link
Contributor

Reran and it's passing. Maybe just flaky.

@akshayka akshayka merged commit 2b7c32f into marimo-team:main Jul 25, 2024
28 checks passed
Copy link

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

@dmadisetti dmadisetti deleted the dm/exception-clean branch July 25, 2024 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants