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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Report multiple definitions from the host analyzer (#11258)
Trevor Manz9e6855843f71
diff --git a/marimo/_ast/definition_range.py b/marimo/_ast/definition_range.pynew file mode 100644index 0000000000..4ad7f285cc--- /dev/null+++ b/marimo/_ast/definition_range.py@@ -0,0 +1,111 @@+# Copyright 2026 Marimo. All rights reserved.+"""Locate a module-level binding's identifier in its original source."""++from __future__ import annotations++import ast+import io+import tokenize+from dataclasses import dataclass+++@dataclass(frozen=True)+class DefinitionRange:+ """Zero-based identifier range with Unicode code-point columns."""++ line: int+ column: int+ end_column: int+++def find_definition_range(+ module: ast.Module, name: str, source: str+) -> DefinitionRange | None:+ """Return the first module-level binding's range, when present in source."""+ visitor = _DefinitionVisitor(name, source)+ visitor.visit(module)+ return visitor.range+++_SCOPES = (+ ast.FunctionDef,+ ast.AsyncFunctionDef,+ ast.ClassDef,+ ast.Lambda,+ ast.ListComp,+ ast.SetComp,+ ast.DictComp,+ ast.GeneratorExp,+)+++class _DefinitionVisitor(ast.NodeVisitor):+ def __init__(self, name: str, source: str) -> None:+ self._name = name+ self._lines = source.splitlines()+ self._tokens = tuple(+ tokenize.generate_tokens(io.StringIO(source).readline)+ )+ self.range: DefinitionRange | None = None++ def visit(self, node: ast.AST) -> None:+ if self.range is not None:+ return+ if isinstance(node, _SCOPES):+ if (+ isinstance(+ node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)+ )+ and node.name == self._name+ ):+ self._record(node)+ return+ super().visit(node)++ def visit_Name(self, node: ast.Name) -> None:+ if node.id == self._name and isinstance(node.ctx, ast.Store):+ self._record(node)++ def visit_alias(self, node: ast.alias) -> None:+ if (node.asname or node.name.split(".")[0]) == self._name:+ self._record(node, last=node.asname is not None)++ def _record(+ self,+ node: ast.Name+ | ast.alias+ | ast.FunctionDef+ | ast.AsyncFunctionDef+ | ast.ClassDef,+ *,+ last: bool = False,+ ) -> None:+ # AST columns are UTF-8 byte offsets; tokenize columns count characters.+ line = node.lineno+ column = len(+ self._lines[line - 1]+ .encode("utf-8")[: node.col_offset]+ .decode("utf-8")+ )+ end_line = node.end_lineno+ assert end_line is not None+ assert node.end_col_offset is not None+ end_column = len(+ self._lines[end_line - 1]+ .encode("utf-8")[: node.end_col_offset]+ .decode("utf-8")+ )+ tokens = [+ token+ for token in self._tokens+ if token.type == tokenize.NAME+ and token.string == self._name+ and (line, column) <= token.start < (end_line, end_column)+ ]+ if tokens:+ token = tokens[-1] if last else tokens[0]+ self.range = DefinitionRange(+ line=token.start[0] - 1,+ column=token.start[1],+ end_column=token.end[1],+ )diff --git a/marimo/_lint/visitors.py b/marimo/_lint/visitors.pyindex 11cee56630..beedd183c5 100644--- a/marimo/_lint/visitors.py+++ b/marimo/_lint/visitors.py@@ -5,14 +5,25 @@ from __future__ import annotationsimport ast+from marimo._ast.definition_range import DefinitionRange, find_definition_range+class VariableLineVisitor(ast.NodeVisitor):"""AST visitor to find the line number of a variable definition."""- def __init__(self, target_variable: str):+ def __init__(self, target_variable: str, source: str | None = None):self.target_variable = target_variableself.line_number: int | None = Noneself.column_number: int = 1+ self.definition_range: DefinitionRange | None = None+ self._source = source++ def visit_Module(self, node: ast.Module) -> None:+ if self._source is not None:+ self.definition_range = find_definition_range(+ node, self.target_variable, self._source+ )+ self.generic_visit(node)def visit_Name(self, node: ast.Name) -> None:"""Visit Name nodes to find variable definitions."""diff --git a/marimo/_server/host/analysis.py b/marimo/_server/host/analysis.pyindex e84520dc76..85494cf2bf 100644--- a/marimo/_server/host/analysis.py+++ b/marimo/_server/host/analysis.py@@ -1,7 +1,7 @@# Copyright 2026 Marimo. All rights reserved.-"""Cell definitions, references, and syntax errors for the host protocol.+"""Cell definitions, references, syntax errors, and multiple definitions.-The kernel reports errors involving multiple cells.+The kernel reports cycles."""from __future__ import annotations@@ -13,6 +13,8 @@ from typing import TYPE_CHECKINGfrom marimo._ast.cell import CellImplfrom marimo._ast.compiler import compile_cellfrom marimo._core import protocol+from marimo._lint.validate_graph import check_for_multiple_definitions+from marimo._lint.visitors import VariableLineVisitorfrom marimo._runtime.dataflow import DirectedGraphfrom marimo._runtime.dataflow.edges import resolve_cell_referencesfrom marimo._types.ids import CellId_t@@ -67,6 +69,8 @@ class CellAnalyzer:graph.register_cell(CellId_t(cell.id), cached.result)self._cache = cache+ conflicts = check_for_multiple_definitions(graph)+ order = {cell.id: index for index, cell in enumerate(document)}analyses: dict[protocol.CellId, protocol.CellAnalysis] = {}for cell in document:compiled = cache[cell.id].result@@ -84,10 +88,72 @@ class CellAnalyzer:analyses[cell.id] = protocol.CellAnalysis(defs=tuple(sorted(compiled.defs)),refs=tuple(sorted(refs)),- errors=(),+ errors=tuple(+ self._multiple_definitions(+ cell.id,+ error.name,+ sorted(+ [+ cell.id,+ *(+ protocol.CellId(cid)+ for cid in error.cells+ ),+ ],+ key=order.__getitem__,+ ),+ )+ for error in conflicts.get(CellId_t(cell.id), ())+ ),)return analyses+ def _multiple_definitions(+ self,+ cell_id: protocol.CellId,+ name: str,+ definers: list[protocol.CellId],+ ) -> protocol.Error:+ return protocol.Error(+ kind="multiple-definitions",+ message=f"Multiple cells define {name}",+ source=SOURCE,+ severity="error",+ range=self._definition(cell_id, name),+ name=None,+ traceback=None,+ related=tuple(+ protocol.Related(+ cell_id=definer,+ range=self._definition(definer, name),+ message=f"Defines {name}",+ )+ for definer in definers+ ),+ )++ def _definition(+ self, cell_id: protocol.CellId, name: str+ ) -> protocol.Range | None:+ compiled = self._cache[cell_id].result+ assert isinstance(compiled, CellImpl)+ visitor = VariableLineVisitor(name, compiled.code)+ visitor.visit(compiled.mod)+ location = visitor.definition_range+ if location is None:+ return None+ line = location.line+ return protocol.Range(+ start=protocol.Position(+ line=line,+ character=_utf16(compiled.code, line, location.column),+ ),+ end=protocol.Position(+ line=line,+ character=_utf16(compiled.code, line, location.end_column),+ ),+ )+def _syntax_error(code: str, e: Exception) -> protocol.Error:where = Nonediff --git a/protocols/host/fixtures/diagnostic.json b/protocols/host/fixtures/diagnostic.jsonindex 75b6607a09..78ba20678d 100644--- a/protocols/host/fixtures/diagnostic.json+++ b/protocols/host/fixtures/diagnostic.json@@ -11,6 +11,6 @@"traceback": null,"related": [{"cell_id": "c1", "range": null, "message": "Defines x"},- {"cell_id": "c2", "range": null, "message": "Also defines x"}+ {"cell_id": "c2", "range": null, "message": "Defines x"}]}diff --git a/tests/_server/host/test_host_analysis.py b/tests/_server/host/test_host_analysis.pyindex 7ba2eb2494..ae7e5fd0c0 100644--- a/tests/_server/host/test_host_analysis.py+++ b/tests/_server/host/test_host_analysis.py@@ -1,5 +1,5 @@# Copyright 2026 Marimo. All rights reserved.-"""The host's analyzer reports each cell's names and syntax errors."""+"""The host's analyzer reports each cell's names, syntax errors, and conflicts."""from __future__ import annotations@@ -47,12 +47,80 @@ def test_refs_are_names_other_cells_define() -> None:assert analyses["c"].refs == ("x",)-def test_a_name_defined_twice_is_a_ref_for_neither_definer() -> None:- analyses = analyze({"a": "x = 1", "b": "x = 2\ny = x", "c": "z = x"})+def test_a_name_defined_twice_is_an_error_on_each_definer() -> None:+ analyses = analyze({"a": "x = 1", "b": "y = 2\nx = y", "c": "z = x"})assert analyses["b"].refs == ()assert analyses["c"].refs == ("x",)- # Cross-cell errors come from the kernel at run time for now.- assert all(analyses[cell_id].errors == () for cell_id in "abc")+ assert analyses["c"].errors == ()++ (first,) = analyses["a"].errors+ (second,) = analyses["b"].errors+ assert first.kind == second.kind == "multiple-definitions"+ assert first.message == "Multiple cells define x"+ assert first.severity == "error"+ # Each copy is ranged at its own definition and lists every definer.+ assert first.range == span(0, 0, 1)+ assert second.range == span(1, 0, 1)+ assert first.related == second.related+ assert [(r.cell_id, r.range, r.message) for r in first.related] == [+ ("a", span(0, 0, 1), "Defines x"),+ ("b", span(1, 0, 1), "Defines x"),+ ]+++def test_definition_ranges_cover_the_bound_name() -> None:+ analyses = analyze(+ {+ "a": "def x():\n pass",+ "b": "import numpy as x",+ "c": "for x in range(3):\n def y():\n x = 1",+ "d": "mo.sql('CREATE TABLE x (id INT)')",+ }+ )+ ranges = {cell_id: analyses[cell_id].errors[0].range for cell_id in "abcd"}+ assert ranges["a"] == span(0, 4, 5)+ assert ranges["b"] == span(0, 16, 17)+ assert ranges["c"] == span(0, 4, 5)+ # A SQL definition has no name in the Python source.+ assert ranges["d"] is None+++def span(line: int, start: int, end: int) -> protocol.Range:+ return protocol.Range(+ start=protocol.Position(line=line, character=start),+ end=protocol.Position(line=line, character=end),+ )+++def test_qualified_sql_definitions_only_conflict_with_the_same_table() -> None:+ analyses = analyze(+ {+ "a": 'mo.sql("CREATE TABLE finance.datalake.xx (id INT)")',+ "b": 'mo.sql("CREATE TABLE finance.information_layer.xx (id INT)")',+ "c": 'mo.sql("CREATE TABLE finance.datalake.xx (id INT)")',+ }+ )+ assert analyses["b"].errors == ()+ for cid in ("a", "c"):+ (error,) = analyses[cid].errors+ assert error.kind == "multiple-definitions"+ assert tuple(item.cell_id for item in error.related) == ("a", "c")+ assert error.range is None+++ ("code", "expected"),+ [+ ("import xlib as x", span(0, 15, 16)),+ ("async def x():\n pass", span(0, 10, 11)),+ ("s = '😀'; x = 1", span(0, 10, 11)),+ ("def f():\n x = 1\nx = 2", span(2, 0, 1)),+ ("[x for x in ()]\nx = 2", span(1, 0, 1)),+ ],+)+def test_definition_ranges_use_shared_scope_and_token_handling(code, expected):+ analyses = analyze({"a": code, "b": "x = 0"})+ assert analyses["a"].errors[0].range == expecteddef test_syntax_errors_leave_the_cell_without_names() -> None: