Support the `del` statement · Issue #9347 · marimo-team/marimo · GitHub
Skip to content

Support the del statement #9347

@joewalp

Description

@joewalp

Description

Currently, Marimo's DAG execution graph does not honor the del statement for non-local variable definitions. In addition to not conforming to the intent of the Python language spec, this lack of support for del forces users to use a relatively noisy function scoping workaround to enable common coding patterns. See the Discussion section for use cases.

Steps to reproduce

Add the following cells to a notebook and then run it. (The full notebook source is quoted within the 'Code to reproduce'.)

@app.cell
def _():
    i = 0
    del i
    return


@app.cell
def _():
    i = 0
    return (i,)


@app.cell
def _(i):
    i
    return

Expected behavior

No errors. The i variable is deleted before the end of the first app.cell scope and thus is not emitted from the cell as a variable. The i variable is emitted from the second cell and then is used in the third cell.

Actual behavior

The first two app.cells yield an error of the following form:

This cell wasn't run because it has errors
This cell redefines variables from other cells.

'i' was also defined by:

cell-2
Fix: Wrap in a function
Why can't I redefine variables?
Need a scratchpad?

The third app.cell yields the following error:

Traceback (most recent call last):
  File "/tmp/marimo_66/__marimo__cell_vblA_.py", line 1, in <module>
    i
NameError: name 'i' is not defined. Did you mean: 'id'?

Discussion

Fixing this issue offers a less noisy and more pythonic way to express iterator and incremental construction patterns, which have been a source of user friction (e.g. #4282, #1477).

Compare this:

for item in ['a', 'b', 'c']:
    pass
del item

Versus the currently recommended workaround to enable redefinition of the item variable in subsequent cells.

def _():
    for item in ['a', 'b', 'c']:
        pass
    return

_()

I have framed this as a bug report rather than as a feature request because the Python spec clearly intends for the del statement to remove a variable definition from the namespace, and Marimo does not honor that intent.

See also:

https://docs.python.org/3/reference/simple_stmts.html#del

https://docs.python.org/3/tutorial/datastructures.html#the-del-statement

Will you submit a PR?

  • Yes

Environment

Details

Code to reproduce

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "marimo",
# ]
# ///

import marimo

__generated_with = "0.23.0"
app = marimo.App(
    width="medium",
    auto_download=["html"],
)

with app.setup:
    import marimo as mo


@app.cell
def _():
    i = 0
    del i
    return


@app.cell
def _():
    i = 0
    return (i,)


@app.cell
def _(i):
    i
    return


if __name__ == "__main__":
    app.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions