Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: `marimo check --fix` (#6288)

Dylan Madisettic3664923b8ea
diff --git a/CODEOWNERS b/CODEOWNERS
index bef6bd02e7..767bf05b6d 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -9,6 +9,9 @@
# Caching
/marimo/_save/ @dmadisetti
+# Linting
+/marimo/_lint/ @dmadisetti
+
# SQL
/marimo/_sql/ @light2dark
/marimo/_data/ @light2dark
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index 5338baff36..0b7d5f9e24 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -1,7 +1,6 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-import ast
import base64
import inspect
import os
@@ -25,6 +24,7 @@ from typing import (
from marimo._ast.app_config import _AppConfig
from marimo._ast.cell_id import external_prefix
+from marimo._ast.parse import ast_parse
from marimo._ast.variables import BUILTINS
from marimo._convert.converters import MarimoConvert
from marimo._schemas.serialization import (
@@ -531,7 +531,7 @@ class App:
errors: list[str] = []
for code in self._unparsable_code:
try:
- ast.parse(dedent(code))
+ ast_parse(dedent(code))
except SyntaxError as e:
error_line = e.text
error_marker: str = (
diff --git a/marimo/_ast/cell.py b/marimo/_ast/cell.py
index d5bd0b25ae..b8b53ae72d 100644
--- a/marimo/_ast/cell.py
+++ b/marimo/_ast/cell.py
@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, Literal, Optional
import msgspec
from marimo import _loggers
+from marimo._ast.parse import ast_parse
from marimo._ast.sql_visitor import SQLRef, SQLVisitor
from marimo._ast.visitor import ImportData, Language, Name, VariableData
from marimo._messaging.msgspec_encoder import asdict
@@ -220,7 +221,7 @@ class CellImpl:
def _get_sqls(self, raw: bool = False) -> list[str]:
try:
visitor = SQLVisitor(raw=raw)
- visitor.visit(ast.parse(self.code))
+ visitor.visit(ast_parse(self.code))
return visitor.get_sqls()
except Exception:
return []
@@ -293,7 +294,7 @@ class CellImpl:
def toplevel_variable(self) -> Optional[VariableData]:
"""Return the single, scoped, toplevel variable defined if found."""
try:
- tree = ast.parse(self.code)
+ tree = ast_parse(self.code)
except SyntaxError:
return None
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.py
index c6bcd4d653..9d997725dc 100644
--- a/marimo/_ast/codegen.py
+++ b/marimo/_ast/codegen.py
@@ -13,6 +13,7 @@ from marimo._ast.app_config import _AppConfig
from marimo._ast.cell import CellConfig, CellImpl
from marimo._ast.compiler import compile_cell
from marimo._ast.names import DEFAULT_CELL_NAME, SETUP_CELL_NAME
+from marimo._ast.parse import ast_parse
from marimo._ast.toplevel import TopLevelExtraction, TopLevelStatus
from marimo._ast.variables import BUILTINS
from marimo._ast.visitor import Name, VariableData
@@ -450,7 +451,7 @@ def get_header_comments(filename: str | Path) -> Optional[str]:
# Ensure the header only contains non-executable code
# ast parses out single line comments, so we only
# need to check that every node is not a multiline comment
- module = ast.parse(header)
+ module = ast_parse(header)
if any(not is_multiline_comment(node) for node in module.body):
return None
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index 83b3043ea3..febfe9171d 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -11,8 +11,10 @@ import re
import sys
import textwrap
import token as token_types
+import warnings
from tokenize import tokenize
-from typing import TYPE_CHECKING, Any, Callable, Optional
+from types import CodeType, FrameType
+from typing import Any, Callable, Optional, cast
from marimo import _loggers
from marimo._ast import parse
@@ -38,8 +40,12 @@ else:
LOGGER = _loggers.marimo_logger()
Cls: TypeAlias = type
-if TYPE_CHECKING:
- from types import FrameType
+
+def ast_compile(*args: Any, **kwargs: Any) -> CodeType:
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", category=SyntaxWarning)
+ # The SyntaxWarning is suppressed only inside this `with` block
+ return cast(CodeType, compile(*args, **kwargs)) # type: ignore[call-overload]
def code_key(code: str) -> int:
@@ -168,14 +174,19 @@ def compile_cell(
# See https://github.com/pyodide/pyodide/issues/3337,
# https://github.com/marimo-team/marimo/issues/1546
code = code.replace("\u00a0", " ")
- module = compile(
- code,
- "<unknown>",
- mode="exec",
- # don't inherit compiler flags, in particular future annotations
- dont_inherit=True,
- flags=ast.PyCF_ONLY_AST | ast.PyCF_ALLOW_TOP_LEVEL_AWAIT,
+ # Overloads on compile are strange, cast for proper typing.
+ module = cast(
+ ast.Module,
+ ast_compile(
+ code,
+ "<unknown>",
+ mode="exec",
+ # don't inherit compiler flags, in particular future annotations
+ dont_inherit=True,
+ flags=ast.PyCF_ONLY_AST | ast.PyCF_ALLOW_TOP_LEVEL_AWAIT,
+ ),
)
+
if not module.body:
# either empty code or just comments
return CellImpl(
@@ -208,7 +219,8 @@ def compile_cell(
# Use final expression if it exists doesn't end in a
# semicolon. Evaluates expression to "None" otherwise.
if isinstance(final_expr, ast.Expr) and not ends_with_semicolon(code):
- expr = ast.Expression(module.body.pop().value)
+ module.body.pop()
+ expr = ast.Expression(final_expr.value)
expr.lineno = final_expr.lineno # type: ignore[attr-defined]
else:
const = ast.Constant(value=None)
@@ -250,10 +262,10 @@ def compile_cell(
)
flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
- body = compile(
+ body = ast_compile(
module, filename, mode="exec", dont_inherit=True, flags=flags
)
- last_expr = compile(
+ last_expr = ast_compile(
expr, filename, mode="eval", dont_inherit=True, flags=flags
)
@@ -378,7 +390,7 @@ def context_cell_factory(
entry_line += 1 - lnum
_, with_block = ContainedExtractWithBlock(entry_line).visit(
- ast.parse(textwrap.dedent(source)).body # type: ignore[arg-type]
+ parse.ast_parse(textwrap.dedent(source)).body # type: ignore[arg-type]
)
start_node = with_block.body[0]
@@ -426,7 +438,7 @@ def toplevel_cell_factory(
# We need to scrub through the initial decorator. Since we don't care about
# indentation etc, easiest just to use AST.
- tree = ast.parse(function_code, type_comments=True)
+ tree = parse.ast_parse(function_code, type_comments=True)
try:
decorator = tree.body[0].decorator_list.pop(0) # type: ignore
# NB. We don't unparse from the AST because it strips comments.
@@ -509,7 +521,7 @@ def cell_factory(
function_code = textwrap.dedent("".join(code))
extractor = parse.Extractor(contents=function_code)
- func_ast = ast.parse(function_code).body[0]
+ func_ast = parse.ast_parse(function_code).body[0]
cell_def = extractor.to_cell(func_ast, attribute="cell")
# anonymous file is required for deterministic testing.
diff --git a/marimo/_ast/load.py b/marimo/_ast/load.py
index 8f1d37f78c..87bf033005 100644
--- a/marimo/_ast/load.py
+++ b/marimo/_ast/load.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import importlib.util
import sys
+from dataclasses import dataclass
from pathlib import Path
from typing import Literal, Optional, Union
@@ -27,7 +28,12 @@ LOGGER = _loggers.marimo_logger()
# However for "managed" marimo (i.e. run/ edit), the expectation is to not fail
# on startup and defer errors to the runtime.
-NotebookStatus = Literal["empty", "has_errors", "invalid", "valid"]
+
+@dataclass
+class LoadResult:
+ status: Literal["empty", "has_errors", "invalid", "valid"] = "empty"
+ notebook: Optional[NotebookSerialization] = None
+ contents: Optional[str] = None
def _maybe_contents(filename: Optional[Union[str, Path]]) -> Optional[str]:
@@ -69,7 +75,7 @@ def _static_load(filepath: Path) -> Optional[App]:
if not contents:
return None
- notebook = parse_notebook(contents)
+ notebook = parse_notebook(contents, filepath=str(filepath))
if notebook and is_non_marimo_python_script(notebook):
# Should fail instead of overriding contents
@@ -80,7 +86,16 @@ def _static_load(filepath: Path) -> Optional[App]:
if notebook is None or not notebook.valid:
return None
- app = App(**notebook.app.options, _filename=str(filepath))
+ return load_notebook_ir(notebook, filepath=str(filepath))
+
+
+def load_notebook_ir(
+ notebook: NotebookSerialization, filepath: Optional[str] = None
+) -> App:
+ # Use filepath from notebook if not explicitly provided
+ if filepath is None:
+ filepath = notebook.filename
+ app = App(**notebook.app.options, _filename=filepath)
for cell in notebook.cells:
if isinstance(cell, UnparsableCell):
app._unparsable_cell(cell.code, **cell.options)
@@ -89,7 +104,42 @@ def _static_load(filepath: Path) -> Optional[App]:
return app
-def get_notebook_status(filename: str) -> NotebookStatus:
+def load_notebook(filename: str) -> Optional[NotebookSerialization]:
+ """Load and return notebook serialization from a marimo notebook file.
+
+ Args:
+ filename: Path to a marimo notebook file (.py or .md)
+
+ Returns:
+ NotebookSerialization if the file exists and contains valid code,
+ None if the file is empty or contains only comments.
+
+ Raises:
+ MarimoFileError: If the file exists but doesn't define a valid marimo app
+ RuntimeError: If there are issues loading the module
+ SyntaxError: If the file contains a syntax error
+ FileNotFoundError: If the file doesn't exist
+ """
+ path = Path(filename)
+
+ contents = _maybe_contents(filename)
+ if not contents:
+ return None
+
+ if path.suffix in (".md", ".qmd"):
+ from marimo._convert.markdown.markdown import (
+ convert_from_md_to_marimo_ir,
+ )
+
+ return convert_from_md_to_marimo_ir(contents)
+
+ if path.suffix == ".py":
+ return parse_notebook(contents)
+
+ raise MarimoFileError("File must end with .py, .md, or .qmd.")
+
+
+def get_notebook_status(filename: str) -> LoadResult:
"""Attempts to parse an app- should raise SyntaxError on failure.
Args:
@@ -105,7 +155,7 @@ def get_notebook_status(filename: str) -> NotebookStatus:
contents = _maybe_contents(filename)
if not contents:
- return "empty"
+ return LoadResult(status="empty", contents=contents)
notebook: Optional[NotebookSerialization] = None
if path.suffix in (".md", ".qmd"):
@@ -115,22 +165,26 @@ def get_notebook_status(filename: str) -> NotebookStatus:
notebook = convert_from_md_to_marimo_ir(contents)
elif path.suffix == ".py":
- notebook = parse_notebook(contents)
+ notebook = parse_notebook(contents, filepath=filename)
else:
raise MarimoFileError("File must end with .py, .md, or .qmd.")
# NB. A invalid notebook can still be opened.
if notebook is None:
- return "empty"
+ return LoadResult(status="empty", contents=contents)
if not notebook.valid:
- return "invalid"
+ return LoadResult(
+ status="invalid", notebook=notebook, contents=contents
+ )
if len(notebook.violations) > 0:
LOGGER.debug(
"Notebook has violations: \n%s",
"\n".join(map(repr, notebook.violations)),
)
- return "has_errors"
- return "valid"
+ return LoadResult(
+ status="has_errors", notebook=notebook, contents=contents
+ )
+ return LoadResult(status="valid", notebook=notebook, contents=contents)
def load_app(filename: Optional[str]) -> Optional[App]:
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index c7b5ef77a5..76ea5ea4a7 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import ast
import io
import token as token_types
+import warnings
from pathlib import Path
from textwrap import dedent
from tokenize import TokenInfo, tokenize
@@ -45,6 +46,17 @@ V = TypeVar("V")
U = TypeVar("U")
+def ast_parse(
+ contents: str, suppress_warnings: bool = True, **kwargs: Any
+) -> ast.Module:
+ if not suppress_warnings:
+ return cast(ast.Module, ast.parse(contents, **kwargs))
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", category=SyntaxWarning)
+ # The SyntaxWarning is suppressed only inside this `with` block
+ return cast(ast.Module, ast.parse(contents, **kwargs))
+
+
def fixed_dedent(text: str) -> str:
"""Manually edited code, can dedent"""
# Added robustness for AI generated code
@@ -387,13 +399,22 @@ class Parser:
@staticmethod
def from_file(filename: Union[str, Path]) -> Parser:
- return Parser(contents=Path(filename).read_text(encoding="utf-8"))
+ return Parser(
+ contents=Path(filename).read_text(encoding="utf-8"),
+ filepath=str(filename),
+ )
- def __init__(self, contents: str):
+ def __init__(self, contents: str, filepath: str = "<marimo>"):
self.extractor = Extractor(contents=contents)
+ self.filepath = filepath
def node_stack(self) -> PeekStack[Node]:
- return PeekStack(iter(ast.parse(self.extractor.contents or "").body))
+ tree = ast_parse(
+ self.extractor.contents or "",
+ filename=self.filepath,
+ suppress_warnings=False,
+ )
+ return PeekStack(iter(tree.body))
def parse_header(self, body: PeekStack[Node]) -> ParseResult[Header]:
# header? = (docstring | comments)*
@@ -440,14 +461,14 @@ class Parser:
if node.names[0].asname:
violations.append(
Violation(
- "`marimo` is typically not imported with an alias. ",
+ MARIMO_ALIAS_VIOLATION,
node.lineno,
)
)
return ParseResult(node, violations=violations)
violations.append(
Violation(
- "Unexpected statement (expected marimo import)",
+ UNEXPECTED_STATEMENT_MARIMO_IMPORT_VIOLATION,
lineno=node.lineno,
)
)
@@ -464,7 +485,7 @@ class Parser:
lineno = node.lineno if node else 0
violations.append(
Violation(
- "Expected `__generated_with` assignment for marimo version number.",
+ EXPECTED_GENERATED_WITH_VIOLATION,
lineno=lineno,
)
)
@@ -492,7 +513,7 @@ class Parser:
)
violations.append(
Violation(
- "Unexpected statement, expected App initialization.",
+ UNEXPECTED_STATEMENT_APP_INIT_VIOLATION,
node.lineno,
)
)
@@ -513,7 +534,7 @@ class Parser:
return ParseResult(violations=violations)
violations.append(
Violation(
- "Unexpected statement, expected cell definitions.",
+ UNEXPECTED_STATEMENT_CELL_DEF_VIOLATION,
node.lineno,
)
)
@@ -539,7 +560,7 @@ class Parser:
else:
violations.append(
Violation(
- "Unexpected statement, expected body cell definition.",
+ UNEXPECTED_STATEMENT_BODY_CELL_VIOLATION,
node.lineno,
)
)
@@ -620,7 +641,7 @@ def _eval_kwargs(
else:
violations.append(
Violation(
- "Unexpected value for keyword argument",
+ UNEXPECTED_KEYWORD_VALUE_VIOLATION,
lineno=kw.lineno,
col_offset=kw.col_offset,
)
@@ -875,12 +896,14 @@ def is_cell(node: Optional[Node]) -> bool:
def is_run_guard(node: Optional[Node]) -> bool:
- basis = ast.parse('if __name__ == "__main__": app.run()').body[0]
+ basis = ast_parse('if __name__ == "__main__": app.run()').body[0]
return bool(node and is_equal_ast(basis, node))
-def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
- parser = Parser(contents)
+def parse_notebook(
+ contents: str, filepath: str = "<marimo>"
+) -> Optional[NotebookSerialization]:
+ parser = Parser(contents, filepath=filepath)
if not parser.extractor.contents:
return None
@@ -896,7 +919,7 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
if not (import_result := parser.parse_import(body)):
violations.append(
Violation(
- "Only able to extract header.",
+ ONLY_HEADER_EXTRACTED_VIOLATION,
lineno=1,
)
)
@@ -906,7 +929,7 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
# just a header is fine, anything else we would ignore and override
violations.append(
Violation(
- _non_marimo_python_script_violation_description,
+ NON_MARIMO_PYTHON_SCRIPT_VIOLATION,
lineno=header.end_lineno + 2 if header.value else 1,
)
)
@@ -924,6 +947,7 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
cells=[],
violations=violations,
valid=False,
+ filename=filepath,
)
violations.extend(import_result.violations)
# Extract import alias for the reference
@@ -958,6 +982,7 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
app=app,
violations=violations,
cells=cells,
+ filename=filepath,
)
body_result = parser.parse_body(body)
@@ -966,7 +991,7 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
# Expected a run guard, but that's OK.
if not is_run_guard(body.last):
- violations.append(Violation("Expected run guard statement"))
+ violations.append(Violation(EXPECTED_RUN_GUARD_VIOLATION))
return NotebookSerialization(
header=header,
@@ -974,16 +999,35 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
app=app,
cells=cells,
violations=violations,
+ filename=filepath,
)
-_non_marimo_python_script_violation_description = (
- "non-marimo Python content beyond header"
+# Violation message constants
+MARIMO_ALIAS_VIOLATION = "`marimo` is typically not imported with an alias. "
+UNEXPECTED_STATEMENT_MARIMO_IMPORT_VIOLATION = (
+ "Unexpected statement (expected marimo import)"
+)
+EXPECTED_GENERATED_WITH_VIOLATION = (
+ "Expected `__generated_with` assignment for marimo version number."
+)
+UNEXPECTED_STATEMENT_APP_INIT_VIOLATION = (
+ "Unexpected statement, expected App initialization."
+)
+UNEXPECTED_STATEMENT_CELL_DEF_VIOLATION = (
+ "Unexpected statement, expected cell definitions."
+)
+UNEXPECTED_STATEMENT_BODY_CELL_VIOLATION = (
+ "Unexpected statement, expected body cell definition."
)
+UNEXPECTED_KEYWORD_VALUE_VIOLATION = "Unexpected value for keyword argument"
+ONLY_HEADER_EXTRACTED_VIOLATION = "Only able to extract header."
+NON_MARIMO_PYTHON_SCRIPT_VIOLATION = "non-marimo Python content beyond header"
+EXPECTED_RUN_GUARD_VIOLATION = "Expected run guard statement"
def is_non_marimo_python_script(notebook: NotebookSerialization) -> bool:
return any(
- (v.description == _non_marimo_python_script_violation_description)
+ (v.description == NON_MARIMO_PYTHON_SCRIPT_VIOLATION)
for v in notebook.violations
)
diff --git a/marimo/_ast/pytest.py b/marimo/_ast/pytest.py
index 2bc38fc6bb..ee2515f533 100644
--- a/marimo/_ast/pytest.py
+++ b/marimo/_ast/pytest.py
@@ -11,6 +11,7 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, NoReturn, TypeVar, cast
from marimo._ast.cell import Cell
+from marimo._ast.parse import ast_parse
from marimo._runtime.context import ContextNotInitializedError, get_context
if TYPE_CHECKING:
@@ -43,7 +44,7 @@ def build_stub_fn(
) -> Callable[..., Any]:
# Avoid declaring the function in the global scope, since it may cause
# issues with meta-analysis tools like cxfreeze (see #3828).
- PYTEST_BASE = ast.parse(inspect.getsource(_pytest_scaffold))
+ PYTEST_BASE = ast_parse(inspect.getsource(_pytest_scaffold))
# We modify the signature of the cell function such that pytest
# does not attempt to use the arguments as fixtures.
@@ -100,7 +101,7 @@ def build_stub_fn(
def wrap_fn_for_pytest(func: Fn, cell: Cell) -> Callable[..., Any]:
- func_ast = ast.parse(inspect.getsource(func))
+ func_ast = ast_parse(inspect.getsource(func))
func_body = func_ast.body[0]
assert isinstance(func_body, (ast.FunctionDef, ast.AsyncFunctionDef))
@@ -279,7 +280,7 @@ def process_for_pytest(func: Fn, cell: Cell) -> None:
# Turn off test for the cell itself in this case.
cell._test_allowed = False
- tree = ast.parse(inspect.getsource(func))
+ tree = ast_parse(inspect.getsource(func))
run = functools.cache(cell.run)
# Must be a unique name, otherwise won't be injected properly.
diff --git a/marimo/_ast/transformers.py b/marimo/_ast/transformers.py
index 4f6ae750dd..5fd5425380 100644
--- a/marimo/_ast/transformers.py
+++ b/marimo/_ast/transformers.py
@@ -6,6 +6,7 @@ import inspect
import textwrap
from typing import TYPE_CHECKING, Any, Callable, Optional, cast
+from marimo._ast.parse import ast_parse
from marimo._ast.variables import unmangle_local
if TYPE_CHECKING:
@@ -81,7 +82,7 @@ def clean_to_modules(
def strip_function(fn: Callable[..., Any]) -> ast.Module:
code, _ = inspect.getsourcelines(fn)
args = set(fn.__code__.co_varnames)
- function_ast = ast.parse(textwrap.dedent("".join(code)))
+ function_ast = ast_parse(textwrap.dedent("".join(code)))
body = function_ast.body.pop()
assert isinstance(body, (ast.FunctionDef, ast.AsyncFunctionDef)), (
"Expected a function definition"
@@ -194,7 +195,7 @@ class RemoveImportTransformer(ast.NodeTransformer):
self.import_name = import_name
def strip_imports(self, code: str) -> str:
- tree = ast.parse(code)
+ tree = ast_parse(code)
tree = self.visit(tree)
return ast.unparse(tree).strip()
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index ab941bd95f..5eea6722e4 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -29,6 +29,7 @@ from marimo._cli.run_docker import (
)
from marimo._cli.upgrade import check_for_updates, print_latest_version
from marimo._config.settings import GLOBAL_SETTINGS
+from marimo._lint import run_check
from marimo._server.file_router import AppFileRouter
from marimo._server.model import SessionMode
from marimo._server.start import start
@@ -59,7 +60,7 @@ def helpful_usage_error(self: Any, file: Any = None) -> None:
def check_app_correctness(filename: str, noninteractive: bool = True) -> None:
try:
- status = get_notebook_status(filename)
+ status = get_notebook_status(filename).status
except SyntaxError:
import traceback
@@ -1220,6 +1221,60 @@ def shell_completion() -> None:
)
[email protected](help="""Check and format marimo files.""")
+ "--fix",
+ is_flag=True,
+ default=False,
+ show_default=True,
+ type=bool,
+ help="Whether to in place update files.",
+)
+ "--strict",
+ is_flag=True,
+ default=False,
+ show_default=True,
+ type=bool,
+ help="Whether warnings return a non-zero exit code.",
+)
+ "-v/-q",
+ "--verbose/--quiet",
+ is_flag=True,
+ default=True,
+ show_default=True,
+ type=bool,
+ help="Whether to print detailed messages.",
+)
[email protected]("files", nargs=-1, type=click.UNPROCESSED)
+def check(
+ fix: bool, strict: bool, verbose: bool, files: tuple[str, ...]
+) -> None:
+ if not files:
+ # If no files are provided, we lint the current directory
+ files = ("**/*.py", "**/*.md", "**/*.qmd")
+
+ # Pass click.echo directly as pipe for streaming output, or None
+ pipe = click.echo if verbose else None
+ linter = run_check(files, pipe=pipe, fix=fix)
+
+ # Get counts from linter (fix happens automatically during streaming)
+ fixed = linter.fixed_count
+ total_issues = linter.issues_count
+
+ # Final summary
+ if fixed > 0:
+ click.echo(f"Updated {fixed} file{'s' if fixed > 1 else ''}.")
+ if total_issues > 0:
+ click.echo(
+ f"Found {total_issues} issue{'s' if total_issues > 1 else ''}."
+ )
+
+ if linter.errored or (strict and (fixed > 0 or total_issues > 0)):
+ sys.exit(1)
+
+
main.command()(convert)
main.add_command(export)
main.add_command(config)
diff --git a/marimo/_cli/print.py b/marimo/_cli/print.py
index 57c33931e8..15ab443deb 100644
--- a/marimo/_cli/print.py
+++ b/marimo/_cli/print.py
@@ -71,6 +71,13 @@ def red(text: str, bold: bool = False) -> str:
return prefix + text + "\033[0m"
+def cyan(text: str, bold: bool = False) -> str:
+ if not _USE_COLOR:
+ return text
+ prefix = "\033[94m" if not bold else "\033[1;94m"
+ return prefix + text + "\033[0m"
+
+
def muted(text: str) -> str:
# Use dark gray (37 is white, 2 is dim) which is more widely supported than 90
return "\033[37;2m" + text + "\033[0m" if _USE_COLOR else text
diff --git a/marimo/_convert/converters.py b/marimo/_convert/converters.py
index b72edc356d..8ec21103a8 100644
--- a/marimo/_convert/converters.py
+++ b/marimo/_convert/converters.py
@@ -20,8 +20,13 @@ class MarimoConverterIntermediate:
return convert_from_ir_to_notebook_v1(self.ir)
- def to_markdown(self) -> str:
- raise NotImplementedError("Not implemented")
+ def to_markdown(self, filename: str | None = None) -> str:
+ """Convert to markdown format."""
+ from marimo._server.export.exporter import Exporter
+
+ exporter = Exporter()
+ generated_contents, _ = exporter.export_as_md(self.ir, filename)
+ return generated_contents
def to_py(self) -> str:
"""Convert to python format."""
diff --git a/marimo/_lint/__init__.py b/marimo/_lint/__init__.py
new file mode 100644
index 0000000000..0d8d12d522
--- /dev/null
+++ b/marimo/_lint/__init__.py
@@ -0,0 +1,51 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.linter import FileStatus, Linter
+from marimo._lint.rule_engine import EarlyStoppingConfig, RuleEngine
+from marimo._lint.rules.base import LintRule
+from marimo._utils.files import expand_file_patterns
+
+if TYPE_CHECKING:
+ from collections.abc import Callable
+
+
+def run_check(
+ file_patterns: tuple[str, ...],
+ pipe: Callable[[str], None] | None = None,
+ fix: bool = False,
+) -> Linter:
+ """Run linting checks on files matching patterns (CLI entry point).
+
+ High-level interface that handles file discovery, parsing, and aggregation.
+ Used by the `marimo check` command.
+
+ Args:
+ file_patterns: Glob patterns for file discovery
+ pipe: Optional function to call for streaming output
+ fix: Whether to fix files automatically
+
+ Returns:
+ Linter with per-file status and diagnostics
+ """
+ # Expand patterns to actual files
+ files_to_check = expand_file_patterns(file_patterns)
+
+ linter = Linter(pipe=pipe, fix_files=fix)
+ linter.run_streaming(files_to_check)
+ return linter
+
+
+__all__ = [
+ "Diagnostic",
+ "LintRule",
+ "Severity",
+ "EarlyStoppingConfig",
+ "RuleEngine",
+ "run_check",
+ "Linter",
+ "FileStatus",
+]
diff --git a/marimo/_lint/context.py b/marimo/_lint/context.py
new file mode 100644
index 0000000000..d6b8065a55
--- /dev/null
+++ b/marimo/_lint/context.py
@@ -0,0 +1,171 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import asyncio
+import heapq
+import threading
+from typing import TYPE_CHECKING
+
+from marimo._ast.load import load_notebook_ir
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._schemas.serialization import NotebookSerialization
+
+if TYPE_CHECKING:
+ from marimo._lint.rules.base import LintRule
+ from marimo._runtime.dataflow import DirectedGraph
+
+# Priority mapping: lower numbers = higher priority
+PRIORITY_MAP = {
+ Severity.BREAKING: 0,
+ Severity.RUNTIME: 1,
+ Severity.FORMATTING: 2,
+}
+
+
+class LintContext:
+ """Context for lint rule execution with priority queuing and graph caching."""
+
+ def __init__(
+ self,
+ notebook: NotebookSerialization,
+ stderr: str = "",
+ stdout: str = "",
+ ):
+ self.notebook = notebook
+ self._diagnostics: list[tuple[int, int, Diagnostic]] = []
+ self._graph: DirectedGraph | None = None
+ self._graph_lock = threading.Lock()
+ self._diagnostics_lock: asyncio.Lock | None = None # Lazy-initialized
+ self._counter = 0 # Monotonic counter for stable sorting
+ self._last_retrieved_counter = (
+ -1
+ ) # Track what was last retrieved for streaming
+
+ self.stderr = stderr
+ self.stdout = stdout
+
+ def _get_diagnostics_lock(self) -> asyncio.Lock:
+ """Get the diagnostics lock, creating it if needed."""
+ if self._diagnostics_lock is None:
+ self._diagnostics_lock = asyncio.Lock()
+ return self._diagnostics_lock
+
+ async def add_diagnostic(self, diagnostic: Diagnostic) -> None:
+ """Add a diagnostic to the priority queue."""
+ priority = 999 # Default low priority
+ if diagnostic.severity:
+ priority = PRIORITY_MAP.get(diagnostic.severity, priority)
+
+ # Use counter as tiebreaker to avoid comparing Diagnostic objects
+ async with self._get_diagnostics_lock():
+ heapq.heappush(
+ self._diagnostics, (priority, self._counter, diagnostic)
+ )
+ self._counter += 1
+
+ async def get_diagnostics(self) -> list[Diagnostic]:
+ """Get all diagnostics sorted by priority (most severe first)."""
+ # Sort by priority and return just the diagnostics
+ async with self._get_diagnostics_lock():
+ sorted_diagnostics = []
+ temp_heap = self._diagnostics.copy()
+
+ while temp_heap:
+ _, _, diagnostic = heapq.heappop(temp_heap)
+ sorted_diagnostics.append(diagnostic)
+
+ return sorted_diagnostics
+
+ async def get_new_diagnostics(self) -> list[Diagnostic]:
+ """Get diagnostics added since last call, sorted by priority."""
+ async with self._get_diagnostics_lock():
+ # Find new diagnostics since last retrieval
+ new_items = [
+ (priority, counter, diagnostic)
+ for priority, counter, diagnostic in self._diagnostics
+ if counter > self._last_retrieved_counter
+ ]
+
+ if not new_items:
+ return []
+
+ # Sort by priority (and counter for stability)
+ new_items.sort()
+
+ # Extract diagnostics and update counter
+ new_diagnostics = []
+ max_counter = self._last_retrieved_counter
+
+ for _priority, counter, diagnostic in new_items:
+ new_diagnostics.append(diagnostic)
+ max_counter = max(max_counter, counter)
+
+ # Update the last retrieved counter
+ self._last_retrieved_counter = max_counter
+
+ return new_diagnostics
+
+ def get_graph(self) -> DirectedGraph:
+ """Get the dependency graph, constructing it once and caching."""
+ if self._graph is not None:
+ return self._graph
+
+ with self._graph_lock:
+ # Double-check pattern for thread safety
+ if self._graph is not None:
+ return self._graph
+
+ # Construct the graph
+ app = load_notebook_ir(self.notebook)
+ self._graph = app._graph
+
+ # Initialize the app to register cells in the graph
+ for cell_id, cell in app._cell_manager.valid_cells():
+ self._graph.register_cell(cell_id, cell._cell)
+
+ return self._graph
+
+
+class RuleContext:
+ """Context for a specific rule execution that wraps LintContext with filename info."""
+
+ def __init__(self, global_context: LintContext, rule: LintRule):
+ self.global_context = global_context
+ self.rule = rule
+
+ async def add_diagnostic(self, diagnostic: Diagnostic) -> None:
+ """Add a diagnostic with context filled in from rule and notebook."""
+ # Backfill any None attributes from rule defaults
+ if diagnostic.code is None:
+ diagnostic.code = self.rule.code
+ if diagnostic.name is None:
+ diagnostic.name = self.rule.name
+ if diagnostic.severity is None:
+ diagnostic.severity = self.rule.severity
+ if diagnostic.fixable is None:
+ diagnostic.fixable = self.rule.fixable
+
+ # Set filename from notebook
+ if diagnostic.filename is None:
+ diagnostic.filename = self.global_context.notebook.filename
+
+ await self.global_context.add_diagnostic(diagnostic)
+
+ def get_graph(self) -> DirectedGraph:
+ """Access to the dependency graph."""
+ return self.global_context.get_graph()
+
+ @property
+ def notebook(self) -> NotebookSerialization:
+ """Access to the notebook being linted."""
+ return self.global_context.notebook
+
+ @property
+ def stdout(self) -> str:
+ """Access to the captured stdout."""
+ return self.global_context.stdout
+
+ @property
+ def stderr(self) -> str:
+ """Access to the captured stderr."""
+ return self.global_context.stderr
diff --git a/marimo/_lint/diagnostic.py b/marimo/_lint/diagnostic.py
new file mode 100644
index 0000000000..ee8b704bcf
--- /dev/null
+++ b/marimo/_lint/diagnostic.py
@@ -0,0 +1,77 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from dataclasses import dataclass
+from enum import Enum
+from typing import Optional, cast
+
+from marimo._types.ids import CellId_t
+
+
+class Severity(Enum):
+ """Severity levels for diagnostic errors."""
+
+ FORMATTING = "formatting" # prefix: MF0000
+ RUNTIME = "runtime" # prefix: MR0000
+ BREAKING = "breaking" # prefix: MB0000
+
+
+def line_num(line: int) -> str:
+ """Format line number for display."""
+ return f"{line:4d}"
+
+
+@dataclass
+class Diagnostic:
+ """Represents a diagnostic found in a notebook."""
+
+ message: str
+ cell_id: None | list[CellId_t]
+ line: int | list[int]
+ column: int | list[int]
+ code: Optional[str] = None
+ name: Optional[str] = None
+ severity: Optional[Severity] = None
+ fixable: Optional[bool] = None
+ fix: Optional[str] = None
+ filename: Optional[str] = None
+
+ def format(
+ self,
+ code_lines: list[str] | None = None,
+ formatter: str = "full",
+ ) -> str:
+ """Format the diagnostic for display.
+
+ Args:
+ filename: The filename where the diagnostic occurred (optional)
+ code_lines: Optional source code lines for context
+ formatter: The formatter to use ("full" is the only supported option)
+
+ Returns:
+ Formatted diagnostic string
+ """
+ from marimo._lint.formatter import FullFormatter
+
+ if formatter == "full":
+ fmt = FullFormatter()
+ # Use stored filename if available, then parameter, then "unknown"
+ actual_filename = self.filename or "unknown"
+ return fmt.format(self, actual_filename, code_lines)
+ else:
+ raise ValueError(f"Unsupported formatter: {formatter}")
+
+ @property
+ def sorted_lines(self) -> tuple[tuple[int], tuple[int]]:
+ """Get sorted line numbers as a list."""
+ lines: list[int] = (
+ self.line if isinstance(self.line, list) else [self.line]
+ )
+ columns: list[int] = (
+ self.column if isinstance(self.column, list) else [self.column]
+ )
+ # mypy seems unable to infer the type
+ return cast(
+ tuple[tuple[int], tuple[int]],
+ tuple(zip(*sorted(zip(lines, columns)))),
+ )
diff --git a/marimo/_lint/formatter.py b/marimo/_lint/formatter.py
new file mode 100644
index 0000000000..303acd44cb
--- /dev/null
+++ b/marimo/_lint/formatter.py
@@ -0,0 +1,113 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import os
+from abc import ABC, abstractmethod
+from typing import TYPE_CHECKING
+
+from marimo._cli.print import bold, cyan, red, yellow
+from marimo._lint.diagnostic import Severity
+
+if TYPE_CHECKING:
+ from marimo._lint.diagnostic import Diagnostic
+
+
+class DiagnosticFormatter(ABC):
+ """Abstract base class for formatting diagnostics."""
+
+ @abstractmethod
+ def format(
+ self,
+ diagnostic: Diagnostic,
+ filename: str,
+ code_lines: list[str] | None = None,
+ ) -> str:
+ """Format a diagnostic for display.
+
+ Args:
+ diagnostic: The diagnostic to format
+ filename: The filename where the diagnostic occurred
+ code_lines: Optional source code lines for context
+
+ Returns:
+ Formatted diagnostic string
+ """
+ pass
+
+
+class FullFormatter(DiagnosticFormatter):
+ """Full formatter that shows diagnostics with code context and colors."""
+
+ def format(
+ self,
+ diagnostic: Diagnostic,
+ filename: str,
+ code_lines: list[str] | None = None,
+ ) -> str:
+ """Format the diagnostic for display with code context."""
+
+ # Implementation error if this is raised.
+ assert diagnostic.name is not None, diagnostic
+ assert diagnostic.severity is not None, diagnostic
+
+ severity_color = {
+ Severity.FORMATTING: yellow,
+ Severity.RUNTIME: red,
+ Severity.BREAKING: red,
+ }.get(diagnostic.severity, bold)
+ severity_str = {
+ Severity.FORMATTING: "warning",
+ Severity.RUNTIME: "error",
+ Severity.BREAKING: "critical",
+ }.get(diagnostic.severity, "info")
+
+ lines, columns = diagnostic.sorted_lines
+
+ location = filename
+ if max(lines + (0,)) > 0:
+ location = f"{filename}:{lines[0]}:{columns[0]}"
+
+ header = f"{bold(severity_color(severity_str + '[' + diagnostic.name + ']'))}: {bold(diagnostic.message)}"
+ header += "\n" + cyan(" --> ") + cyan(location)
+
+ # If code_lines is None, try to read from file
+ if code_lines is None and diagnostic.line and os.path.exists(filename):
+ try:
+ with open(filename, encoding="utf-8") as f:
+ code_lines = f.read().splitlines()
+ except (OSError, UnicodeDecodeError):
+ # If we can't read the file, just return header
+ return header
+
+ if code_lines is None or not diagnostic.line:
+ return header
+
+ context_lines = []
+ previous_line = -1
+ for line, column in zip(lines, columns):
+ line += 1 # Convert to 1-based index
+ # Show context: 1 line above, current line, 1 line below
+ start_line = max(1, line - 1)
+ end_line = min(len(code_lines), line + 1)
+ if previous_line != -1 and start_line > previous_line + 2:
+ context_lines.append(" ...")
+
+ for i in range(start_line, end_line + 1):
+ line_content = (
+ code_lines[i - 1] if i <= len(code_lines) else ""
+ )
+ line_num = cyan(f"{i:4d} |")
+
+ if i == line:
+ # Current line with error indicator
+ context_lines.append(f"{line_num} {line_content}")
+ # Add error indicator line
+ indicator = " " * 5 + " " * (column - 2) + red("^" * 1)
+ context_lines.append(f" {cyan('|')}{indicator}")
+ else:
+ context_lines.append(f"{line_num} {line_content}")
+ previous_line = end_line
+
+ if diagnostic.fix:
+ context_lines.append(cyan("info: ") + bold(diagnostic.fix))
+ return f"{header}\n" + "\n".join(context_lines)
diff --git a/marimo/_lint/linter.py b/marimo/_lint/linter.py
new file mode 100644
index 0000000000..92366037bf
--- /dev/null
+++ b/marimo/_lint/linter.py
@@ -0,0 +1,244 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import asyncio
+import contextlib
+import io
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import TYPE_CHECKING
+
+from marimo._ast.load import get_notebook_status
+from marimo._ast.parse import MarimoFileError
+from marimo._cli.print import red
+from marimo._convert.converters import MarimoConvert
+from marimo._lint.diagnostic import Diagnostic
+from marimo._lint.rule_engine import EarlyStoppingConfig, RuleEngine
+from marimo._schemas.serialization import NotebookSerialization
+
+if TYPE_CHECKING:
+ from collections.abc import AsyncIterator, Callable
+
+
+@dataclass
+class FileStatus:
+ """Processing status and results for a single file."""
+
+ file: str # File path
+ diagnostics: list[Diagnostic] = field(
+ default_factory=list
+ ) # Found diagnostics
+ skipped: bool = False # File skipped (not a notebook)
+ failed: bool = False # Parsing/processing failed
+ message: str = "" # Status message
+ details: list[str] = field(default_factory=list) # Error details
+ notebook: NotebookSerialization | None = None
+ contents: str | None = None # Store original file contents
+
+
+class Linter:
+ """High-level interface for linting and fixing marimo files.
+
+ Orchestrates file-level processing and delegates notebook linting to RuleEngine.
+ """
+
+ def __init__(
+ self,
+ early_stopping: EarlyStoppingConfig | None = None,
+ pipe: Callable[[str], None] | None = None,
+ fix_files: bool = False,
+ ):
+ self.rule_engine = RuleEngine.create_default(early_stopping)
+ self.pipe = pipe
+ self.fix_files = fix_files
+ self.files: list[FileStatus] = []
+
+ # File processing state
+ self.errored: bool = False
+
+ # Counters for summary
+ self.fixed_count: int = 0
+ self.issues_count: int = 0
+
+ async def _run_stream(
+ self, files_to_check: list[Path]
+ ) -> AsyncIterator[FileStatus]:
+ """Asynchronously check files and yield results as they complete."""
+
+ # Create tasks for all files
+ async def process_file(file: Path) -> FileStatus:
+ file_path = str(file)
+ file_status = FileStatus(file=file_path)
+
+ # Check if file is a supported notebook format
+ if not file_path.endswith((".py", ".md", ".qmd")):
+ file_status.skipped = True
+ file_status.message = (
+ f"Skipped: {file_path} (not a notebook file)"
+ )
+ return file_status
+
+ captured_stdout = io.StringIO()
+ captured_stderr = io.StringIO()
+
+ try:
+ with (
+ contextlib.redirect_stdout(captured_stdout),
+ contextlib.redirect_stderr(captured_stderr),
+ ):
+ load_result = get_notebook_status(file_path)
+ except SyntaxError as e:
+ # Handle syntax errors in notebooks
+ self.errored = True
+ file_status.failed = True
+ file_status.message = f"Failed to parse: {file_path}"
+ file_status.details = [f"SyntaxError: {str(e)}"]
+ return file_status
+ except MarimoFileError as e:
+ # Handle syntax errors in notebooks
+ self.errored = True
+ file_status.failed = True
+ file_status.message = (
+ f"Not recognizable as a marimo notebook: {file_path}"
+ )
+ file_status.details = [f"MarimoFileError: {str(e)}"]
+ return file_status
+
+ file_status.notebook = load_result.notebook
+ file_status.contents = load_result.contents
+
+ if load_result.status == "empty":
+ file_status.skipped = True
+ file_status.message = f"Skipped: {file_path} (empty file)"
+ elif load_result.status == "invalid":
+ file_status.failed = True
+ file_status.message = (
+ f"Failed to parse: {file_path} (not a valid notebook)"
+ )
+ elif load_result.notebook is not None:
+ try:
+ # Check notebook with all rules including parsing
+ file_status.diagnostics = (
+ await self.rule_engine.check_notebook(
+ load_result.notebook,
+ # Add parsing rule if there's captured output
+ stdout=captured_stdout.getvalue().strip(),
+ stderr=captured_stderr.getvalue().strip(),
+ )
+ )
+ except Exception as e:
+ # Handle other parsing errors
+ self.errored = True
+ file_status.failed = True
+ file_status.message = f"Failed to process {file_path}"
+ file_status.details = [str(e)]
+ else:
+ # Status is valid but no notebook - shouldn't happen but handle gracefully
+ file_status.skipped = True
+ file_status.message = (
+ f"Skipped: {file_path} (no notebook content)"
+ )
+
+ # Ensure diagnostics list is initialized for cases where no processing happened
+ if not hasattr(file_status, "diagnostics"):
+ file_status.diagnostics = []
+
+ return file_status
+
+ # Create tasks for all files
+ tasks = [
+ asyncio.create_task(process_file(file_path))
+ for file_path in files_to_check
+ ]
+
+ # Yield results as they complete
+ for task in asyncio.as_completed(tasks):
+ file_status = await task
+ yield file_status
+
+ def _pipe_file_status(self, file_status: FileStatus) -> None:
+ """Send file status through pipe for real-time output."""
+ for diagnostic in file_status.diagnostics:
+ if not (self.fix_files and diagnostic.fixable):
+ self.issues_count += 1
+
+ if not self.pipe:
+ return
+
+ if file_status.skipped:
+ # Don't output skipped files unless they failed
+ return
+ elif file_status.failed:
+ self.pipe(red(file_status.message))
+ for detail in file_status.details:
+ self.pipe(red(f"{detail}"))
+ else:
+ # Show diagnostics immediately as they're found
+ for diagnostic in file_status.diagnostics:
+ self.pipe(diagnostic.format())
+ self.pipe("") # Empty line for spacing
+
+ @staticmethod
+ def _generate_file_contents(file_status: FileStatus) -> str:
+ """Generate file contents from notebook serialization."""
+ if file_status.notebook is None:
+ raise ValueError(
+ "Cannot generate contents for file without notebook"
+ )
+
+ converter = MarimoConvert.from_ir(file_status.notebook)
+
+ if file_status.file.endswith((".md", ".qmd")):
+ return converter.to_markdown(file_status.file)
+ else:
+ return converter.to_py()
+
+ def run_streaming(self, files_to_check: list[Path]) -> None:
+ """Run linting checks with real-time streaming output."""
+ asyncio.run(self._run_streaming_async(files_to_check))
+
+ async def _run_streaming_async(self, files_to_check: list[Path]) -> None:
+ """Internal async implementation of run_streaming."""
+ # Process files as they complete
+ fixed_count = 0
+ async for file_status in self._run_stream(files_to_check):
+ self.files.append(file_status)
+
+ # Stream output via pipe if available
+ if self.pipe:
+ self._pipe_file_status(file_status)
+
+ # Add to fix queue and potentially fix if requested
+ if self.fix_files and not (
+ file_status.skipped
+ or file_status.failed
+ or file_status.notebook is None
+ ):
+ if await self.fix(file_status):
+ fixed_count += 1
+ if self.pipe:
+ self.pipe(f"Updated: {file_status.file}")
+ self.fixed_count = fixed_count
+
+ @staticmethod
+ async def fix(file_status: FileStatus) -> bool:
+ """Fix a single file and write to disk.
+
+ Returns:
+ True if file was modified and written, False otherwise
+ """
+ if file_status.notebook is None or file_status.contents is None:
+ return False
+
+ generated_contents = Linter._generate_file_contents(file_status)
+
+ # Only write if content changed
+ if file_status.contents != generated_contents:
+ await asyncio.to_thread(
+ Path(file_status.file).write_text,
+ generated_contents,
+ encoding="utf-8",
+ )
+ return True
+
+ return False
diff --git a/marimo/_lint/rule_engine.py b/marimo/_lint/rule_engine.py
new file mode 100644
index 0000000000..eb5732b380
--- /dev/null
+++ b/marimo/_lint/rule_engine.py
@@ -0,0 +1,143 @@
+# Copyright 2025 Marimo. All rights reserved.
+from __future__ import annotations
+
+import asyncio
+from typing import TYPE_CHECKING, Optional
+
+from marimo._lint.context import LintContext, RuleContext
+from marimo._lint.diagnostic import Severity
+from marimo._lint.rules import RULE_CODES
+from marimo._schemas.serialization import NotebookSerialization
+
+if TYPE_CHECKING:
+ from collections.abc import AsyncIterator
+
+ from marimo._lint.diagnostic import Diagnostic
+ from marimo._lint.rules.base import LintRule
+
+
+class EarlyStoppingConfig:
+ """Configuration for early stopping behavior."""
+
+ def __init__(
+ self,
+ stop_on_breaking: bool = False,
+ stop_on_runtime: bool = False,
+ max_diagnostics: Optional[int] = None,
+ stop_on_first_of_severity: Optional[Severity] = None,
+ ):
+ self.stop_on_breaking = stop_on_breaking
+ self.stop_on_runtime = stop_on_runtime
+ self.max_diagnostics = max_diagnostics
+ self.stop_on_first_of_severity = stop_on_first_of_severity
+
+ def should_stop(self, diagnostic: Diagnostic, total_count: int) -> bool:
+ """Check if we should stop processing based on this diagnostic."""
+ if self.max_diagnostics and total_count >= self.max_diagnostics:
+ return True
+
+ if (
+ self.stop_on_first_of_severity
+ and diagnostic.severity == self.stop_on_first_of_severity
+ ):
+ return True
+
+ if self.stop_on_breaking and diagnostic.severity == Severity.BREAKING:
+ return True
+
+ if self.stop_on_runtime and diagnostic.severity == Severity.RUNTIME:
+ return True
+
+ return False
+
+
+class RuleEngine:
+ """Orchestrates lint rules and provides checking functionality for a single notebook."""
+
+ def __init__(
+ self,
+ rules: list[LintRule],
+ early_stopping: Optional[EarlyStoppingConfig] = None,
+ ):
+ self.rules = rules
+ self.early_stopping = early_stopping or EarlyStoppingConfig()
+
+ async def check_notebook_streaming(
+ self,
+ notebook: NotebookSerialization,
+ stdout: str = "",
+ stderr: str = "",
+ ) -> AsyncIterator[Diagnostic]:
+ """Check notebook and yield diagnostics as they become available."""
+ ctx = LintContext(notebook, stdout, stderr)
+
+ # Create tasks for all rules with their completion tracking
+ pending_tasks = {
+ asyncio.create_task(rule.check(RuleContext(ctx, rule))): rule
+ for rule in self.rules
+ }
+
+ diagnostic_count = 0
+
+ # Process rules as they complete
+ while pending_tasks:
+ # Wait for at least one task to complete
+ done, pending = await asyncio.wait(
+ pending_tasks.keys(), return_when=asyncio.FIRST_COMPLETED
+ )
+
+ # Update pending tasks
+ for task in done:
+ del pending_tasks[task]
+
+ # Get any new diagnostics and yield them in priority order
+ new_diagnostics = await ctx.get_new_diagnostics()
+ for diagnostic in new_diagnostics:
+ diagnostic_count += 1
+ yield diagnostic
+
+ # Check for early stopping
+ if self.early_stopping.should_stop(
+ diagnostic, diagnostic_count
+ ):
+ # Cancel remaining tasks
+ for task in pending_tasks.keys():
+ task.cancel()
+
+ # Wait for cancellations to complete
+ await asyncio.gather(
+ *pending_tasks.keys(), return_exceptions=True
+ )
+ return
+
+ async def check_notebook(
+ self,
+ notebook: NotebookSerialization,
+ stdout: str = "",
+ stderr: str = "",
+ ) -> list[Diagnostic]:
+ """Check notebook for all lint rule violations using async execution."""
+ diagnostics = []
+ async for diagnostic in self.check_notebook_streaming(
+ notebook, stdout, stderr
+ ):
+ diagnostics.append(diagnostic)
+ return diagnostics
+
+ def check_notebook_sync(
+ self,
+ notebook: NotebookSerialization,
+ stdout: str = "",
+ stderr: str = "",
+ ) -> list[Diagnostic]:
+ """Synchronous wrapper for check_notebook for backward compatibility."""
+ return asyncio.run(self.check_notebook(notebook, stdout, stderr))
+
+ @classmethod
+ def create_default(
+ cls, early_stopping: Optional[EarlyStoppingConfig] = None
+ ) -> RuleEngine:
+ """Create a RuleEngine with all default rules."""
+ # TODO: Filter rules based on user configuration if needed
+ rules = [rule() for rule in RULE_CODES.values()]
+ return cls(rules, early_stopping)
diff --git a/marimo/_lint/rules/__init__.py b/marimo/_lint/rules/__init__.py
new file mode 100644
index 0000000000..e251c6504e
--- /dev/null
+++ b/marimo/_lint/rules/__init__.py
@@ -0,0 +1,14 @@
+# Copyright 2025 Marimo. All rights reserved.
+from marimo._lint.rules.base import LintRule
+from marimo._lint.rules.breaking import BREAKING_RULE_CODES
+from marimo._lint.rules.formatting import FORMATTING_RULE_CODES
+
+RULE_CODES: dict[str, type[LintRule]] = (
+ BREAKING_RULE_CODES | FORMATTING_RULE_CODES
+)
+
+__all__ = [
+ "RULE_CODES",
+ "BREAKING_RULE_CODES",
+ "FORMATTING_RULE_CODES",
+]
diff --git a/marimo/_lint/rules/base.py b/marimo/_lint/rules/base.py
new file mode 100644
index 0000000000..b0b91379bf
--- /dev/null
+++ b/marimo/_lint/rules/base.py
@@ -0,0 +1,26 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from abc import ABC, abstractmethod
+from typing import TYPE_CHECKING
+
+from marimo._lint.diagnostic import Severity
+
+if TYPE_CHECKING:
+ from marimo._lint.context import RuleContext
+
+
+class LintRule(ABC):
+ """Base class for lint rules."""
+
+ # Class attributes that must be set by subclasses
+ code: str
+ name: str
+ description: str
+ severity: Severity
+ fixable: bool
+
+ @abstractmethod
+ async def check(self, ctx: RuleContext) -> None:
+ """Check notebook for violations of this rule using the provided context."""
+ pass
diff --git a/marimo/_lint/rules/breaking/__init__.py b/marimo/_lint/rules/breaking/__init__.py
new file mode 100644
index 0000000000..d4020aef8b
--- /dev/null
+++ b/marimo/_lint/rules/breaking/__init__.py
@@ -0,0 +1,23 @@
+# Copyright 2025 Marimo. All rights reserved.
+from marimo._lint.rules.base import LintRule
+from marimo._lint.rules.breaking.graph import (
+ CycleDependenciesRule,
+ MultipleDefinitionsRule,
+ SetupCellDependenciesRule,
+)
+from marimo._lint.rules.breaking.unparsable import UnparsableRule
+
+BREAKING_RULE_CODES: dict[str, type[LintRule]] = {
+ "MB001": UnparsableRule,
+ "MR002": MultipleDefinitionsRule,
+ "MR003": CycleDependenciesRule,
+ "MR004": SetupCellDependenciesRule,
+}
+
+__all__ = [
+ "MultipleDefinitionsRule",
+ "CycleDependenciesRule",
+ "SetupCellDependenciesRule",
+ "UnparsableRule",
+ "BREAKING_RULE_CODES",
+]
diff --git a/marimo/_lint/rules/breaking/graph.py b/marimo/_lint/rules/breaking/graph.py
new file mode 100644
index 0000000000..14370c04fd
--- /dev/null
+++ b/marimo/_lint/rules/breaking/graph.py
@@ -0,0 +1,335 @@
+# Copyright 2025 Marimo. All rights reserved.
+from __future__ import annotations
+
+from abc import abstractmethod
+from dataclasses import dataclass
+from typing import TYPE_CHECKING
+
+from marimo._ast.parse import ast_parse
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rules.base import LintRule
+from marimo._lint.validate_graph import (
+ check_for_cycles,
+ check_for_invalid_root,
+ check_for_multiple_definitions,
+)
+from marimo._lint.visitors import VariableLineVisitor
+from marimo._types.ids import CellId_t
+from marimo._utils.cell_matching import match_cell_ids_by_similarity
+
+if TYPE_CHECKING:
+ from marimo._lint.context import RuleContext
+ from marimo._runtime.dataflow import DirectedGraph
+ from marimo._schemas.serialization import CellDef
+
+
+@dataclass
+class _ErrorInfo:
+ cell_id: CellId_t
+ line: int
+ column: int
+
+
+class GraphRule(LintRule):
+ """Base class for graph-based lint rules that analyze the dependency graph.
+
+ This class provides the foundation for runtime lint rules that need to analyze
+ the cell dependency graph to detect issues like cycles, multiple definitions,
+ and setup cell violations. These rules ensure marimo's core constraints are
+ maintained for reproducible, executable notebooks.
+
+ The dependency graph represents how cells depend on each other through variable
+ definitions and references. marimo uses this graph to determine execution order
+ and enforce constraints that make notebooks reliable and shareable.
+
+ See Also:
+ - https://docs.marimo.io/guides/understanding_errors/ (Understanding errors)
+ - https://docs.marimo.io/guides/editor_features/understanding_dataflow/ (Dataflow)
+ """
+
+ def _get_cell_from_id(
+ self, cell_id: CellId_t, ctx: RuleContext
+ ) -> CellDef | None:
+ """Get the corresponding CellDef from notebook serialization for a given cell_id."""
+ # For setup cells, use the special setup cell name
+ if cell_id == CellId_t("setup"):
+ for cell in ctx.notebook.cells:
+ if cell.name == CellId_t("setup"):
+ return cell
+ return None
+
+ # Use cell matching to map graph cell IDs to notebook cells
+ graph = ctx.get_graph()
+
+ # Build code mappings for cell matching using position numbers
+ graph_codes = {
+ cid: cell.code
+ for cid, cell in graph.cells.items()
+ if cid != CellId_t("setup")
+ }
+ notebook_codes = {
+ CellId_t(str(i)): cell.code
+ for i, cell in enumerate(ctx.notebook.cells)
+ if cell.name != CellId_t("setup")
+ }
+
+ # Match cell IDs using the existing cell matching system
+ cell_mapping = match_cell_ids_by_similarity(
+ graph_codes, notebook_codes
+ )
+
+ # Find the notebook cell that matches this graph cell_id
+ if cell_id in cell_mapping:
+ notebook_position = cell_mapping[cell_id]
+ # Get the cell at the matched position
+ non_setup_cells = [
+ cell
+ for cell in ctx.notebook.cells
+ if cell.name != CellId_t("setup")
+ ]
+ position = int(notebook_position)
+ if 0 <= position < len(non_setup_cells):
+ return non_setup_cells[position]
+
+ return None
+
+ def _get_variable_line_info(
+ self, cell_id: CellId_t, variable_name: str, ctx: RuleContext
+ ) -> tuple[int, int]:
+ """Get line and column info for a specific variable within a cell."""
+ target_cell = self._get_cell_from_id(cell_id, ctx)
+
+ if target_cell:
+ # Parse the cell code to find the variable definition
+ tree = ast_parse(target_cell.code)
+ visitor = VariableLineVisitor(variable_name)
+ visitor.visit(tree)
+ if visitor.line_number:
+ return (
+ target_cell.lineno + visitor.line_number - 1,
+ target_cell.col_offset + visitor.column_number,
+ )
+ # Fallback to cell line info
+ return target_cell.lineno, target_cell.col_offset + 1
+
+ # Fallback to (0, 0) to indicate unknown line/column
+ return 0, 0
+
+ async def check(self, ctx: RuleContext) -> None:
+ """Perform graph-based validation using the provided context."""
+ # Get the graph from context (cached)
+ graph = ctx.get_graph()
+
+ # Call the specific validation method
+ await self._validate_graph(graph, ctx)
+
+ @abstractmethod
+ async def _validate_graph(
+ self, graph: DirectedGraph, ctx: RuleContext
+ ) -> None:
+ """Abstract method to validate the graph and add diagnostics to context.
+
+ Args:
+ graph: The dependency graph to validate
+ ctx: The lint context to add diagnostics to
+ """
+ pass
+
+
+class MultipleDefinitionsRule(GraphRule):
+ """MR001: Multiple cells define the same variable.
+
+ marimo requires that each variable be defined in only one cell. This constraint
+ ensures that notebooks are reproducible, executable as scripts, and shareable
+ as web apps with better performance than streamlit.
+
+ When a variable is defined in multiple cells, marimo cannot determine which
+ definition to use, leading to unpredictable behavior and hidden bugs.
+
+ See Also:
+ - https://docs.marimo.io/guides/understanding_errors/multiple_definitions/
+ - https://docs.marimo.io/guides/understanding_errors/ (Understanding errors)
+
+ Examples:
+ Violation:
+ Cell 1: x = 1
+ Cell 2: x = 2 # Error: x defined in multiple cells
+
+ Solution:
+ Cell 1: x = 1
+ Cell 2: y = 2 # Use different variable name
+ """
+
+ code = "MR001"
+ name = "multiple-definitions"
+ description = "Multiple cells define the same variable"
+ severity = Severity.RUNTIME
+ fixable = False
+
+ async def _validate_graph(
+ self, graph: DirectedGraph, ctx: RuleContext
+ ) -> None:
+ """Validate the graph for multiple definitions."""
+ validation_errors = check_for_multiple_definitions(graph)
+
+ names: dict[str, list[_ErrorInfo]] = {}
+ for cell_id, error_list in validation_errors.items():
+ for error in error_list:
+ # Get specific line info for the variable definition
+ line, column = self._get_variable_line_info(
+ cell_id, error.name, ctx
+ )
+ names.setdefault(error.name, []).append(
+ _ErrorInfo(cell_id=cell_id, line=line, column=column)
+ )
+
+ for name in names:
+ lines = [info.line for info in names[name]]
+ columns = [info.column for info in names[name]]
+ cell_ids = [info.cell_id for info in names[name]]
+
+ diagnostic = Diagnostic(
+ message=f"Variable '{name}' is defined in multiple cells",
+ cell_id=cell_ids,
+ line=lines,
+ column=columns,
+ code=self.code,
+ name=self.name,
+ severity=self.severity,
+ fixable=self.fixable,
+ fix=(
+ "Variables must be unique across cells. Alternatively, "
+ f"they can be private with an underscore prefix (i.e. `_{name}`.)"
+ ),
+ )
+
+ await ctx.add_diagnostic(diagnostic)
+
+
+class CycleDependenciesRule(GraphRule):
+ """MR002: Cells have circular dependencies.
+
+ marimo prevents circular dependencies between cells to ensure a well-defined
+ execution order. If cell A declares variable 'a' and reads variable 'b', then
+ cell B cannot declare 'b' and read 'a' without creating a cycle.
+
+ Cycles make notebooks non-reproducible and prevent marimo from determining
+ the correct execution order, leading to undefined behavior.
+
+ See Also:
+ - https://docs.marimo.io/guides/understanding_errors/cycles/
+ - https://docs.marimo.io/guides/understanding_errors/ (Understanding errors)
+
+ Examples:
+ Violation:
+ Cell 1: a = b + 1 # Reads b
+ Cell 2: b = a + 1 # Reads a -> Cycle!
+
+ Solution:
+ Cell 1: a = 1
+ Cell 2: b = a + 1 # Unidirectional dependency
+ """
+
+ code = "MR002"
+ name = "cycle-dependencies"
+ description = "Cells have circular dependencies"
+ severity = Severity.RUNTIME
+ fixable = False
+
+ async def _validate_graph(
+ self, graph: DirectedGraph, ctx: RuleContext
+ ) -> None:
+ """Validate the graph for circular dependencies."""
+ validation_errors = check_for_cycles(graph)
+
+ seen = set()
+ for cell_id, error_list in validation_errors.items():
+ for error in error_list:
+ if error.edges_with_vars in seen:
+ continue
+ seen.add(error.edges_with_vars)
+
+ cells = []
+ lines = []
+ columns = []
+ for cell_id, variables, _ in error.edges_with_vars:
+ # Get cell from notebook serialization
+ for v in variables:
+ line, column = self._get_variable_line_info(
+ cell_id, v, ctx
+ )
+ cells.append(cell_id)
+ lines.append(line)
+ columns.append(column)
+
+ diagnostic = Diagnostic(
+ message="Cell is part of a circular dependency",
+ cell_id=cells,
+ line=lines,
+ column=columns,
+ code=self.code,
+ name=self.name,
+ severity=self.severity,
+ fixable=self.fixable,
+ )
+
+ await ctx.add_diagnostic(diagnostic)
+
+
+class SetupCellDependenciesRule(GraphRule):
+ """MR003: Setup cell cannot have dependencies.
+
+ The setup cell in marimo is special - it runs first and can define variables
+ that are available to all other cells. However, the setup cell itself cannot
+ depend on variables defined in other cells, as this would create a dependency
+ cycle and violate marimo's execution model.
+
+ The setup cell is designed for imports, configuration, and other initialization
+ code that should run before any other cells execute.
+
+ See Also:
+ - https://docs.marimo.io/guides/understanding_errors/setup/
+ - https://docs.marimo.io/guides/understanding_errors/ (Understanding errors)
+
+ Examples:
+ Violation:
+ Setup cell: y = x + 1 # Error: setup depends on other cells
+ Cell 1: x = 1
+
+ Solution:
+ Setup cell: y = 1 # Setup defines its own variables
+ Cell 1: x = y + 1 # Other cells can use setup variables
+ """
+
+ code = "MR003"
+ name = "setup-cell-dependencies"
+ description = "Setup cell cannot have dependencies"
+ severity = Severity.RUNTIME
+ fixable = False
+
+ async def _validate_graph(
+ self, graph: DirectedGraph, ctx: RuleContext
+ ) -> None:
+ """Validate the graph for setup cell dependency violations."""
+ validation_errors = check_for_invalid_root(graph)
+
+ for cell_id, error_list in validation_errors.items():
+ for _ in error_list:
+ # Get cell from notebook serialization
+ cell = self._get_cell_from_id(cell_id, ctx)
+ line, column = (
+ (cell.lineno, cell.col_offset + 1) if cell else (0, 0)
+ )
+
+ diagnostic = Diagnostic(
+ message="Setup cell cannot have dependencies",
+ cell_id=[cell_id],
+ line=line,
+ column=column,
+ code=self.code,
+ name=self.name,
+ severity=self.severity,
+ fixable=self.fixable,
+ )
+
+ await ctx.add_diagnostic(diagnostic)
diff --git a/marimo/_lint/rules/breaking/unparsable.py b/marimo/_lint/rules/breaking/unparsable.py
new file mode 100644
index 0000000000..f11c8f2926
--- /dev/null
+++ b/marimo/_lint/rules/breaking/unparsable.py
@@ -0,0 +1,38 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rules.base import LintRule
+from marimo._schemas.serialization import UnparsableCell
+
+if TYPE_CHECKING:
+ from marimo._lint.context import RuleContext
+
+
+class UnparsableRule(LintRule):
+ """MB001: Cell contains unparsable code."""
+
+ code = "MB001"
+ name = "unparsable-cells"
+ description = "Cell contains unparsable code"
+ severity = Severity.BREAKING
+ fixable = False
+
+ async def check(self, ctx: RuleContext) -> None:
+ """Check for unparsable cells."""
+ for cell in ctx.notebook.cells:
+ if isinstance(cell, UnparsableCell): # Unparsable cell
+ # Try to find the line number of the error
+ line_num = cell.lineno
+ col_num = cell.col_offset
+
+ diagnostic = Diagnostic(
+ message="Notebook contains unparsable code",
+ cell_id=None,
+ line=line_num,
+ column=col_num,
+ )
+
+ await ctx.add_diagnostic(diagnostic)
diff --git a/marimo/_lint/rules/formatting/__init__.py b/marimo/_lint/rules/formatting/__init__.py
new file mode 100644
index 0000000000..c5cb2477b0
--- /dev/null
+++ b/marimo/_lint/rules/formatting/__init__.py
@@ -0,0 +1,17 @@
+# Copyright 2025 Marimo. All rights reserved.
+from marimo._lint.rules.base import LintRule
+from marimo._lint.rules.formatting.general import GeneralFormattingRule
+from marimo._lint.rules.formatting.parsing import StderrRule, StdoutRule
+
+FORMATTING_RULE_CODES: dict[str, type[LintRule]] = {
+ "MF001": GeneralFormattingRule,
+ "MF002": StdoutRule,
+ "MF003": StderrRule,
+}
+
+__all__ = [
+ "GeneralFormattingRule",
+ "FORMATTING_RULE_CODES",
+ "StdoutRule",
+ "StderrRule",
+]
diff --git a/marimo/_lint/rules/formatting/general.py b/marimo/_lint/rules/formatting/general.py
new file mode 100644
index 0000000000..86137b6614
--- /dev/null
+++ b/marimo/_lint/rules/formatting/general.py
@@ -0,0 +1,51 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rules.base import LintRule
+
+if TYPE_CHECKING:
+ from marimo._lint.context import RuleContext
+
+
+class GeneralFormattingRule(LintRule):
+ """MF001: General formatting issues."""
+
+ code = "MF001"
+ name = "general-formatting"
+ description = "General formatting issues with the notebook format."
+ severity = Severity.FORMATTING
+ fixable = True
+
+ async def check(self, ctx: RuleContext) -> None:
+ """Check for general formatting issues by extracting violations from serialization."""
+ # Import the violation constants to check for specific types
+ from marimo._ast.parse import (
+ EXPECTED_GENERATED_WITH_VIOLATION,
+ UNEXPECTED_STATEMENT_APP_INIT_VIOLATION,
+ UNEXPECTED_STATEMENT_CELL_DEF_VIOLATION,
+ UNEXPECTED_STATEMENT_MARIMO_IMPORT_VIOLATION,
+ )
+
+ # Extract violations from the notebook serialization
+ for violation in ctx.notebook.violations:
+ # Determine if this violation is fixable
+ is_fixable = violation.description in [
+ UNEXPECTED_STATEMENT_CELL_DEF_VIOLATION,
+ UNEXPECTED_STATEMENT_MARIMO_IMPORT_VIOLATION,
+ EXPECTED_GENERATED_WITH_VIOLATION,
+ UNEXPECTED_STATEMENT_APP_INIT_VIOLATION,
+ ]
+
+ # Create diagnostic and add to context
+ diagnostic = Diagnostic(
+ message=violation.description,
+ cell_id=[], # Violations don't have cell_id
+ line=violation.lineno - 1, # Convert 1-based to 0-based
+ column=violation.col_offset + 1,
+ fixable=is_fixable,
+ )
+
+ await ctx.add_diagnostic(diagnostic)
diff --git a/marimo/_lint/rules/formatting/parsing.py b/marimo/_lint/rules/formatting/parsing.py
new file mode 100644
index 0000000000..e7b2408a1f
--- /dev/null
+++ b/marimo/_lint/rules/formatting/parsing.py
@@ -0,0 +1,98 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+import re
+from typing import TYPE_CHECKING
+
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rules.base import LintRule
+
+if TYPE_CHECKING:
+ from marimo._lint.context import RuleContext
+
+
+class StdoutRule(LintRule):
+ """MF002: Parse stdout captured during notebook loading.
+
+ When marimo parses a notebook file, any output to stdout.
+ """
+
+ code = "MF002"
+ name = "parse-stdout"
+ description = "Parse captured stdout during notebook loading"
+ severity = Severity.FORMATTING
+ fixable = False
+
+ async def check(self, ctx: RuleContext) -> None:
+ """Process captured stdout and create diagnostics."""
+ # Pattern to match file:line format (e.g., "file.py:68: SyntaxWarning")
+ line_pattern = re.compile(r"([^:]+):(\d+):\s*(.+)")
+
+ # Split stderr by lines to handle multiple warnings
+ lines = ctx.stdout.strip().split("\n")
+ captured = False
+
+ for line in lines:
+ line = line.strip()
+ if not line:
+ continue
+
+ # Check if this line contains a file:line reference
+ match = line_pattern.match(line)
+ if match:
+ captured = True
+ _, line_num_str, warning_msg = match.groups()
+ await ctx.add_diagnostic(
+ Diagnostic(
+ message=warning_msg,
+ line=int(line_num_str) - 1, # Convert to 0-based index
+ cell_id=None,
+ column=0,
+ )
+ )
+
+ # Fallback: if no line patterns found, create single diagnostic
+ if not captured and ctx.stdout:
+ await ctx.add_diagnostic(
+ Diagnostic(
+ message=f"Parsing warning: {ctx.stderr}",
+ cell_id=None,
+ line=0,
+ column=0,
+ )
+ )
+
+
+class StderrRule(LintRule):
+ """MF003: Parse stderr captured during notebook loading.
+
+ When marimo parses a notebook file, any output to stderr (such as syntax
+ warnings) is captured and converted to formatting diagnostics. This rule
+ processes that captured output and extracts useful information like line
+ numbers.
+
+ Examples:
+ Stderr: "file.py:68: SyntaxWarning: invalid escape sequence '\\l'"
+ -> Creates diagnostic pointing to line 68
+
+ Stdout: General parsing information
+ -> Creates diagnostic at line 1
+ """
+
+ code = "MF003"
+ name = "parse-stderr"
+ description = "Parse captured stderr during notebook loading"
+ severity = Severity.FORMATTING
+ fixable = False
+
+ async def check(self, ctx: RuleContext) -> None:
+ # Process stderr content
+ if ctx.stderr:
+ await ctx.add_diagnostic(
+ Diagnostic(
+ message=f"stderr: {ctx.stderr}",
+ cell_id=None,
+ line=0,
+ column=0,
+ )
+ )
diff --git a/marimo/_runtime/validate_graph.py b/marimo/_lint/validate_graph.py
similarity index 84%
rename from marimo/_runtime/validate_graph.py
rename to marimo/_lint/validate_graph.py
index 1c4bf46576..992526ea77 100644
--- a/marimo/_runtime/validate_graph.py
+++ b/marimo/_lint/validate_graph.py
@@ -15,6 +15,37 @@ from marimo._runtime.dataflow import DirectedGraph
from marimo._types.ids import CellId_t
+def check_for_errors(
+ graph: DirectedGraph,
+) -> dict[CellId_t, tuple[Error, ...]]:
+ """
+ Check graph for violations of marimo semantics.
+
+ Return a dict of errors in the graph, with an entry for each cell
+ that is involved in an error.
+ """
+ multiple_definition_errors = check_for_multiple_definitions(graph)
+ cycle_errors = check_for_cycles(graph)
+ invalid_root_errors = check_for_invalid_root(graph)
+
+ errors: dict[CellId_t, tuple[Error, ...]] = {}
+ for cid in set(
+ itertools.chain(
+ multiple_definition_errors.keys(),
+ cycle_errors.keys(),
+ invalid_root_errors.keys(),
+ )
+ ):
+ errors[cid] = tuple(
+ itertools.chain(
+ multiple_definition_errors[cid],
+ cycle_errors[cid],
+ invalid_root_errors[cid],
+ )
+ )
+ return errors
+
+
def check_for_multiple_definitions(
graph: DirectedGraph,
) -> dict[CellId_t, list[MultipleDefinitionError]]:
@@ -53,8 +84,10 @@ def check_for_invalid_root(
)
for ancestor in ancestors
if (
- deps := sorted(
- graph.cells[ancestor].defs & graph.cells[setup_id].refs
+ deps := tuple(
+ sorted(
+ graph.cells[ancestor].defs & graph.cells[setup_id].refs
+ )
)
)
)
@@ -77,15 +110,17 @@ def check_for_cycles(graph: DirectedGraph) -> dict[CellId_t, list[CycleError]]:
cycle_with_vars = tuple(
(
edge[0],
- sorted(
- set(
- list(
- graph.cells[edge[0]].defs
- & graph.cells[edge[1]].refs
- )
- + list(
- graph.cells[edge[0]].refs
- & graph.cells[edge[1]].deleted_refs
+ tuple(
+ sorted(
+ set(
+ list(
+ graph.cells[edge[0]].defs
+ & graph.cells[edge[1]].refs
+ )
+ + list(
+ graph.cells[edge[0]].refs
+ & graph.cells[edge[1]].deleted_refs
+ )
)
)
),
@@ -96,34 +131,3 @@ def check_for_cycles(graph: DirectedGraph) -> dict[CellId_t, list[CycleError]]:
for cid in nodes_in_cycle:
errors[cid].append(CycleError(edges_with_vars=cycle_with_vars))
return errors
-
-
-def check_for_errors(
- graph: DirectedGraph,
-) -> dict[CellId_t, tuple[Error, ...]]:
- """
- Check graph for violations of marimo semantics.
-
- Return a dict of errors in the graph, with an entry for each cell
- that is involved in an error.
- """
- multiple_definition_errors = check_for_multiple_definitions(graph)
- cycle_errors = check_for_cycles(graph)
- invalid_root_errors = check_for_invalid_root(graph)
-
- errors: dict[CellId_t, tuple[Error, ...]] = {}
- for cid in set(
- itertools.chain(
- multiple_definition_errors.keys(),
- cycle_errors.keys(),
- invalid_root_errors.keys(),
- )
- ):
- errors[cid] = tuple(
- itertools.chain(
- multiple_definition_errors[cid],
- cycle_errors[cid],
- invalid_root_errors[cid],
- )
- )
- return errors
diff --git a/marimo/_lint/visitors.py b/marimo/_lint/visitors.py
new file mode 100644
index 0000000000..4ae59440a4
--- /dev/null
+++ b/marimo/_lint/visitors.py
@@ -0,0 +1,40 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""AST visitors for linting purposes."""
+
+import ast
+from typing import Optional
+
+
+class VariableLineVisitor(ast.NodeVisitor):
+ """AST visitor to find the line number of a variable definition."""
+
+ def __init__(self, target_variable: str):
+ self.target_variable = target_variable
+ self.line_number: Optional[int] = None
+ self.column_number: int = 1
+
+ def visit_Name(self, node: ast.Name) -> None:
+ """Visit Name nodes to find variable definitions."""
+ if node.id == self.target_variable:
+ # Check if this is a definition (not just a reference)
+ if isinstance(node.ctx, (ast.Store, ast.Del)):
+ self.line_number = node.lineno
+ self.column_number = node.col_offset + 1
+ return
+ self.generic_visit(node)
+
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
+ """Visit function definition nodes."""
+ if node.name == self.target_variable:
+ self.line_number = node.lineno
+ self.column_number = node.col_offset + 1
+ return
+ self.generic_visit(node)
+
+ def visit_ClassDef(self, node: ast.ClassDef) -> None:
+ """Visit class definition nodes."""
+ if node.name == self.target_variable:
+ self.line_number = node.lineno
+ self.column_number = node.col_offset + 1
+ return
+ self.generic_visit(node)
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.py
index e648081d24..bd80e06979 100644
--- a/marimo/_runtime/dataflow.py
+++ b/marimo/_runtime/dataflow.py
@@ -28,7 +28,7 @@ Edge = tuple[CellId_t, CellId_t]
# The first entry is the source node; the second entry is a list of defs from
# the source read by the destination; and the third entry is the destination
# node.
-EdgeWithVar = tuple[CellId_t, list[str], CellId_t]
+EdgeWithVar = tuple[CellId_t, tuple[str, ...], CellId_t]
LOGGER = _loggers.marimo_logger()
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 86ad568d0e..94de643132 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -42,6 +42,7 @@ from marimo._data.preview_column import (
from marimo._dependencies.dependencies import DependencyManager
from marimo._dependencies.errors import ManyModulesNotFoundError
from marimo._entrypoints.registry import EntryPointRegistry
+from marimo._lint.validate_graph import check_for_errors
from marimo._messaging.cell_output import CellChannel
from marimo._messaging.context import http_request_context, run_id_context
from marimo._messaging.errors import (
@@ -168,7 +169,6 @@ from marimo._runtime.state import State
from marimo._runtime.utils.set_ui_element_request_manager import (
SetUIElementRequestManager,
)
-from marimo._runtime.validate_graph import check_for_errors
from marimo._runtime.win32_interrupt_handler import Win32InterruptHandler
from marimo._secrets.load_dotenv import (
load_dotenv_with_fallback,
diff --git a/marimo/_schemas/serialization.py b/marimo/_schemas/serialization.py
index 8fd03a94d4..eb117747e6 100644
--- a/marimo/_schemas/serialization.py
+++ b/marimo/_schemas/serialization.py
@@ -122,6 +122,7 @@ class NotebookSerializationV1:
cells: list[CellDef] = field(default_factory=list)
violations: list[Violation] = field(default_factory=list)
valid: bool = True
+ filename: Optional[str] = None
NotebookSerialization: TypeAlias = NotebookSerializationV1
diff --git a/marimo/_utils/files.py b/marimo/_utils/files.py
index dad6527705..2ad5a1394f 100644
--- a/marimo/_utils/files.py
+++ b/marimo/_utils/files.py
@@ -1,5 +1,9 @@
# Copyright 2025 Marimo. All rights reserved.
+import fnmatch
+import os
import re
+from collections.abc import Generator
+from pathlib import Path
from typing import Union
@@ -11,3 +15,58 @@ def natural_sort(filename: str) -> list[Union[int, str]]:
return [convert(c) for c in re.split("([0-9]+)", key)]
return alphanum_key(filename)
+
+
+def get_files(folder: str) -> Generator[Path, None, None]:
+ """Recursively get all files from a folder."""
+ with os.scandir(folder) as scan:
+ for item in scan:
+ if item.is_file():
+ yield Path(item.path)
+ elif item.is_dir() and not item.name.startswith("."):
+ yield from get_files(item.path)
+
+
+def expand_file_patterns(file_patterns: tuple[str, ...]) -> list[Path]:
+ """Expand file patterns to actual file paths.
+
+ Args:
+ file_patterns: Tuple of file patterns (files, directories, or glob-like patterns)
+
+ Returns:
+ List of Path objects for all matching files
+ """
+ files_to_check = []
+ for pattern in file_patterns:
+ if os.path.isfile(pattern):
+ files_to_check.append(Path(pattern))
+ elif os.path.isdir(pattern):
+ files_to_check.extend(get_files(pattern))
+ else:
+ # Handle glob patterns by walking from root and filtering
+ if "**" in pattern or "*" in pattern or "?" in pattern:
+ # Extract root directory to walk from
+ root = "."
+ parts = pattern.split("/")
+ for i, part in enumerate(parts):
+ if "*" in part or "?" in part:
+ root = "/".join(parts[:i]) if i > 0 else "."
+ break
+ elif os.path.isdir("/".join(parts[: i + 1])):
+ root = "/".join(parts[: i + 1])
+
+ # Get all files from root and filter by pattern
+ if os.path.isdir(root):
+ all_files = get_files(root)
+ matched_files = [
+ path
+ for path in all_files
+ if fnmatch.fnmatch(str(path), pattern)
+ ]
+ files_to_check.extend(matched_files)
+ else:
+ # Not a glob pattern but file doesn't exist, skip
+ pass
+
+ # Remove duplicates and sort
+ return sorted(set(files_to_check))
diff --git a/tests/_ast/test_load.py b/tests/_ast/test_load.py
index a76f035bfe..3443b210b9 100644
--- a/tests/_ast/test_load.py
+++ b/tests/_ast/test_load.py
@@ -308,16 +308,23 @@ class TestGetCodes:
class TestGetStatus:
@staticmethod
def test_get_status_valid() -> None:
- assert load.get_notebook_status(get_filepath("test_main")) == "valid"
+ assert (
+ load.get_notebook_status(get_filepath("test_main")).status
+ == "valid"
+ )
@staticmethod
def test_get_status_empty() -> None:
- assert load.get_notebook_status(get_filepath("test_empty")) == "empty"
+ assert (
+ load.get_notebook_status(get_filepath("test_empty")).status
+ == "empty"
+ )
@staticmethod
def test_get_status_invalid() -> None:
assert (
- load.get_notebook_status(get_filepath("test_invalid")) == "invalid"
+ load.get_notebook_status(get_filepath("test_invalid")).status
+ == "invalid"
)
@staticmethod
@@ -325,6 +332,6 @@ class TestGetStatus:
assert (
load.get_notebook_status(
get_filepath("test_get_codes_messy_toplevel")
- )
+ ).status
== "has_errors"
)
diff --git a/tests/_cli/test_cli_check.py b/tests/_cli/test_cli_check.py
new file mode 100644
index 0000000000..9fbce28c26
--- /dev/null
+++ b/tests/_cli/test_cli_check.py
@@ -0,0 +1,176 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""CLI tests for the marimo check command."""
+
+import tempfile
+
+from click.testing import CliRunner
+
+from marimo._cli.cli import check
+
+
+class TestLintCLI:
+ """Test the check CLI command."""
+
+ def test_check_command_basic(self):
+ """Test basic check command functionality."""
+ runner = CliRunner()
+
+ # Create a temporary file with basic marimo code
+ with tempfile.NamedTemporaryFile(
+ mode="w", suffix=".py", delete=False
+ ) as f:
+ f.write("""
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+def _():
+ x = 1
+ return (x,)
+
+if __name__ == "__main__":
+ app.run()
+""")
+ f.flush()
+
+ # Run check command
+ result = runner.invoke(check, [f.name])
+
+ # Should succeed and show some output
+ assert result.exit_code == 0, result.output
+ assert not result.output.strip()
+
+ def test_check_command_with_violations(self):
+ """Test check command with parsing violations."""
+ runner = CliRunner()
+
+ # Create a temporary file with violations
+ with tempfile.NamedTemporaryFile(
+ mode="w", suffix=".py", delete=False
+ ) as f:
+ f.write("""
+import marimo
+
+app = marimo.App()
+
+def _():
+ y = 2
+ return (y,)
+""")
+ f.flush()
+
+ # Run check command
+ result = runner.invoke(check, [f.name, "--strict"])
+
+ # Should give and show errors
+ assert result.exit_code == 1, result.output
+ assert "warning[general-formatting]" in result.output
+
+ def test_check_command_with_fix(self):
+ """Test check command with fix option."""
+ runner = CliRunner()
+
+ # Create a temporary file with violations
+ with tempfile.NamedTemporaryFile(
+ mode="w", suffix=".py", delete=False
+ ) as f:
+ f.write("""
+import marimo
+
+app = marimo.App()
+
+def _():
+ y = 2
+ return (y,)
+
+# This should create a violation with missing guard
+""")
+ f.flush()
+
+ # Run check command with fix
+ result = runner.invoke(check, [f.name, "--fix"])
+
+ # The fix might fail due to file permissions or other issues
+ # Just check that the command runs
+ assert result.exit_code == 0, result.output
+
+ def test_check_command_nonexistent_file(self):
+ """Test check command with nonexistent file."""
+ runner = CliRunner()
+
+ result = runner.invoke(check, ["nonexistent.py"])
+
+ # The CLI might handle nonexistent files gracefully
+ # Just check that it doesn't crash
+ assert result.exit_code in [0, 1, 2] # Various possible exit codes
+
+ def test_check_command_syntax_error(self):
+ """Test check command with syntax error."""
+ runner = CliRunner()
+
+ # Create a temporary file with syntax error
+ with tempfile.NamedTemporaryFile(
+ mode="w", suffix=".py", delete=False
+ ) as f:
+ f.write("""
+import marimo
+
+app = marimo.App()
+
+def _():
+ x = 1 + # Syntax error
+ return (x,)
+""")
+ f.flush()
+
+ # Run check command
+ result = runner.invoke(check, [f.name])
+
+ # Should fail due to syntax error
+ assert result.exit_code != 0, result.output
+
+ def test_check_command_help(self):
+ """Test check command help."""
+ runner = CliRunner()
+
+ result = runner.invoke(check, ["--help"])
+
+ # Should succeed and show help
+ assert result.exit_code == 0
+ assert "format" in result.output
+ assert "--fix" in result.output
+
+ def test_check_command_no_errors(self):
+ """Test check command with valid notebook."""
+ runner = CliRunner()
+
+ # Create a temporary file with valid marimo code
+ with tempfile.NamedTemporaryFile(
+ mode="w", suffix=".py", delete=False
+ ) as f:
+ f.write("""
+import marimo
+
+__generated_with = "0.1.0"
+app = marimo.App()
+
+def _():
+ x = 1
+ return (x,)
+
+if __name__ == "__main__":
+ app.run()
+""")
+ f.flush()
+
+ # Run check command
+ result = runner.invoke(check, [f.name])
+
+ # Should succeed
+ assert result.exit_code == 0, result.output
diff --git a/tests/_lint/snapshots/cycle_dependencies_errors.txt b/tests/_lint/snapshots/cycle_dependencies_errors.txt
new file mode 100644
index 0000000000..72fc6ed70f
--- /dev/null
+++ b/tests/_lint/snapshots/cycle_dependencies_errors.txt
@@ -0,0 +1,16 @@
+error[cycle-dependencies]: Cell is part of a circular dependency
+ --> tests/_lint/test_files/cycle_dependencies.py:8:1
+ 8 | def _(z):
+ 9 | x = 1 + z # This should trigger MR002 - cycle dependency
+ | ^
+ 10 | return (x,)
+ ...
+ 14 | def _(x):
+ 15 | y = x + 1
+ | ^
+ 16 | return (y,)
+ ...
+ 20 | def _(y):
+ 21 | z = y + 1
+ | ^
+ 22 | return (z,)
\ No newline at end of file
diff --git a/tests/_lint/snapshots/formatting.txt b/tests/_lint/snapshots/formatting.txt
new file mode 100644
index 0000000000..df51a1d962
--- /dev/null
+++ b/tests/_lint/snapshots/formatting.txt
@@ -0,0 +1,27 @@
+warning[general-formatting]: `marimo` is typically not imported with an alias.
+ --> tests/_lint/test_files/formatting.py:2:1
+ 2 |
+ 3 | import marimo as mo # intentional to test import aliasing
+ | ^
+ 4 |
+warning[general-formatting]: Expected `__generated_with` assignment for marimo version number.
+ --> tests/_lint/test_files/formatting.py:4:1
+ 4 |
+ 5 | statement = "not in a cell, so unexpected"
+ | ^
+ 6 |
+warning[general-formatting]: Unexpected statement, expected App initialization.
+ --> tests/_lint/test_files/formatting.py:4:1
+ 4 |
+ 5 | statement = "not in a cell, so unexpected"
+ | ^
+ 6 |
+warning[general-formatting]: Unexpected statement, expected cell definitions.
+ --> tests/_lint/test_files/formatting.py:9:1
+ 9 |
+ 10 | statement = "not in a cell, so unexpected"
+ | ^
+ 11 |
+warning[general-formatting]: Expected run guard statement
+ --> tests/_lint/test_files/formatting.py
+ 1 | """Example to test formatting issues."""
\ No newline at end of file
diff --git a/tests/_lint/snapshots/multiple_definitions_errors.txt b/tests/_lint/snapshots/multiple_definitions_errors.txt
new file mode 100644
index 0000000000..c427be3149
--- /dev/null
+++ b/tests/_lint/snapshots/multiple_definitions_errors.txt
@@ -0,0 +1,12 @@
+error[multiple-definitions]: Variable 'x' is defined in multiple cells
+ --> tests/_lint/test_files/multiple_definitions.py:9:1
+ 9 | print(1)
+ 10 | x = 1
+ | ^
+ 11 | return
+ ...
+ 15 | def _():
+ 16 | x = 2 # This should trigger MR001 - multiple definitions
+ | ^
+ 17 | return
+info: Variables must be unique across cells. Alternatively, they can be private with an underscore prefix (i.e. `_x`.)
\ No newline at end of file
diff --git a/tests/_lint/snapshots/setup_dependencies_errors.txt b/tests/_lint/snapshots/setup_dependencies_errors.txt
new file mode 100644
index 0000000000..6f77fa416b
--- /dev/null
+++ b/tests/_lint/snapshots/setup_dependencies_errors.txt
@@ -0,0 +1,12 @@
+error[setup-cell-dependencies]: Setup cell cannot have dependencies
+ --> tests/_lint/test_files/setup_dependencies.py:8:1
+ 8 | with app.setup:
+ 9 | y = x + 1 # This should trigger MR003 - setup cell dependencies
+ | ^
+ 10 |
+warning[general-formatting]: Unexpected statement, expected cell definitions.
+ --> tests/_lint/test_files/setup_dependencies.py:5:1
+ 5 |
+ 6 | x = 1
+ | ^
+ 7 |
\ No newline at end of file
diff --git a/tests/_lint/snapshots/unparsable_cell_errors.txt b/tests/_lint/snapshots/unparsable_cell_errors.txt
new file mode 100644
index 0000000000..db5ac4479e
--- /dev/null
+++ b/tests/_lint/snapshots/unparsable_cell_errors.txt
@@ -0,0 +1,6 @@
+critical[unparsable-cells]: Notebook contains unparsable code
+ --> tests/_lint/test_files/unparsable_cell.py:14:0
+ 14 | app._unparsable_cell("""
+ 15 | x = 1 + # Syntax error
+ | ^
+ 16 | """)
\ No newline at end of file
diff --git a/tests/_lint/test_async_context_system.py b/tests/_lint/test_async_context_system.py
new file mode 100644
index 0000000000..1a6279a916
--- /dev/null
+++ b/tests/_lint/test_async_context_system.py
@@ -0,0 +1,371 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""Unit tests for the async context-based lint system."""
+
+from marimo._ast.parse import parse_notebook
+from marimo._lint.context import LintContext, RuleContext
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rule_engine import RuleEngine
+from marimo._lint.rules.base import LintRule
+from marimo._lint.rules.breaking import MultipleDefinitionsRule, UnparsableRule
+from marimo._lint.rules.formatting import GeneralFormattingRule
+from tests._lint.utils import lint_notebook
+
+
+class MockRule(LintRule):
+ """Mock rule for testing."""
+
+ def __init__(
+ self, code: str, severity: Severity, diagnostic_count: int = 1
+ ):
+ # Set class attributes dynamically
+ self.code = code
+ self.name = f"mock-{code.lower()}"
+ self.description = f"Mock rule {code}"
+ self.severity = severity
+ self.fixable = False
+
+ # Instance attributes
+ self.diagnostic_count = diagnostic_count
+ self.call_count = 0
+
+ async def check(self, rule_ctx: RuleContext) -> None:
+ """Add mock diagnostics to context."""
+ self.call_count += 1
+ for i in range(self.diagnostic_count):
+ diagnostic = Diagnostic(
+ message=f"Mock diagnostic {i + 1}",
+ cell_id=None,
+ line=1,
+ column=1,
+ code=self.code,
+ name=self.name,
+ severity=self.severity,
+ fixable=self.fixable,
+ )
+ await rule_ctx.add_diagnostic(diagnostic)
+
+
+class TestLintContext:
+ """Test the LintContext class."""
+
+ def setup_method(self):
+ self.notebook = parse_notebook("import marimo\napp = marimo.App()")
+ self.ctx = LintContext(self.notebook)
+
+ async def test_add_diagnostic_priority_queue(self):
+ """Test that diagnostics are queued by priority."""
+ # Add diagnostics in reverse priority order
+ formatting_diag = Diagnostic(
+ message="formatting",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF001",
+ name="test",
+ severity=Severity.FORMATTING,
+ fixable=False,
+ )
+ breaking_diag = Diagnostic(
+ message="breaking",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MB001",
+ name="test",
+ severity=Severity.BREAKING,
+ fixable=False,
+ )
+ runtime_diag = Diagnostic(
+ message="runtime",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MR001",
+ name="test",
+ severity=Severity.RUNTIME,
+ fixable=False,
+ )
+
+ # Add in non-priority order
+ await self.ctx.add_diagnostic(formatting_diag)
+ await self.ctx.add_diagnostic(runtime_diag)
+ await self.ctx.add_diagnostic(breaking_diag)
+
+ # Get diagnostics - should be sorted by priority
+ diagnostics = await self.ctx.get_diagnostics()
+
+ assert len(diagnostics) == 3
+ assert diagnostics[0].severity == Severity.BREAKING
+ assert diagnostics[1].severity == Severity.RUNTIME
+ assert diagnostics[2].severity == Severity.FORMATTING
+
+ async def test_add_diagnostic_stable_order(self):
+ """Test that diagnostics with same priority maintain insertion order."""
+ # Add multiple diagnostics with same priority
+ diag1 = Diagnostic(
+ message="first",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF001",
+ name="test1",
+ severity=Severity.FORMATTING,
+ fixable=False,
+ )
+ diag2 = Diagnostic(
+ message="second",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF002",
+ name="test2",
+ severity=Severity.FORMATTING,
+ fixable=False,
+ )
+ diag3 = Diagnostic(
+ message="third",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF003",
+ name="test3",
+ severity=Severity.FORMATTING,
+ fixable=False,
+ )
+
+ await self.ctx.add_diagnostic(diag1)
+ await self.ctx.add_diagnostic(diag2)
+ await self.ctx.add_diagnostic(diag3)
+
+ diagnostics = await self.ctx.get_diagnostics()
+
+ assert len(diagnostics) == 3
+ assert diagnostics[0].message == "first"
+ assert diagnostics[1].message == "second"
+ assert diagnostics[2].message == "third"
+
+ def test_graph_caching(self):
+ """Test that the graph is cached and reused."""
+ # First call should construct the graph
+ graph1 = self.ctx.get_graph()
+
+ # Second call should return the same graph instance
+ graph2 = self.ctx.get_graph()
+
+ assert graph1 is graph2
+
+ def test_graph_thread_safety(self):
+ """Test that graph construction is thread-safe."""
+ import threading
+ import time
+
+ graphs = []
+ exceptions = []
+
+ def get_graph():
+ try:
+ # Add small delay to increase chance of race condition
+ time.sleep(0.001)
+ graph = self.ctx.get_graph()
+ graphs.append(graph)
+ except Exception as e:
+ exceptions.append(e)
+
+ # Create multiple threads trying to get the graph simultaneously
+ threads = [threading.Thread(target=get_graph) for _ in range(10)]
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join()
+
+ # All threads should succeed and get the same graph instance
+ assert len(exceptions) == 0
+ assert len(graphs) == 10
+ assert all(graph is graphs[0] for graph in graphs)
+
+
+class TestAsyncRuleEngine:
+ """Test the async RuleEngine functionality."""
+
+ def setup_method(self):
+ self.notebook = parse_notebook("import marimo\napp = marimo.App()")
+
+ async def test_async_rule_execution(self):
+ """Test that rules are executed asynchronously."""
+ # Create mock rules with different severities
+ breaking_rule = MockRule(
+ "MB001", Severity.BREAKING, diagnostic_count=2
+ )
+ runtime_rule = MockRule("MR001", Severity.RUNTIME, diagnostic_count=1)
+ formatting_rule = MockRule(
+ "MF001", Severity.FORMATTING, diagnostic_count=3
+ )
+
+ checker = RuleEngine([breaking_rule, runtime_rule, formatting_rule])
+
+ # Execute rules
+ diagnostics = await checker.check_notebook(self.notebook)
+
+ # All rules should have been called
+ assert breaking_rule.call_count == 1
+ assert runtime_rule.call_count == 1
+ assert formatting_rule.call_count == 1
+
+ # Should get diagnostics in priority order
+ assert len(diagnostics) == 6 # 2 + 1 + 3
+
+ # First two should be breaking
+ assert diagnostics[0].severity == Severity.BREAKING
+ assert diagnostics[1].severity == Severity.BREAKING
+
+ # Next should be runtime
+ assert diagnostics[2].severity == Severity.RUNTIME
+
+ # Last three should be formatting
+ assert diagnostics[3].severity == Severity.FORMATTING
+ assert diagnostics[4].severity == Severity.FORMATTING
+ assert diagnostics[5].severity == Severity.FORMATTING
+
+ def test_sync_wrapper(self):
+ """Test the synchronous wrapper."""
+ mock_rule = MockRule("MF001", Severity.FORMATTING)
+ checker = RuleEngine([mock_rule])
+
+ # Should work synchronously
+ diagnostics = checker.check_notebook_sync(self.notebook)
+
+ assert len(diagnostics) == 1
+ assert mock_rule.call_count == 1
+
+ def test_rule_priority_execution(self):
+ """Test that diagnostics are returned in priority order."""
+ # Create rules in non-priority order
+ formatting_rule = MockRule("MF001", Severity.FORMATTING)
+ breaking_rule = MockRule("MB001", Severity.BREAKING)
+ runtime_rule = MockRule("MR001", Severity.RUNTIME)
+
+ checker = RuleEngine([formatting_rule, breaking_rule, runtime_rule])
+
+ # Get diagnostics
+ diagnostics = checker.check_notebook_sync(self.notebook)
+
+ # Should get diagnostics in priority order regardless of rule submission order
+ assert len(diagnostics) == 3
+ assert diagnostics[0].severity == Severity.BREAKING # MB001
+ assert diagnostics[1].severity == Severity.RUNTIME # MR001
+ assert diagnostics[2].severity == Severity.FORMATTING # MF001
+
+
+class TestRealRules:
+ """Test the real rule implementations with the new context system."""
+
+ async def test_formatting_rule(self):
+ """Test GeneralFormattingRule with context."""
+ # Create notebook with formatting violations
+ code = """import marimo
+
+app = marimo.App()
+
+def __():
+ x = 1
+ return x,
+"""
+ notebook = parse_notebook(code)
+ ctx = LintContext(notebook)
+
+ rule = GeneralFormattingRule()
+ await rule.check(ctx)
+
+ diagnostics = await ctx.get_diagnostics()
+ # This should succeed without error
+ assert isinstance(diagnostics, list)
+
+ async def test_multiple_definitions_rule(self):
+ """Test MultipleDefinitionsRule with context."""
+ # Create notebook with multiple definitions
+ code = """import marimo
+app = marimo.App()
+
+def _():
+ x = 1
+ return
+
+def _():
+ x = 2 # Should trigger multiple definitions
+ return
+"""
+ notebook = parse_notebook(code)
+ ctx = LintContext(notebook)
+
+ rule = MultipleDefinitionsRule()
+ await rule.check(ctx)
+
+ diagnostics = await ctx.get_diagnostics()
+ assert len(diagnostics) > 0
+ assert diagnostics[0].severity == Severity.RUNTIME
+ assert "multiple cells" in diagnostics[0].message
+
+ async def test_unparsable_rule(self):
+ """Test UnparsableRule with context."""
+ # Create a simple notebook (unparsable cells need special setup)
+ notebook = parse_notebook("import marimo\napp = marimo.App()")
+ ctx = LintContext(notebook)
+
+ rule = UnparsableRule()
+ await rule.check(ctx)
+
+ # Should not find any unparsable cells in valid code
+ diagnostics = await ctx.get_diagnostics()
+ unparsable_diagnostics = [d for d in diagnostics if d.code == "MB001"]
+ assert len(unparsable_diagnostics) == 0
+
+
+class TestIntegration:
+ """Integration tests for the complete system."""
+
+ def test_end_to_end_linting(self):
+ """Test complete end-to-end linting process."""
+ # Create notebook with multiple types of issues
+ code = """import marimo
+app = marimo.App()
+
+def _():
+ x = 1
+ return
+
+def _():
+ x = 2 # Multiple definitions
+ return
+"""
+
+ notebook = parse_notebook(code)
+ diagnostics = lint_notebook(notebook)
+
+ # Should find diagnostics
+ assert len(diagnostics) > 0
+
+ # Should be sorted by priority (breaking first, then runtime, then formatting)
+ severities = [d.severity for d in diagnostics]
+ severity_values = [s.value for s in severities]
+
+ # Check that breaking comes before runtime, runtime before formatting
+ priority_order = {"breaking": 0, "runtime": 1, "formatting": 2}
+ for i in range(len(severity_values) - 1):
+ current_priority = priority_order.get(severity_values[i], 999)
+ next_priority = priority_order.get(severity_values[i + 1], 999)
+ assert current_priority <= next_priority
+
+ def test_default_checker_creation(self):
+ """Test that default checker includes all expected rules."""
+ checker = RuleEngine.create_default()
+
+ # Should include all the standard rules
+ rule_codes = {rule.code for rule in checker.rules}
+ expected_codes = {"MF001", "MR001", "MR002", "MR003", "MB001"}
+
+ assert expected_codes.issubset(rule_codes)
diff --git a/tests/_lint/test_files/cycle_dependencies.py b/tests/_lint/test_files/cycle_dependencies.py
new file mode 100644
index 0000000000..a18bdb6648
--- /dev/null
+++ b/tests/_lint/test_files/cycle_dependencies.py
@@ -0,0 +1,26 @@
+import marimo
+
+__generated_with = "0.15.2"
+app = marimo.App()
+
+
+def _(z):
+ x = 1 + z # This should trigger MR002 - cycle dependency
+ return (x,)
+
+
+def _(x):
+ y = x + 1
+ return (y,)
+
+
+def _(y):
+ z = y + 1
+ return (z,)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_lint/test_files/formatting.py b/tests/_lint/test_files/formatting.py
new file mode 100644
index 0000000000..3f23e15aaa
--- /dev/null
+++ b/tests/_lint/test_files/formatting.py
@@ -0,0 +1,18 @@
+"""Example to test formatting issues."""
+
+import marimo as mo # intentional to test import aliasing
+
+statement = "not in a cell, so unexpected"
+
+# no generate guard
+app = mo.App()
+
+statement = "not in a cell, so unexpected"
+
+
+def _():
+ pass
+
+
+# no run guard
diff --git a/tests/_lint/test_files/multiple_definitions.py b/tests/_lint/test_files/multiple_definitions.py
new file mode 100644
index 0000000000..8d9af51c5c
--- /dev/null
+++ b/tests/_lint/test_files/multiple_definitions.py
@@ -0,0 +1,21 @@
+import marimo
+
+__generated_with = "0.15.2"
+app = marimo.App()
+
+
+def _():
+ print(1)
+ x = 1
+ return
+
+
+def _():
+ x = 2 # This should trigger MR001 - multiple definitions
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_lint/test_files/setup_dependencies.py b/tests/_lint/test_files/setup_dependencies.py
new file mode 100644
index 0000000000..4607053ee6
--- /dev/null
+++ b/tests/_lint/test_files/setup_dependencies.py
@@ -0,0 +1,19 @@
+import marimo
+
+__generated_with = "0.15.2"
+app = marimo.App()
+
+x = 1
+
+with app.setup:
+ y = x + 1 # This should trigger MR003 - setup cell dependencies
+
+
+def _():
+ x = 1
+ return (x,)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_lint/test_files/unparsable_cell.py b/tests/_lint/test_files/unparsable_cell.py
new file mode 100644
index 0000000000..e277182eb7
--- /dev/null
+++ b/tests/_lint/test_files/unparsable_cell.py
@@ -0,0 +1,26 @@
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def _():
+ x = 1
+ return
+
+
+# This should create an unparsable cell
+app._unparsable_cell("""
+x = 1 + # Syntax error
+""")
+
+
+def _():
+ y = 2
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_lint/test_lint_system.py b/tests/_lint/test_lint_system.py
new file mode 100644
index 0000000000..485af068a4
--- /dev/null
+++ b/tests/_lint/test_lint_system.py
@@ -0,0 +1,202 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""Unit tests for the marimo lint system."""
+
+from marimo._ast.parse import parse_notebook
+from marimo._lint.context import LintContext, RuleContext
+from marimo._lint.rule_engine import RuleEngine
+from marimo._lint.rules.base import Severity
+from marimo._lint.rules.breaking import (
+ CycleDependenciesRule,
+ MultipleDefinitionsRule,
+ SetupCellDependenciesRule,
+ UnparsableRule,
+)
+from marimo._lint.rules.formatting import GeneralFormattingRule
+from marimo._schemas.serialization import (
+ AppInstantiation,
+ CellDef,
+ NotebookSerializationV1,
+)
+from tests._lint.utils import lint_notebook
+
+
+class TestLintSystem:
+ """Test the core lint system functionality."""
+
+ def test_lint_notebook_basic(self):
+ """Test basic linting functionality."""
+ code = """
+import marimo
+
+app = marimo.App()
+
+def __():
+ x = 1
+ return (x,)
+"""
+ notebook = parse_notebook(code)
+ errors = lint_notebook(notebook)
+
+ # Should have formatting error for missing __generated_with
+ assert len(errors) > 0
+ assert any(error.code == "MF001" for error in errors)
+
+ def test_lint_notebook_with_violations(self):
+ """Test linting with parsing violations."""
+ code = """
+import marimo
+
+app = marimo.App()
+
+# This should create a violation
+x = 1
+
+def __():
+ y = 2
+ return (y,)
+"""
+ notebook = parse_notebook(code)
+ errors = lint_notebook(notebook)
+
+ # Should have formatting errors for violations
+ assert len(errors) > 0
+ assert any(error.code == "MF001" for error in errors)
+
+
+class TestLintRules:
+ """Test individual lint rules."""
+
+ async def test_general_formatting_rule(self):
+ """Test the general formatting rule."""
+ rule = GeneralFormattingRule()
+
+ # Create a notebook with violations
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(),
+ cells=[],
+ violations=[
+ type(
+ "Violation",
+ (),
+ {
+ "description": "UNEXPECTED_STATEMENT_CELL_DEF_VIOLATION",
+ "lineno": 1,
+ "col_offset": 0,
+ },
+ )()
+ ],
+ )
+
+ ctx = LintContext(notebook)
+ rule_ctx = RuleContext(ctx, rule)
+ await rule.check(rule_ctx)
+ errors = await ctx.get_diagnostics()
+ assert len(errors) == 1
+ assert errors[0].code == "MF001"
+ assert errors[0].severity == Severity.FORMATTING
+
+ async def test_multiple_definitions_rule(self):
+ """Test the multiple definitions rule."""
+ rule = MultipleDefinitionsRule()
+
+ # Create a simple notebook
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(),
+ cells=[
+ CellDef(code="x = 1", name="cell1", lineno=1, col_offset=0),
+ CellDef(code="x = 2", name="cell2", lineno=2, col_offset=0),
+ ],
+ )
+
+ ctx = LintContext(notebook)
+ rule_ctx = RuleContext(ctx, rule)
+ await rule.check(rule_ctx)
+ errors = await ctx.get_diagnostics()
+ # The rule should run without errors (even if no multiple definitions found)
+ assert isinstance(errors, list)
+
+ async def test_cycle_dependencies_rule(self):
+ """Test the cycle dependencies rule."""
+ rule = CycleDependenciesRule()
+
+ # Create a simple notebook
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(),
+ cells=[
+ CellDef(code="x = 1", name="cell1", lineno=1, col_offset=0),
+ CellDef(code="y = 2", name="cell2", lineno=2, col_offset=0),
+ ],
+ )
+
+ ctx = LintContext(notebook)
+ rule_ctx = RuleContext(ctx, rule)
+ await rule.check(rule_ctx)
+ errors = await ctx.get_diagnostics()
+ # The rule should run without errors
+ assert isinstance(errors, list)
+
+ async def test_setup_cell_dependencies_rule(self):
+ """Test the setup cell dependencies rule."""
+ rule = SetupCellDependenciesRule()
+
+ # Create a simple notebook
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(),
+ cells=[
+ CellDef(code="x = 1", name="cell1", lineno=1, col_offset=0),
+ ],
+ )
+
+ ctx = LintContext(notebook)
+ rule_ctx = RuleContext(ctx, rule)
+ await rule.check(rule_ctx)
+ errors = await ctx.get_diagnostics()
+ # The rule should run without errors
+ assert isinstance(errors, list)
+
+ async def test_unparsable_cells_rule(self):
+ """Test the unparsable cells rule."""
+ rule = UnparsableRule()
+
+ # Create a notebook with unparsable cell
+ from marimo._schemas.serialization import UnparsableCell
+
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(),
+ cells=[
+ UnparsableCell(
+ code="x = 1 +", name="cell1", lineno=1, col_offset=0
+ ),
+ ],
+ )
+
+ ctx = LintContext(notebook)
+ rule_ctx = RuleContext(ctx, rule)
+ await rule.check(rule_ctx)
+ errors = await ctx.get_diagnostics()
+ assert len(errors) == 1
+ assert errors[0].code == "MB001"
+ assert errors[0].severity == Severity.BREAKING
+
+
+class TestRuleEngine:
+ """Test the RuleEngine class."""
+
+ def test_create_default(self):
+ """Test creating a default checker."""
+ checker = RuleEngine.create_default()
+ assert checker is not None
+ assert len(checker.rules) > 0
+
+ def test_check_notebook(self):
+ """Test checking a notebook."""
+ checker = RuleEngine.create_default()
+
+ notebook = NotebookSerializationV1(
+ app=AppInstantiation(), cells=[], violations=[]
+ )
+
+ errors = checker.check_notebook_sync(notebook)
+ assert isinstance(errors, list)
diff --git a/tests/_lint/test_run_check.py b/tests/_lint/test_run_check.py
new file mode 100644
index 0000000000..7c08e9dcf1
--- /dev/null
+++ b/tests/_lint/test_run_check.py
@@ -0,0 +1,367 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""Unit tests for the run_check CLI integration."""
+
+import tempfile
+from pathlib import Path
+
+import pytest
+
+from marimo._lint import FileStatus, Linter, run_check
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._utils.platform import is_windows
+
+
+class TestRunCheck:
+ """Test the run_check function and CLI integration."""
+
+ def test_run_check_with_empty_files(self):
+ """Test run_check with file patterns that match no files."""
+ result = run_check(("nonexistent/**/*.py",))
+
+ assert isinstance(result, Linter)
+ assert len(result.files) == 0
+ assert result.errored is False
+
+ def test_run_check_with_unsupported_files(self):
+ """Test run_check skips unsupported file types."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ # Create a non-notebook file
+ txt_file = Path(tmpdir) / "test.txt"
+ txt_file.write_text("This is not a notebook")
+
+ result = run_check((str(txt_file),))
+
+ assert len(result.files) == 1
+ assert result.files[0].skipped is True
+ assert "not a notebook file" in result.files[0].message
+
+ def test_run_check_with_empty_py_file(self):
+ """Test run_check with an empty Python file."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ py_file = Path(tmpdir) / "empty.py"
+ py_file.write_text("")
+
+ result = run_check((str(py_file),))
+
+ assert len(result.files) == 1
+ assert result.files[0].skipped is True
+ assert "empty file" in result.files[0].message
+
+ def test_run_check_with_valid_notebook(self):
+ """Test run_check with a valid marimo notebook."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ notebook_file = Path(tmpdir) / "notebook.py"
+ notebook_content = """import marimo
+
+__generated_with = "0.15.0"
+app = marimo.App()
+
+def __():
+ x = 1
+ return (x,)
+"""
+ notebook_file.write_text(notebook_content)
+
+ result = run_check((str(notebook_file),))
+
+ assert len(result.files) == 1
+ assert result.files[0].skipped is False
+ assert result.files[0].failed is False
+ assert isinstance(result.files[0].diagnostics, list)
+
+ def test_run_check_with_syntax_error(self):
+ """Test run_check with a file containing syntax errors."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ bad_file = Path(tmpdir) / "bad.py"
+ bad_file.write_text(
+ "import marimo\napp = marimo.App(\ndef broken(:\n pass"
+ )
+
+ result = run_check((str(bad_file),))
+
+ assert len(result.files) == 1
+ assert result.files[0].failed is True
+ assert "Failed to parse" in result.files[0].message
+ assert len(result.files[0].details) > 0
+
+ @pytest.mark.skipif(
+ is_windows(), reason=r"Skip glob patterns due to path issues"
+ )
+ def test_run_check_with_glob_patterns(self):
+ """Test run_check with glob patterns."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ # Create multiple files
+ py_file = Path(tmpdir) / "test.py"
+ py_file.write_text("# empty notebook")
+
+ md_file = Path(tmpdir) / "test.md"
+ md_file.write_text("# Empty markdown")
+
+ txt_file = Path(tmpdir) / "test.txt"
+ txt_file.write_text("ignored")
+
+ # Use glob pattern
+ pattern = str(Path(tmpdir) / "*")
+ result = run_check((pattern,))
+
+ # Should find py and md files, skip txt
+ assert len(result.files) == 3
+ py_result = next(f for f in result.files if f.file.endswith(".py"))
+ md_result = next(f for f in result.files if f.file.endswith(".md"))
+ txt_result = next(
+ f for f in result.files if f.file.endswith(".txt")
+ )
+
+ # py files with simple comments are not valid notebooks, so they fail parsing
+ assert py_result.failed is True # Not a valid notebook
+ # md files with simple content might be processed differently
+ assert (
+ md_result.failed is False or md_result.skipped is True
+ ) # Markdown files may be handled differently
+ assert txt_result.skipped is True # Not a notebook
+
+
+class TestFileStatus:
+ """Test the FileStatus class."""
+
+ def test_file_status_initialization(self):
+ """Test FileStatus initialization with defaults."""
+ status = FileStatus(file="test.py")
+
+ assert status.file == "test.py"
+ assert status.diagnostics == []
+ assert status.skipped is False
+ assert status.failed is False
+ assert status.message == ""
+ assert status.details == []
+
+ def test_file_status_with_diagnostics(self):
+ """Test FileStatus with diagnostics."""
+ diagnostic = Diagnostic(
+ code="MB001",
+ name="test-error",
+ message="Test error",
+ severity=Severity.BREAKING,
+ cell_id=None,
+ line=1,
+ column=1,
+ fixable=False,
+ )
+
+ status = FileStatus(file="test.py", diagnostics=[diagnostic])
+
+ assert len(status.diagnostics) == 1
+ assert status.diagnostics[0].code == "MB001"
+
+ async def test_file_status_fix_no_fixable_diagnostics(self):
+ """Test Linter.fix() with no fixable diagnostics."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ test_file = Path(tmpdir) / "test.py"
+ original_content = "# Original content"
+ test_file.write_text(original_content)
+
+ diagnostic = Diagnostic(
+ message="Test error",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MB001",
+ name="test-error",
+ severity=Severity.BREAKING,
+ fixable=False, # Not fixable
+ )
+
+ # FileStatus needs notebook and contents for fix to work
+ status = FileStatus(
+ file=str(test_file),
+ diagnostics=[diagnostic],
+ notebook=None, # No notebook means fix returns False
+ contents=original_content,
+ )
+
+ # Since no notebook, fix should return False (no changes)
+ result = await Linter.fix(status)
+ assert result is False
+
+ # File should remain unchanged
+ assert test_file.read_text() == original_content
+
+ async def test_file_status_fix_with_fixable_diagnostics(self):
+ """Test Linter.fix() with fixable diagnostics (but no notebook)."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ test_file = Path(tmpdir) / "test.py"
+ original_content = "# Original content"
+ test_file.write_text(original_content)
+
+ diagnostic = Diagnostic(
+ message="Formatting error",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF001",
+ name="formatting-error",
+ severity=Severity.FORMATTING,
+ fixable=True, # Fixable
+ )
+
+ # FileStatus needs notebook and contents for fix to work
+ status = FileStatus(
+ file=str(test_file),
+ diagnostics=[diagnostic],
+ notebook=None, # No notebook means fix returns False
+ contents=original_content,
+ )
+
+ # Since no notebook, fix should return False (no changes)
+ result = await Linter.fix(status)
+ assert result is False
+
+ # File should remain unchanged
+ assert test_file.read_text() == original_content
+
+ async def test_file_status_fix_with_multiple_diagnostics(self):
+ """Test Linter.fix() with multiple diagnostics (but no notebook)."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ test_file = Path(tmpdir) / "test.py"
+ original_content = "# Original content"
+ test_file.write_text(original_content)
+
+ diagnostics = [
+ Diagnostic(
+ message="Formatting",
+ cell_id=None,
+ line=1,
+ column=1,
+ code="MF001",
+ name="formatting-error",
+ severity=Severity.FORMATTING,
+ fixable=True,
+ ),
+ Diagnostic(
+ message="Breaking",
+ cell_id=None,
+ line=2,
+ column=1,
+ code="MB001",
+ name="breaking-error",
+ severity=Severity.BREAKING,
+ fixable=True,
+ ),
+ Diagnostic(
+ message="Runtime",
+ cell_id=None,
+ line=3,
+ column=1,
+ code="MR001",
+ name="runtime-error",
+ severity=Severity.RUNTIME,
+ fixable=True,
+ ),
+ ]
+
+ # FileStatus needs notebook and contents for fix to work
+ status = FileStatus(
+ file=str(test_file),
+ diagnostics=diagnostics,
+ notebook=None, # No notebook means fix returns False
+ contents=original_content,
+ )
+
+ # Since no notebook, fix should return False (no changes)
+ result = await Linter.fix(status)
+ assert result is False
+
+ # File should remain unchanged
+ assert test_file.read_text() == original_content
+
+
+class TestLinter:
+ """Test the Linter class."""
+
+ def test_check_result_initialization(self):
+ """Test Linter initialization."""
+ result = Linter()
+
+ assert result.files == []
+ assert result.errored is False
+
+ def test_check_result_with_files(self):
+ """Test Linter with file status objects added to files list."""
+ file_status = FileStatus(file="test.py")
+ result = Linter()
+
+ # Manually add files and set errored (simulating after run)
+ result.files.append(file_status)
+ result.errored = True
+
+ assert len(result.files) == 1
+ assert result.files[0].file == "test.py"
+ assert result.errored is True
+
+
+class TestIntegration:
+ """Integration tests combining multiple components."""
+
+ def test_full_workflow_with_notebook_violations(self):
+ """Test the full workflow with a notebook that has violations."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ notebook_file = Path(tmpdir) / "bad_notebook.py"
+
+ # Create a notebook with violations (missing __generated_with)
+ notebook_content = """import marimo
+
+app = marimo.App()
+
+# This violates marimo structure - code outside cell
+x = 1
+
+def __():
+ y = 2
+ return (y,)
+"""
+ notebook_file.write_text(notebook_content)
+
+ result = run_check((str(notebook_file),))
+
+ assert len(result.files) == 1
+ file_status = result.files[0]
+
+ assert not file_status.skipped
+ assert not file_status.failed
+ assert len(file_status.diagnostics) > 0
+
+ # Should have formatting violations
+ assert any(
+ d.severity == Severity.FORMATTING
+ for d in file_status.diagnostics
+ )
+
+ # Test fixing (if notebook is available)
+ if file_status.notebook is not None:
+ # Use Linter.fix() static method
+ import asyncio
+
+ result = asyncio.run(Linter.fix(file_status))
+ assert isinstance(result, bool)
+
+ def test_error_handling_in_run_check(self):
+ """Test error handling in run_check."""
+ # Test by providing invalid content that will cause parsing to fail
+ import tempfile
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ test_file = Path(tmpdir) / "test.py"
+ # Write invalid Python content that will cause an exception
+ test_file.write_text(
+ "import marimo\napp = marimo.App()\ndef broken(:\n pass"
+ )
+
+ result = run_check((str(test_file),))
+
+ assert len(result.files) == 1
+ assert result.files[0].failed is True
+ # Note: errored might not be True as we changed error handling
+ assert "Failed to parse" in result.files[0].message
+ """Test error handling in run_check."""
diff --git a/tests/_lint/test_runtime_errors_snapshot.py b/tests/_lint/test_runtime_errors_snapshot.py
new file mode 100644
index 0000000000..e9d4afe9cc
--- /dev/null
+++ b/tests/_lint/test_runtime_errors_snapshot.py
@@ -0,0 +1,93 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""Snapshot tests for runtime lint errors."""
+
+from marimo._ast.parse import parse_notebook
+from tests._lint.utils import lint_notebook
+from tests.mocks import snapshotter
+
+snapshot = snapshotter(__file__)
+
+
+def test_multiple_definitions_snapshot():
+ """Test snapshot for multiple definitions error."""
+ file = "tests/_lint/test_files/multiple_definitions.py"
+ with open(file) as f:
+ code = f.read()
+
+ notebook = parse_notebook(code, filepath=file)
+ errors = lint_notebook(notebook)
+
+ # Format errors for snapshot
+ error_output = []
+ for error in errors:
+ error_output.append(error.format())
+
+ snapshot("multiple_definitions_errors.txt", "\n".join(error_output))
+
+
+def test_cycle_dependencies_snapshot():
+ """Test snapshot for cycle dependencies error."""
+ file = "tests/_lint/test_files/cycle_dependencies.py"
+ with open(file) as f:
+ code = f.read()
+
+ notebook = parse_notebook(code, filepath=file)
+ errors = lint_notebook(notebook)
+
+ # Format errors for snapshot
+ error_output = []
+ for error in errors:
+ error_output.append(error.format())
+
+ snapshot("cycle_dependencies_errors.txt", "\n".join(error_output))
+
+
+def test_setup_dependencies_snapshot():
+ """Test snapshot for setup dependencies error."""
+ file = "tests/_lint/test_files/setup_dependencies.py"
+ with open(file) as f:
+ code = f.read()
+
+ notebook = parse_notebook(code, filepath=file)
+ errors = lint_notebook(notebook)
+
+ # Format errors for snapshot
+ error_output = []
+ for error in errors:
+ error_output.append(error.format())
+
+ snapshot("setup_dependencies_errors.txt", "\n".join(error_output))
+
+
+def test_unparsable_cell_snapshot():
+ """Test snapshot for unparsable cell error."""
+ file = "tests/_lint/test_files/unparsable_cell.py"
+ with open(file) as f:
+ code = f.read()
+
+ notebook = parse_notebook(code, filepath=file)
+ errors = lint_notebook(notebook)
+
+ # Format errors for snapshot
+ error_output = []
+ for error in errors:
+ error_output.append(error.format())
+
+ snapshot("unparsable_cell_errors.txt", "\n".join(error_output))
+
+
+def test_formatting_snapshot():
+ """Test snapshot for unparsable cell error."""
+ file = "tests/_lint/test_files/formatting.py"
+ with open(file) as f:
+ code = f.read()
+
+ notebook = parse_notebook(code, filepath=file)
+ errors = lint_notebook(notebook)
+
+ # Format errors for snapshot
+ error_output = []
+ for error in errors:
+ error_output.append(error.format())
+
+ snapshot("formatting.txt", "\n".join(error_output))
diff --git a/tests/_lint/test_streaming_early_stopping.py b/tests/_lint/test_streaming_early_stopping.py
new file mode 100644
index 0000000000..8b024494a8
--- /dev/null
+++ b/tests/_lint/test_streaming_early_stopping.py
@@ -0,0 +1,382 @@
+# Copyright 2025 Marimo. All rights reserved.
+"""Unit tests for streaming diagnostics and early stopping functionality."""
+
+import asyncio
+
+from marimo._ast.parse import parse_notebook
+from marimo._lint.context import LintContext
+from marimo._lint.diagnostic import Diagnostic, Severity
+from marimo._lint.rule_engine import EarlyStoppingConfig, RuleEngine
+from marimo._lint.rules.base import LintRule
+
+
+def create_diagnostic(
+ code: str,
+ cell_id: str,
+ message: str,
+ severity: Severity,
+ fixable: bool = False,
+) -> Diagnostic:
+ """Helper to create diagnostic with correct parameter order."""
+ return Diagnostic(
+ message=message,
+ cell_id=cell_id,
+ line=1,
+ column=1,
+ code=code,
+ severity=severity,
+ fixable=fixable,
+ )
+
+
+class SlowRule(LintRule):
+ """Mock rule that takes time to complete."""
+
+ def __init__(
+ self,
+ code: str,
+ severity: Severity,
+ delay: float = 0.1,
+ diagnostic_count: int = 1,
+ ):
+ # Set class attributes dynamically
+ self.code = code
+ self.name = f"slow-{code.lower()}"
+ self.description = f"Slow rule {code}"
+ self.severity = severity
+ self.fixable = False
+
+ # Instance attributes
+ self.delay = delay
+ self.diagnostic_count = diagnostic_count
+ self.started = False
+ self.completed = False
+ self.cancelled = False
+
+ async def check(self, ctx: LintContext) -> None:
+ """Add diagnostics after delay."""
+ self.started = True
+ try:
+ await asyncio.sleep(self.delay)
+ for i in range(self.diagnostic_count):
+ diagnostic = Diagnostic(
+ code=self.code,
+ name=self.name,
+ message=f"Slow diagnostic {i + 1}",
+ severity=self.severity,
+ cell_id=None,
+ line=1,
+ column=1,
+ fixable=self.fixable,
+ )
+ await ctx.add_diagnostic(diagnostic)
+ self.completed = True
+ except asyncio.CancelledError:
+ self.cancelled = True
+ raise
+
+
+class TestLintContextStreaming:
+ """Test LintContext streaming functionality."""
+
+ def setup_method(self):
+ self.notebook = parse_notebook("import marimo\napp = marimo.App()")
+ self.ctx = LintContext(self.notebook)
+
+ async def test_get_new_diagnostics_empty(self):
+ """Test get_new_diagnostics when no diagnostics added."""
+ new_diagnostics = await self.ctx.get_new_diagnostics()
+ assert len(new_diagnostics) == 0
+
+ async def test_get_new_diagnostics_incremental(self):
+ """Test get_new_diagnostics returns only new diagnostics."""
+ # Add first batch
+ diag1 = Diagnostic(
+ message="first",
+ cell_id="test1",
+ line=1,
+ column=1,
+ code="MF001",
+ severity=Severity.FORMATTING,
+ fixable=False,
+ )
+ diag2 = Diagnostic(
+ message="second",
+ cell_id="test2",
+ line=1,
+ column=1,
+ code="MR001",
+ severity=Severity.RUNTIME,
+ fixable=False,
+ )
+
+ await self.ctx.add_diagnostic(diag1)
+ await self.ctx.add_diagnostic(diag2)
+
+ # Get first batch
+ new_diagnostics = await self.ctx.get_new_diagnostics()
+ assert len(new_diagnostics) == 2
+ assert (
+ new_diagnostics[0].severity == Severity.RUNTIME
+ ) # Higher priority first
+ assert new_diagnostics[1].severity == Severity.FORMATTING
+
+ # Add second batch
+ diag3 = create_diagnostic("MB001", "test3", "third", Severity.BREAKING)
+ await self.ctx.add_diagnostic(diag3)
+
+ # Get only new diagnostics
+ new_diagnostics = await self.ctx.get_new_diagnostics()
+ assert len(new_diagnostics) == 1
+ assert new_diagnostics[0].severity == Severity.BREAKING
+
+ # Calling again should return empty
+ new_diagnostics = await self.ctx.get_new_diagnostics()
+ assert len(new_diagnostics) == 0
+
+ async def test_get_all_diagnostics_still_works(self):
+ """Test that get_diagnostics still returns all diagnostics."""
+ diag1 = create_diagnostic(
+ "MF001", "test1", "first", Severity.FORMATTING
+ )
+ diag2 = create_diagnostic("MR001", "test2", "second", Severity.RUNTIME)
+
+ await self.ctx.add_diagnostic(diag1)
+ await self.ctx.add_diagnostic(diag2)
+
+ # Get new diagnostics
+ await self.ctx.get_new_diagnostics()
+
+ # get_diagnostics should still return all
+ all_diagnostics = await self.ctx.get_diagnostics()
+ assert len(all_diagnostics) == 2
+
+
+class TestEarlyStoppingConfig:
+ """Test EarlyStoppingConfig functionality."""
+
+ def test_no_early_stopping(self):
+ """Test default config doesn't stop."""
+ config = EarlyStoppingConfig()
+
+ breaking_diag = create_diagnostic(
+ "MB001", "test", "breaking", Severity.BREAKING
+ )
+ runtime_diag = create_diagnostic(
+ "MR001", "test", "runtime", Severity.RUNTIME
+ )
+
+ assert not config.should_stop(breaking_diag, 1)
+ assert not config.should_stop(runtime_diag, 1)
+
+ def test_stop_on_breaking(self):
+ """Test stopping on breaking severity."""
+ config = EarlyStoppingConfig(stop_on_breaking=True)
+
+ breaking_diag = create_diagnostic(
+ "MB001", "test", "breaking", Severity.BREAKING
+ )
+ runtime_diag = create_diagnostic(
+ "MR001", "test", "runtime", Severity.RUNTIME
+ )
+
+ assert config.should_stop(breaking_diag, 1)
+ assert not config.should_stop(runtime_diag, 1)
+
+ def test_stop_on_runtime(self):
+ """Test stopping on runtime severity."""
+ config = EarlyStoppingConfig(stop_on_runtime=True)
+
+ runtime_diag = create_diagnostic(
+ "MR001", "test", "runtime", Severity.RUNTIME
+ )
+ formatting_diag = create_diagnostic(
+ "MF001", "test", "formatting", Severity.FORMATTING
+ )
+
+ assert config.should_stop(runtime_diag, 1)
+ assert not config.should_stop(formatting_diag, 1)
+
+ def test_max_diagnostics(self):
+ """Test stopping based on max diagnostic count."""
+ config = EarlyStoppingConfig(max_diagnostics=2)
+
+ diag = Diagnostic(
+ "MF001",
+ "test",
+ "formatting",
+ Severity.FORMATTING,
+ None,
+ 1,
+ 1,
+ False,
+ )
+
+ assert not config.should_stop(diag, 1)
+ assert config.should_stop(diag, 2)
+ assert config.should_stop(diag, 3)
+
+ def test_stop_on_first_of_severity(self):
+ """Test stopping on first occurrence of specific severity."""
+ config = EarlyStoppingConfig(
+ stop_on_first_of_severity=Severity.RUNTIME
+ )
+
+ runtime_diag = create_diagnostic(
+ "MR001", "test", "runtime", Severity.RUNTIME
+ )
+ breaking_diag = create_diagnostic(
+ "MB001", "test", "breaking", Severity.BREAKING
+ )
+ formatting_diag = create_diagnostic(
+ "MF001", "test", "formatting", Severity.FORMATTING
+ )
+
+ assert config.should_stop(runtime_diag, 1)
+ assert not config.should_stop(breaking_diag, 1)
+ assert not config.should_stop(formatting_diag, 1)
+
+
+class TestStreamingRuleEngine:
+ """Test streaming functionality of RuleEngine."""
+
+ def setup_method(self):
+ self.notebook = parse_notebook("import marimo\napp = marimo.App()")
+
+ async def test_streaming_basic(self):
+ """Test basic streaming functionality."""
+ # Create rules with different completion times
+ fast_rule = SlowRule("MR001", Severity.RUNTIME, delay=0.01)
+ slow_rule = SlowRule("MF001", Severity.FORMATTING, delay=0.05)
+
+ checker = RuleEngine([fast_rule, slow_rule])
+
+ # Collect diagnostics as they stream
+ diagnostics = []
+ async for diagnostic in checker.check_notebook_streaming(
+ self.notebook
+ ):
+ diagnostics.append(diagnostic)
+
+ # Should get diagnostics as rules complete
+ assert len(diagnostics) == 2
+
+ # Fast rule should complete first, but diagnostics are ordered by priority
+ # So we should get runtime before formatting
+ assert diagnostics[0].severity == Severity.RUNTIME
+ assert diagnostics[1].severity == Severity.FORMATTING
+
+ # Both rules should have completed
+ assert fast_rule.completed
+ assert slow_rule.completed
+
+ async def test_early_stopping_on_breaking(self):
+ """Test early stopping cancels remaining tasks."""
+ # Create rules with different severities and delays
+ fast_breaking = SlowRule("MB001", Severity.BREAKING, delay=0.01)
+ slow_formatting = SlowRule(
+ "MF001", Severity.FORMATTING, delay=0.1
+ ) # Takes longer
+
+ config = EarlyStoppingConfig(stop_on_breaking=True)
+ checker = RuleEngine(
+ [fast_breaking, slow_formatting], early_stopping=config
+ )
+
+ # Collect diagnostics
+ diagnostics = []
+ async for diagnostic in checker.check_notebook_streaming(
+ self.notebook
+ ):
+ diagnostics.append(diagnostic)
+
+ # Should only get one diagnostic (breaking)
+ assert len(diagnostics) == 1
+ assert diagnostics[0].severity == Severity.BREAKING
+
+ # Fast rule should complete, slow rule should be cancelled
+ assert fast_breaking.completed
+ assert slow_formatting.cancelled
+
+ async def test_early_stopping_max_diagnostics(self):
+ """Test early stopping based on max diagnostic count."""
+ # Create rule that produces multiple diagnostics
+ multi_rule = SlowRule(
+ "MF001", Severity.FORMATTING, delay=0.01, diagnostic_count=3
+ )
+ slow_rule = SlowRule("MR001", Severity.RUNTIME, delay=0.1)
+
+ config = EarlyStoppingConfig(max_diagnostics=2)
+ checker = RuleEngine([multi_rule, slow_rule], early_stopping=config)
+
+ diagnostics = []
+ async for diagnostic in checker.check_notebook_streaming(
+ self.notebook
+ ):
+ diagnostics.append(diagnostic)
+
+ # Should stop after 2 diagnostics
+ assert len(diagnostics) == 2
+
+ # Slow rule should be cancelled
+ assert slow_rule.cancelled
+
+ def test_backward_compatibility(self):
+ """Test that non-streaming methods still work."""
+ fast_rule = SlowRule("MR001", Severity.RUNTIME, delay=0.01)
+ slow_rule = SlowRule("MF001", Severity.FORMATTING, delay=0.02)
+
+ checker = RuleEngine([fast_rule, slow_rule])
+
+ # Non-streaming method should still work
+ diagnostics = checker.check_notebook_sync(self.notebook)
+
+ assert len(diagnostics) == 2
+ assert fast_rule.completed
+ assert slow_rule.completed
+
+
+class TestRealWorldScenarios:
+ """Test real-world scenarios with streaming and early stopping."""
+
+ def setup_method(self):
+ self.notebook = parse_notebook("""import marimo
+app = marimo.App()
+
+def _():
+ x = 1
+ return
+
+def _():
+ x = 2 # Multiple definitions
+ return
+""")
+
+ async def test_real_rules_with_early_stopping(self):
+ """Test real rules with early stopping."""
+ from marimo._lint.rules.breaking import MultipleDefinitionsRule
+ from marimo._lint.rules.formatting import GeneralFormattingRule
+
+ # Stop on first runtime error
+ config = EarlyStoppingConfig(stop_on_runtime=True)
+ checker = RuleEngine(
+ [MultipleDefinitionsRule(), GeneralFormattingRule()],
+ early_stopping=config,
+ )
+
+ diagnostics = []
+ async for diagnostic in checker.check_notebook_streaming(
+ self.notebook
+ ):
+ diagnostics.append(diagnostic)
+ # Should stop after first runtime error
+ if diagnostic.severity == Severity.RUNTIME:
+ break
+
+ # Should have at least one runtime diagnostic (multiple definitions)
+ runtime_diagnostics = [
+ d for d in diagnostics if d.severity == Severity.RUNTIME
+ ]
+ assert len(runtime_diagnostics) > 0
diff --git a/tests/_runtime/test_validate_graph.py b/tests/_lint/test_validate_graph.py
similarity index 88%
rename from tests/_runtime/test_validate_graph.py
rename to tests/_lint/test_validate_graph.py
index 7643dffcf8..53ef340779 100644
--- a/tests/_runtime/test_validate_graph.py
+++ b/tests/_lint/test_validate_graph.py
@@ -6,13 +6,13 @@ from typing import cast
from marimo._ast import compiler
from marimo._ast.names import SETUP_CELL_NAME
+from marimo._lint.validate_graph import check_for_errors
from marimo._messaging.errors import (
CycleError,
MultipleDefinitionError,
SetupRootError,
)
from marimo._runtime import dataflow
-from marimo._runtime.validate_graph import check_for_errors
parse_cell = partial(compiler.compile_cell, cell_id="0")
@@ -76,10 +76,10 @@ def test_two_node_cycle() -> None:
errors = check_for_errors(graph)
assert set(errors.keys()) == set(["0", "1"])
assert errors["0"] == (
- CycleError(edges_with_vars=(("0", ["x"], "1"), ("1", ["y"], "0"))),
+ CycleError(edges_with_vars=(("0", ("x",), "1"), ("1", ("y",), "0"))),
)
assert errors["1"] == (
- CycleError(edges_with_vars=(("0", ["x"], "1"), ("1", ["y"], "0"))),
+ CycleError(edges_with_vars=(("0", ("x",), "1"), ("1", ("y",), "0"))),
)
@@ -95,9 +95,9 @@ def test_three_node_cycle() -> None:
assert isinstance(t[0], CycleError)
edges_with_vars = t[0].edges_with_vars
assert len(edges_with_vars) == 3
- assert ("0", ["x"], "2") in edges_with_vars
- assert ("1", ["y"], "0") in edges_with_vars
- assert ("2", ["z"], "1") in edges_with_vars
+ assert ("0", ("x",), "2") in edges_with_vars
+ assert ("1", ("y",), "0") in edges_with_vars
+ assert ("2", ("z",), "1") in edges_with_vars
def test_cycle_and_multiple_def() -> None:
@@ -117,8 +117,8 @@ def test_cycle_and_multiple_def() -> None:
)
edges_with_vars = cycle_error.edges_with_vars
assert len(edges_with_vars) == 2
- assert ("0", ["x"], "1") in edges_with_vars
- assert ("1", ["y"], "0") in edges_with_vars
+ assert ("0", ("x",), "1") in edges_with_vars
+ assert ("1", ("y",), "0") in edges_with_vars
multiple_definition_error = cast(
MultipleDefinitionError,
@@ -138,8 +138,8 @@ def test_del_ref_cycle() -> None:
# Edge ordering in returned error is not deterministic, so we list both
# possible cycles
expected_cycle = [
- CycleError(edges_with_vars=(("2", ["x"], "1"), ("1", ["y"], "2"))),
- CycleError(edges_with_vars=(("1", ["y"], "2"), ("2", ["x"], "1"))),
+ CycleError(edges_with_vars=(("2", ("x",), "1"), ("1", ("y",), "2"))),
+ CycleError(edges_with_vars=(("1", ("y",), "2"), ("2", ("x",), "1"))),
]
assert len(errors["1"]) == 1
@@ -160,4 +160,4 @@ def test_setup_has_refs() -> None:
setup_error = cast(SetupRootError, t[0])
edges_with_vars = setup_error.edges_with_vars
assert len(edges_with_vars) == 1
- assert ("0", ["y"], SETUP_CELL_NAME) in edges_with_vars
+ assert ("0", ("y",), SETUP_CELL_NAME) in edges_with_vars
diff --git a/tests/_lint/utils.py b/tests/_lint/utils.py
new file mode 100644
index 0000000000..79b6722b76
--- /dev/null
+++ b/tests/_lint/utils.py
@@ -0,0 +1,9 @@
+"""Utilities for lint tests."""
+
+from marimo._lint.rule_engine import RuleEngine
+
+
+def lint_notebook(notebook):
+ """Lint a notebook and return all diagnostics found."""
+ rule_engine = RuleEngine.create_default()
+ return rule_engine.check_notebook_sync(notebook)
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index 2f4e3e260b..5e9335001d 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -670,8 +670,12 @@ class TestExecution:
assert len(k.errors["1"]) == 1
if k.execution_type == "strict":
assert len(k.errors["2"]) == 1
- _check_edges(k.errors["0"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])
- _check_edges(k.errors["1"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])
+ _check_edges(
+ k.errors["0"][0], [("0", ("x",), "1"), ("1", ("y",), "0")]
+ )
+ _check_edges(
+ k.errors["1"][0], [("0", ("x",), "1"), ("1", ("y",), "0")]
+ )
# break cycle by modifying cell
await k.run([ExecutionRequest(cell_id="1", code="y=1")])
@@ -702,7 +706,9 @@ class TestExecution:
assert set(k.errors.keys()) == {"0", "1"}
assert len(k.errors["0"]) == 1
assert len(k.errors["1"]) == 1
- _check_edges(k.errors["0"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])
+ _check_edges(
+ k.errors["0"][0], [("0", ("x",), "1"), ("1", ("y",), "0")]
+ )
# break cycle by deleting cell
await k.delete_cell(DeleteCellRequest(cell_id="1"))
@@ -742,8 +748,12 @@ class TestExecution:
]
)
assert set(k.errors.keys()) == {"0", "1"}
- _check_edges(k.errors["0"][0], [("0", ["x"], "1"), ("1", ["x"], "0")])
- _check_edges(k.errors["1"][0], [("0", ["x"], "1"), ("1", ["x"], "0")])
+ _check_edges(
+ k.errors["0"][0], [("0", ("x",), "1"), ("1", ("x",), "0")]
+ )
+ _check_edges(
+ k.errors["1"][0], [("0", ("x",), "1"), ("1", ("x",), "0")]
+ )
async def test_delete_nonlocal_incremental_ref_raises_name_error(
self, k: Kernel