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
fix: still allow codegen even if there are duplicate defs (#7280)
Myles Scolnicka2d38cab2726
diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.pyindex 4dea703f31..c3bc5a4a5b 100644--- a/marimo/_ast/toplevel.py+++ b/marimo/_ast/toplevel.py@@ -276,7 +276,9 @@ class TopLevelExtraction:unresolved: set[Name] = set()for status in self.statuses:if status.is_unresolved or status.is_cell:- self._statuses[status.type].pop(status.name)+ # Use pop with default to handle duplicate definitions gracefully+ # If there are duplicates, only the last one is in the dict+ self._statuses[status.type].pop(status.name, None)status.update(self.allowed_refs,unresolved,diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.pyindex 39953cfd1b..96c4aa9e41 100644--- a/tests/_ast/test_codegen.py+++ b/tests/_ast/test_codegen.py@@ -281,6 +281,20 @@ class TestGeneration:"test_generate_filecontents_shadowed_builtin")+ @staticmethod+ def test_generate_filecontents_duplicate_definitions() -> None:+ """Test that duplicate top-level definitions don't cause KeyError during codegen."""+ cell_one = "def Two(): return 2"+ cell_two = "def Two(): return 'two'"+ codes = [cell_one, cell_two]+ names = ["one", "two"]+ # This should not raise a KeyError during TopLevelExtraction+ # (duplicate validation happens at app.run(), not during codegen)+ contents = wrap_generate_filecontents(codes, names)+ # Should successfully generate file contents+ assert "import marimo" in contents+ assert contents is not None+def test_with_second_type_noop(self) -> None:referring = "x = 1; x: int = 0"ref_vars = compile_cell(referring).init_variable_data