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.

fix: remove deepcopy from save / compile path (#7226)

Dylan Madisetti22cbbca67f2b
diff --git a/frontend/src/components/editor/file-tree/__tests__/file-expolorer.test.ts b/frontend/src/components/editor/file-tree/__tests__/file-expolorer.test.ts
index 3b700bb3a3..a68c0ee922 100644
--- a/frontend/src/components/editor/file-tree/__tests__/file-expolorer.test.ts
+++ b/frontend/src/components/editor/file-tree/__tests__/file-expolorer.test.ts
@@ -1,6 +1,6 @@
-import { describe, it, expect } from "vitest";
-import { isDirectoryOrFileHidden, filterHiddenTree } from "../file-explorer";
+import { describe, expect, it } from "vitest";
import type { FileInfo } from "@/core/network/types";
+import { filterHiddenTree, isDirectoryOrFileHidden } from "../file-explorer";
// Helpers to build FileInfo objects for tests
const file = (name: string, path: string): FileInfo => ({
diff --git a/frontend/src/components/editor/file-tree/file-explorer.tsx b/frontend/src/components/editor/file-tree/file-explorer.tsx
index 00c836332f..7c0356ea4a 100644
--- a/frontend/src/components/editor/file-tree/file-explorer.tsx
+++ b/frontend/src/components/editor/file-tree/file-explorer.tsx
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { useAtom } from "jotai";
+import { atomWithStorage } from "jotai/utils";
import {
ArrowLeftIcon,
BetweenHorizontalStartIcon,
@@ -57,6 +58,7 @@ import { downloadBlob } from "@/utils/download";
import { openNotebook } from "@/utils/links";
import type { FilePath } from "@/utils/paths";
import { fileSplit } from "@/utils/pathUtils";
+import { jotaiJsonStorage } from "@/utils/storage/jotai";
import marimoIcon from "../../../assets/icon-32x32.png";
import { FileViewer } from "./file-viewer";
import type { RequestingTree } from "./requesting-tree";
@@ -68,15 +70,15 @@ import {
PYTHON_CODE_FOR_FILE_TYPE,
} from "./types";
import { useFileExplorerUpload } from "./upload";
-import { atomWithStorage } from "jotai/utils";
-import { jotaiJsonStorage } from "@/utils/storage/jotai";
-const hiddenFilesState = atomWithStorage("marimo:showHiddenFiles", true,
+const hiddenFilesState = atomWithStorage(
+ "marimo:showHiddenFiles",
+ true,
jotaiJsonStorage,
{
getOnInit: true,
},
-)
+);
const RequestingTreeContext = React.createContext<RequestingTree | null>(null);
@@ -87,8 +89,8 @@ export const FileExplorer: React.FC<{
const [tree] = useAtom(treeAtom);
const [data, setData] = useState<FileInfo[]>([]);
const [openFile, setOpenFile] = useState<FileInfo | null>(null);
- const [showHiddenFiles, setShowHiddenFiles] = useAtom<boolean>(hiddenFilesState);
-
+ const [showHiddenFiles, setShowHiddenFiles] =
+ useAtom<boolean>(hiddenFilesState);
const { openPrompt } = useImperativeModal();
// Keep external state to remember which folders are open
@@ -166,7 +168,10 @@ export const FileExplorer: React.FC<{
);
}
- const visibleData = React.useMemo(() => filterHiddenTree(data, showHiddenFiles), [data, showHiddenFiles]);
+ const visibleData = React.useMemo(
+ () => filterHiddenTree(data, showHiddenFiles),
+ [data, showHiddenFiles],
+ );
return (
<>
<Toolbar
@@ -644,8 +649,8 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
className={cn(
"flex items-center pl-1 py-1 cursor-pointer hover:bg-accent/50 hover:text-accent-foreground rounded-l flex-1 overflow-hidden group",
node.willReceiveDrop &&
- node.data.isDirectory &&
- "bg-accent/80 hover:bg-accent/80 text-accent-foreground",
+ node.data.isDirectory &&
+ "bg-accent/80 hover:bg-accent/80 text-accent-foreground",
)}
>
{node.data.isMarimoFile ? (
@@ -710,23 +715,25 @@ function openMarimoNotebook(
openNotebook(path);
}
-export function filterHiddenTree(list: FileInfo[], showHidden: boolean): FileInfo[] {
+export function filterHiddenTree(
+ list: FileInfo[],
+ showHidden: boolean,
+): FileInfo[] {
if (showHidden) {
- return list
+ return list;
}
const out: FileInfo[] = [];
for (const item of list) {
if (isDirectoryOrFileHidden(item.name)) {
- continue
- };
+ continue;
+ }
let next = item;
if (item.children && item.children.length) {
const kids = filterHiddenTree(item.children, showHidden);
if (kids !== item.children) {
- next = { ...item, children: kids }
-
- };
+ next = { ...item, children: kids };
+ }
}
out.push(next);
}
@@ -734,8 +741,8 @@ export function filterHiddenTree(list: FileInfo[], showHidden: boolean): FileInf
}
export function isDirectoryOrFileHidden(filename: string): boolean {
- if (filename.startsWith('.')) {
- return true
+ if (filename.startsWith(".")) {
+ return true;
}
- return false
+ return false;
}
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index 7b70da3697..fb80522913 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -2,7 +2,6 @@
from __future__ import annotations
import ast
-import copy
import inspect
import io
import linecache
@@ -43,6 +42,21 @@ def ast_compile(*args: Any, **kwargs: Any) -> CodeType:
return cast(CodeType, compile(*args, **kwargs)) # type: ignore[call-overload]
+def module_compile(code: str) -> ast.Module:
+ # Overloads on compile are strange, cast for proper typing.
+ return 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,
+ ),
+ )
+
+
def code_key(code: str) -> int:
return hash(code)
@@ -236,18 +250,7 @@ def compile_cell(
# See https://github.com/pyodide/pyodide/issues/3337,
# https://github.com/marimo-team/marimo/issues/1546
code = code.replace("\u00a0", " ")
- # 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,
- ),
- )
+ module = module_compile(code)
if not module.body:
# either empty code or just comments
@@ -277,7 +280,9 @@ def compile_cell(
expr: ast.Expression
final_expr = module.body[-1]
- original_module = copy.deepcopy(module)
+ # Compile again as an effective copy since copying directly seems slow and
+ # error prone.
+ original_module = module_compile(code)
# 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):