docs/guides/lint_rules/rules/reusable_definition_order.md
⚠️ Runtime ⚠️ Unsafe Fixable
MR003: Invalid ordering of potentially reusable definitions.
marimo serializes reusable definitions in notebook order. Like all python scripts, a reusable function cannot refer to a variable that has not yet been defined. While ordering in marimo normally doesn't matter, for reuse as a module or script, dependent top level definitions must be ordered correctly.
This rule flags and fixes function or class definitions that would normally be saved as "reusable", but cannot due to cell ordering.
When a reusable definition depends on another reusable definition declared later in the notebook:
Problematic:
@app.function
def uses_offset(x: int = offset()) -> int:
# This will run in marimo, but will cause an error if run as a script!
# `offset` is not defined!
return x + 1
@app.function
def offset() -> int:
return 1
Problematic:
@app.cell
def _():
# This could be reusable if it was defined after `decorate`.
class Wrapped:
@decorate
def value(self) -> int:
return 1
@app.function
def decorate(fn):
return fn
Move the referenced reusable definitions earlier in the notebook so they appear before the reusable definition that depends on them.
@app.function
def offset() -> int:
return 1
@app.function
def uses_offset(x: int = offset()) -> int:
return x + 1
This rule can be fixed with:
marimo check --fix --unsafe-fixes my_notebook.py
The unsafe fix reorders the provider cells earlier in the notebook. This is marked unsafe because changing cell order changes the document structure, even when the resulting notebook is still valid.