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: Handle banned names when renaming a cell (#505)
Myles Scolnicka76b830d7572
diff --git a/frontend/src/components/editor/actions/name-cell-input.tsx b/frontend/src/components/editor/actions/name-cell-input.tsxindex 2eba2b3f14..6671acb065 100644--- a/frontend/src/components/editor/actions/name-cell-input.tsx+++ b/frontend/src/components/editor/actions/name-cell-input.tsx@@ -78,6 +78,7 @@ export const NameCellContentEditable: React.FC<{className)}contentEditable={true}+ suppressContentEditableWarning={true}onChange={inputProps.onChange}onBlur={inputProps.onBlur}onKeyDown={Events.onEnter((e) => {diff --git a/frontend/src/components/editor/cell/cell-actions.tsx b/frontend/src/components/editor/cell/cell-actions.tsxindex 044cacc262..d5099775c6 100644--- a/frontend/src/components/editor/cell/cell-actions.tsx+++ b/frontend/src/components/editor/cell/cell-actions.tsx@@ -51,6 +51,9 @@ export const CellActionsDropdown = ({ children, ...props }: Props) => {</div></TooltipContent>)}+ {/* This creates a warning in React due to nested <button> elements.+ Adding asChild could fix this, but it also changes the styling (is hidden) of the button when+ the Popover is open. */}<TooltipTrigger><PopoverTrigger className="flex">{children}</PopoverTrigger></TooltipTrigger>diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsxindex 89c60e86ff..416af8f310 100644--- a/frontend/src/components/editor/output/ConsoleOutput.tsx+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx@@ -31,7 +31,11 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {hasOutputs ? "p-5" : "p-3")}>- {consoleOutputs.map((output) => formatOutput({ message: output }))}+ {consoleOutputs.map((output, idx) => (+ <React.Fragment key={idx}>+ {formatOutput({ message: output })}+ </React.Fragment>+ ))}<NameCellContentEditablevalue={cellName}cellId={cellId}diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsxindex 3279764f07..670c908792 100644--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx@@ -99,12 +99,12 @@ export const MarimoErrorOutput = ({case "delete-nonlocal":return (<Fragment key={idx}>- <p className="mt-4">+ <div className="mt-4">{`The variable '${error.name}' can't be deleted because it was defined by another cell ` +`(`}<CellLinkError cellId={error.cells[0]} />{")"}- </p>+ </div><Tip>Try refactoring so that you can delete '{error.name}' in the cellthat creates it.@@ -126,24 +126,24 @@ export const MarimoErrorOutput = ({<Tip>See the console area for a traceback.</Tip></Fragment>) : (- <p key={idx}>+ <div key={idx}>{error.msg}<CellLinkError cellId={error.raising_cell} /><Tip>Fix the error in <CellLinkError cellId={error.raising_cell} />, orhandle the exception in with a try/except block.</Tip>- </p>+ </div>);case "ancestor-stopped":titleContents = "Ancestor stopped";alertVariant = "default";textColor = "text-secondary-foreground";return (- <p key={idx}>+ <div key={idx}>{error.msg}<CellLinkError cellId={error.raising_cell} />- </p>+ </div>);default:diff --git a/frontend/src/core/cells/__tests__/names.test.ts b/frontend/src/core/cells/__tests__/names.test.tsindex a7eb07338e..350ba3b792 100644--- a/frontend/src/core/cells/__tests__/names.test.ts+++ b/frontend/src/core/cells/__tests__/names.test.ts@@ -34,6 +34,15 @@ describe("getValidName", () => {expect(getValidName("name", ["name"])).toBe("name_1");expect(getValidName("name", ["name_1", "name"])).toBe("name_2");});++ it("should avoid names that are in the disallowed set", () => {+ expect(getValidName("None", [])).toBe("None_1");+ expect(getValidName("None", ["None_1"])).toBe("None_2");++ expect(getValidName("marimo", [])).toBe("marimo_1");+ expect(getValidName("app", [])).toBe("app_1");+ expect(getValidName("continue", [])).toBe("continue_1");+ });});describe("displayCellName", () => {diff --git a/frontend/src/core/cells/names.ts b/frontend/src/core/cells/names.tsindex 102471c80c..d76313b7ef 100644--- a/frontend/src/core/cells/names.ts+++ b/frontend/src/core/cells/names.ts@@ -4,6 +4,50 @@ import { CellId, HTMLCellId } from "./ids";export const DEFAULT_CELL_NAME = "__";+// Generated with `python scripts/print_banned_cell_names.py`+const DISALLOWED_NAMES = new Set([+ "__*",+ "marimo",+ "app",+ "False",+ "None",+ "True",+ "__peg_parser__",+ "and",+ "as",+ "assert",+ "async",+ "await",+ "break",+ "class",+ "continue",+ "def",+ "del",+ "elif",+ "else",+ "except",+ "finally",+ "for",+ "from",+ "global",+ "if",+ "import",+ "in",+ "is",+ "lambda",+ "nonlocal",+ "not",+ "or",+ "pass",+ "raise",+ "return",+ "try",+ "while",+ "with",+ "yield",+ "None",+]);+/*** Make's name pythonic - removes spaces, special characters and makes lowercase*/@@ -26,12 +70,16 @@ export function getValidName(name: string, existingNames: string[]): string {const set = new Set(existingNames);let result = name;- if (!set.has(name)) {+ const isValid = (name: string) => {+ return !set.has(name) && !DISALLOWED_NAMES.has(name);+ };++ if (isValid(name)) {return name;}let count = 1;- while (set.has(result)) {+ while (!isValid(result)) {result = `${name}_${count}`;count++;}