marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
7.97k stars 281 forks source link

Issue with formating and persistent caching #2633

Open AdrienDart opened 1 month ago

AdrienDart commented 1 month ago

Describe the bug

When persistent caching a cell, I got the following error: AssertionError('Unexpected block') when the cell is formatted as follow (and the formatter gives this format when a is a bit chunk of code: " b = ( a )

Environment

0.9.9

Code to reproduce

`import polars as pl import marimo as mo

a=pl.DataFrame({a=[1]})`

New cell with mo.persistent_cache("my_cache"): (Indent) b = ( a )

If b = (a ), there is no issue

dmadisetti commented 1 month ago

Hmm, I don't think the formatting got preserved in your reporting. Do you mean something like:

with mo.persistent_cache("test"):
    a = (b
)

Under the hood, persistent cache attempts to extract the code block, so there might be something brittle there


tip use 3 backticks for formatting

```python
print("hello world")
AdrienDart commented 1 month ago

Apologies, I'm typing this on my phone.

This works:

with mo.persistent_cache("test"):
    a = (b
     )

This doesn't work (and the formatter formats the code like this):

with mo.persistent_cache("test"):
    a = (
          b
     )
dmadisetti commented 1 month ago

Got it. Thanks for reporting

Example notebook:

https://marimo.app/#code/JYWwDg9gTgLgBCAhlUEBQaD6mDmBTAOzykRjwBNMB3YGACzgF44AiABgDoBODgRjYC05PADdevFmkRgwTBMlQcAgjIAUASgxoAAtLAcAxngA2xtMIBmcbKsQAaBBHUAuNHHdwa9RxzDEAzsD+ZAQwmAaIBnR4qiwWwEQsLm4eqbwpqe4GcqoZmamIeamaqVB4MACuUARwqgZ2mhi6MoYmZpbWmKogTq4FcgDseV4MPb4BQSFhEVExLABGUBAA1oRJfflw83IA2kWbhZtwALp5ZZXVcPZbWs36RqbmeFY2yamgkLDyKD1X-o5ncpVGrdCANLTAF6YAiIEB4bBMZgsbBIBLYFgbdx6DhQCoEDRoIA

akshayka commented 2 weeks ago

Additional context from @dmadisetti:

It's an AST issue to determine where the block starts, we look for the first expression, and check to make sure it isn't in a for or try block, since that behavior would be undefined:

https://github.com/marimo-team/marimo/blob/cb39cdf71762c2034329d22d1e4d8ec705a980d0/marimo/_save/ast.py#L93