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
improve: error messages for import *, multiple definition (#3937)
Akshay Agrawal86e7a8e380e4
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yamlindex 36a3e0a2b9..fc56a9faa0 100644--- a/frontend/pnpm-lock.yaml+++ b/frontend/pnpm-lock.yaml@@ -44,7 +44,7 @@ importers:version: 6.8.4'@codemirror/search':specifier: ^6.5.8- version: 6.5.9+ version: 6.5.10'@codemirror/state':specifier: ^6.5.2version: 6.5.2@@ -191,7 +191,7 @@ importers:'@replit/codemirror-vim':specifier: ^6.2.1- version: 6.2.1(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])+ version: 6.2.1(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])'@tailwindcss/typography':specifier: ^0.5.16version: 0.5.16([email protected])@@ -221,7 +221,7 @@ importers:version: 4.23.8(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])(@lezer/[email protected])(@lezer/[email protected])(@lezer/[email protected])'@uiw/react-codemirror':specifier: ^4.23.7- version: 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])+ version: 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])'@uwdata/flechette':specifier: ^1.1.2version: 1.1.2@@ -338,7 +338,7 @@ importers:react-codemirror-merge:specifier: ^4.23.7- version: 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])+ version: 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])react-dropzone:specifier: ^14.3.5version: 14.3.5([email protected])@@ -998,8 +998,8 @@ packages:'@codemirror/[email protected]':resolution: {integrity: sha512-VXKxm8Jrv2HpEVW9CcYiV4VMVWsFz2XOk34Ve9ouqFT4iKc2r9+IGuMjyeH+g5T0HKUnRGYIv2wayMsPtao7Zw==}- '@codemirror/[email protected]':- resolution: {integrity: sha512-7DdQ9aaZMMxuWB1u6IIFWWuK9NocVZwvo4nG8QjJTS6oZGvteoLSiXw3EbVZVlO08Ri2ltO89JVInMpfcJxhtg==}+ '@codemirror/[email protected]':+ resolution: {integrity: sha512-RMdPdmsrUf53pb2VwflKGHEe1XVM07hI7vV2ntgw1dmqhimpatSJKva4VA9h4TLUDOD4EIF02201oZurpnEFsg==}'@codemirror/[email protected]':resolution: {integrity: sha512-FVqsPqtPWKVVL3dPSxy8wEF/ymIEuVzF1PK3VbUgrxXpJUSHQWWZz4JMToquRxnkw+36LTamCZG2iua2Ptq0fA==}@@ -8115,8 +8115,8 @@ packages:resolution: {integrity: sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==}engines: {node: '>=6'}- resolution: {integrity: sha512-RVCsMfuD0+cTt3EwX8hSl2Ks56EbFHWmhluwcqoPKtBnfjiT6olaq7PRIRfhyU8nnC2MrnDrBLfrD/RGE+cVXQ==}+ resolution: {integrity: sha512-jDLYqo7oF8tJIttjXO6jBY5Hk8p3A8W4ttih7cCEq64fQFWmgJ4VqAQjKr7WwIDlmXKEc6QeoRb5ecjZ+2afcg==}engines: {node: '>= 10.13.0'}peerDependencies:'@swc/core': '*'@@ -9620,7 +9620,7 @@ snapshots:'@lezer/highlight': 1.2.1style-mod: 4.1.2- '@codemirror/[email protected]':+ '@codemirror/[email protected]':dependencies:'@codemirror/state': 6.5.2'@codemirror/view': 6.36.3@@ -12111,11 +12111,11 @@ snapshots:'@lezer/javascript': 1.4.18'@lezer/lr': 1.4.2- '@replit/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':+ '@replit/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':dependencies:'@codemirror/commands': 6.8.0'@codemirror/language': 6.10.8- '@codemirror/search': 6.5.9+ '@codemirror/search': 6.5.10'@codemirror/state': 6.5.2'@codemirror/view': 6.36.3@@ -12989,13 +12989,13 @@ snapshots:react: 18.3.1react-dom: 18.3.1([email protected])- '@uiw/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':+ '@uiw/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':dependencies:'@codemirror/autocomplete': 6.18.6'@codemirror/commands': 6.8.0'@codemirror/language': 6.10.8'@codemirror/lint': 6.8.4- '@codemirror/search': 6.5.9+ '@codemirror/search': 6.5.10'@codemirror/state': 6.5.2'@codemirror/view': 6.36.3@@ -13038,14 +13038,14 @@ snapshots:- '@lezer/javascript'- '@lezer/lr'- '@uiw/[email protected](@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])':+ '@uiw/[email protected](@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])':dependencies:'@babel/runtime': 7.25.6'@codemirror/commands': 6.8.0'@codemirror/state': 6.5.2'@codemirror/theme-one-dark': 6.1.2'@codemirror/view': 6.36.3- '@uiw/codemirror-extensions-basic-setup': 4.23.7(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])+ '@uiw/codemirror-extensions-basic-setup': 4.23.7(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])codemirror: 6.0.1react: 18.3.1react-dom: 18.3.1([email protected])@@ -13791,7 +13791,7 @@ snapshots:'@codemirror/commands': 6.8.0'@codemirror/language': 6.10.8'@codemirror/lint': 6.8.4- '@codemirror/search': 6.5.9+ '@codemirror/search': 6.5.10'@codemirror/state': 6.5.2'@codemirror/view': 6.36.3@@ -17355,14 +17355,14 @@ snapshots:react: 18.3.1react-dom: 18.3.1([email protected])- [email protected](@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected]):+ [email protected](@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected]):dependencies:'@babel/runtime': 7.25.6'@codemirror/merge': 6.6.0'@codemirror/state': 6.5.2'@codemirror/theme-one-dark': 6.1.2'@codemirror/view': 6.36.3- '@uiw/react-codemirror': 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])+ '@uiw/react-codemirror': 4.23.7(@babel/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])([email protected])([email protected]([email protected]))([email protected])codemirror: 6.0.1react: 18.3.1react-dom: 18.3.1([email protected])@@ -18432,7 +18432,7 @@ snapshots:dependencies:'@jridgewell/trace-mapping': 0.3.25jest-worker: 27.5.1@@ -19303,7 +19303,7 @@ snapshots:neo-async: 2.6.2schema-utils: 3.3.0tapable: 2.2.1watchpack: 2.4.2webpack-sources: 3.2.3transitivePeerDependencies:diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsxindex b2cc454005..69ebcf6e9b 100644--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx@@ -24,8 +24,29 @@ const Tip = (props: {return (<Accordion type="single" collapsible={true} className={props.className}><AccordionItem value="item-1" className="text-muted-foreground">- <AccordionTrigger className="py-2">Tip:</AccordionTrigger>- <AccordionContent>{props.children}</AccordionContent>+ <AccordionTrigger className="pt-4 pb-2">Tip</AccordionTrigger>+ <AccordionContent className="mr-24 text-[0.84375rem]">+ {props.children}+ </AccordionContent>+ </AccordionItem>+ </Accordion>+ );+};++const Explainer = (props: {+ title?: string;+ className?: string;+ children: React.ReactNode;+}): JSX.Element => {+ return (+ <Accordion type="single" collapsible={true} className={props.className}>+ <AccordionItem value="item-1" className="text-muted-foreground">+ <AccordionTrigger className="pt-4 pb-2">+ {props.title ?? "Learn more"}+ </AccordionTrigger>+ <AccordionContent className="mr-24 text-[0.84375rem]">+ {props.children}+ </AccordionContent></AccordionItem></Accordion>);@@ -58,7 +79,9 @@ export const MarimoErrorOutput = ({case "cycle":return (<Fragment key={idx}>- <p>{"This cell is in a cycle:"}</p>+ <p className="text-muted-foreground">+ {"This cell is in a cycle:"}+ </p><ul className="list-disc">{error.edges_with_vars.map((edge) => (<li className={liStyle} key={`${edge[0]}-${edge[1]}`}>@@ -79,7 +102,7 @@ export const MarimoErrorOutput = ({case "multiple-defs":return (<Fragment key={idx}>- <p>{`The variable '${error.name}' was defined by another cell:`}</p>+ <p className="text-muted-foreground">{`The variable '${error.name}' was defined by another cell:`}</p><ul className="list-disc">{error.cells.map((cid) => (<li className={liStyle} key={cid}>@@ -88,13 +111,48 @@ export const MarimoErrorOutput = ({))}</ul><Tip>- Try merging this cell with the above cells. Alternatively, rename- '{error.name}' to '_{error.name}' to make the variable private to- this cell.+ <p className="pb-2">+ marimo requires that each variable is defined in just one cell.+ This constraint enables reactive and reproducible execution,+ arbitrary cell reordering, seamless UI elements, execution as a+ script, and more.+ </p>++ <p className="py-2">+ Try merging this cell with the above cells or wrapping it in a+ function. Alternatively, rename '{error.name}' to '_{error.name}+ ' to make the variable private to this cell.+ </p></Tip></Fragment>);+ case "import-star":+ return (+ <Fragment key={idx}>+ <p className="text-muted-foreground">{error.msg}</p>++ <Explainer>+ <p className="pb-2">+ Star imports are incompatible with marimo's git-friendly file+ format and reproducible reactive execution.+ </p>++ <p className="py-2">+ marimo's Python file format stores code in functions, so+ notebooks can be imported as regular Python modules without+ executing all their code. But Python disallows `import *`+ everywhere except at the top-level of a module.+ </p>++ <p className="py-2">+ Star imports would also silently add names to globals, which+ would be incompatible with reactive execution.+ </p>+ </Explainer>+ </Fragment>+ );+case "delete-nonlocal":return (<Fragment key={idx}>@@ -120,8 +178,8 @@ export const MarimoErrorOutput = ({titleContents = error.exception_type;return error.raising_cell == null ? (<Fragment key={idx}>- <p>{error.msg}</p>- <div className="text-sm text-muted-foreground mt-2">+ <p className="text-muted-foreground">{error.msg}</p>+ <div className="text-muted-foreground mt-2">See the console area for a traceback.</div></Fragment>diff --git a/marimo/_ast/errors.py b/marimo/_ast/errors.pyindex f5e0ec30cd..487301d9af 100644--- a/marimo/_ast/errors.py+++ b/marimo/_ast/errors.py@@ -3,6 +3,10 @@ class CycleError(Exception):pass+class ImportStarError(SyntaxError):+ pass++class MultipleDefinitionError(Exception):passdiff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.pyindex 074d55a08f..00b6110626 100644--- a/marimo/_ast/visitor.py+++ b/marimo/_ast/visitor.py@@ -10,6 +10,7 @@ from typing import Callable, Literal, Optional, Unionfrom uuid import uuid4from marimo import _loggers+from marimo._ast.errors import ImportStarErrorfrom marimo._ast.sql_visitor import (SQLDefs,find_sql_defs,@@ -220,8 +221,9 @@ class ScopedVisitor(ast.NodeVisitor):if hasattr(node, "lineno")else "line ...")- raise SyntaxError(- f"{line} SyntaxError: `import *` is not allowed in marimo."+ raise ImportStarError(+ f"{line} SyntaxError: Importing symbols with `import *` "+ "is not allowed in marimo.")return basenameelse:diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.pyindex 1c5b291116..daefe4a66f 100644--- a/marimo/_cli/development/commands.py+++ b/marimo/_cli/development/commands.py@@ -57,6 +57,7 @@ def _generate_server_api_schema() -> dict[str, Any]:# Errorserrors.CycleError,errors.MultipleDefinitionError,+ errors.ImportStarError,errors.DeleteNonlocalError,errors.MarimoInterruptionError,errors.MarimoInternalError,diff --git a/marimo/_messaging/errors.py b/marimo/_messaging/errors.pyindex 5db59fdc50..bac0c9e99e 100644--- a/marimo/_messaging/errors.py+++ b/marimo/_messaging/errors.py@@ -27,6 +27,15 @@ class MultipleDefinitionError:return f"The variable '{self.name}' was defined by another cell"+@dataclass+class ImportStarError:+ msg: str+ type: Literal["import-star"] = "import-star"++ def describe(self) -> str:+ return self.msg++@dataclassclass DeleteNonlocalError:name: str@@ -154,6 +163,7 @@ def is_sensitive_error(error: Error) -> bool:Error = Union[CycleError,MultipleDefinitionError,+ ImportStarError,DeleteNonlocalError,MarimoAncestorStoppedError,MarimoAncestorPreventedError,diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 7731cea53a..fab00a03f6 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -22,6 +22,7 @@ from uuid import uuid4from marimo import _loggersfrom marimo._ast.cell import CellConfig, CellImplfrom marimo._ast.compiler import compile_cell+from marimo._ast.errors import ImportStarErrorfrom marimo._ast.variables import is_localfrom marimo._ast.visitor import ImportData, Name, VariableDatafrom marimo._config.config import ExecutionType, MarimoConfig, OnCellChangeType@@ -35,6 +36,7 @@ from marimo._messaging.cell_output import CellChannelfrom marimo._messaging.context import http_request_context, run_id_contextfrom marimo._messaging.errors import (Error,+ ImportStarError as MarimoImportStarError,MarimoInterruptionError,MarimoStrictExecutionError,MarimoSyntaxError,@@ -766,7 +768,10 @@ class Kernel:syntax_error[0] = syntax_error[0][syntax_error[0].find("line") :]- error = MarimoSyntaxError(msg="\n".join(syntax_error))+ if isinstance(e, ImportStarError):+ error = MarimoImportStarError(msg=str(e))+ else:+ error = MarimoSyntaxError(msg="\n".join(syntax_error))else:tmpio = io.StringIO()traceback.print_exc(file=tmpio)diff --git a/openapi/api.yaml b/openapi/api.yamlindex e0061c127b..1c2ad3e6b5 100644--- a/openapi/api.yaml+++ b/openapi/api.yaml@@ -586,6 +586,7 @@ components:oneOf:- $ref: '#/components/schemas/CycleError'- $ref: '#/components/schemas/MultipleDefinitionError'+ - $ref: '#/components/schemas/ImportStarError'- $ref: '#/components/schemas/DeleteNonlocalError'- $ref: '#/components/schemas/MarimoAncestorStoppedError'- $ref: '#/components/schemas/MarimoAncestorPreventedError'@@ -949,6 +950,18 @@ components:required:- codetype: object+ ImportStarError:+ properties:+ msg:+ type: string+ type:+ enum:+ - import-star+ type: string+ required:+ - msg+ - type+ type: objectInstallMissingPackagesRequest:properties:manager:@@ -2194,7 +2207,7 @@ components:type: objectinfo:title: marimo API- version: 0.11.9+ version: 0.11.11openapi: 3.1.0paths:/@file/{filename_and_length}:diff --git a/openapi/src/api.ts b/openapi/src/api.tsindex 45de65aa96..534c0666f1 100644--- a/openapi/src/api.ts+++ b/openapi/src/api.ts@@ -2338,6 +2338,7 @@ export interface components {Error:| components["schemas"]["CycleError"]| components["schemas"]["MultipleDefinitionError"]+ | components["schemas"]["ImportStarError"]| components["schemas"]["DeleteNonlocalError"]| components["schemas"]["MarimoAncestorStoppedError"]| components["schemas"]["MarimoAncestorPreventedError"]@@ -2471,7 +2472,21 @@ export interface components {function_call_id: string;/** @enum {string} */name: "function-call-result";- return_value?: components["schemas"]["JSONType"];+ return_value?:+ | (+ | {+ [key: string]: components["schemas"]["JSONType"];+ }+ | components["schemas"]["JSONType"][]+ | string+ | number+ | boolean+ | {+ [key: string]: unknown;+ }+ | components["schemas"]["MIME"]+ )+ | null;status: components["schemas"]["HumanReadableStatus"];};HTTPRequest: null;@@ -2481,6 +2496,11 @@ export interface components {message?: string | null;title?: string | null;};+ ImportStarError: {+ msg: string;+ /** @enum {string} */+ type: "import-star";+ };InstallMissingPackagesRequest: {manager: string;versions: {@@ -2549,9 +2569,38 @@ export interface components {[key: string]:| (| {- [key: string]: components["schemas"]["JSONType"];+ [key: string]:+ | (+ | {+ [key: string]: components["schemas"]["JSONType"];+ }+ | components["schemas"]["JSONType"][]+ | string+ | number+ | boolean+ | {+ [key: string]: unknown;+ }+ | components["schemas"]["MIME"]+ )+ | null;}- | components["schemas"]["JSONType"][]+ | (+ | (+ | {+ [key: string]: components["schemas"]["JSONType"];+ }+ | components["schemas"]["JSONType"][]+ | string+ | number+ | boolean+ | {+ [key: string]: unknown;+ }+ | components["schemas"]["MIME"]+ )+ | null+ )[]| string| number| booleandiff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.pyindex d4cdc97f58..8333bcc5d4 100644--- a/tests/_ast/test_visitor.py+++ b/tests/_ast/test_visitor.py@@ -8,6 +8,7 @@ from textwrap import dedentimport pytestfrom marimo._ast import visitor+from marimo._ast.errors import ImportStarErrorfrom marimo._ast.visitor import (ImportData,VariableData,@@ -798,7 +799,7 @@ def test_from_import_star() -> None:expr = "from a.b.c import *"v = visitor.ScopedVisitor()mod = ast.parse(expr)- with pytest.raises(SyntaxError) as e:+ with pytest.raises(ImportStarError) as e:v.visit(mod)assert "`import *` is not allowed in marimo." in str(e)assert v.defs == set()