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.

Use runtime assertions instead of type assertions (#5208)

Trevor Manzf82812dbe37e
diff --git a/frontend/src/components/pages/home-page.tsx b/frontend/src/components/pages/home-page.tsx
index e0da4b082b..e21b003401 100644
--- a/frontend/src/components/pages/home-page.tsx
+++ b/frontend/src/components/pages/home-page.tsx
@@ -22,11 +22,7 @@ import {
SearchIcon,
} from "lucide-react";
import { ShutdownButton } from "../editor/controls/shutdown-button";
-import {
- type SessionId,
- getSessionId,
- isSessionId,
-} from "@/core/kernel/session";
+import { getSessionId, isSessionId } from "@/core/kernel/session";
import { useInterval } from "@/hooks/useInterval";
import { useImperativeModal } from "@/components/modal/ImperativeModal";
import { AlertDialogDestructiveAction } from "@/components/ui/alert-dialog";
@@ -469,9 +465,9 @@ const SessionShutdownButton: React.FC<{ filePath: string }> = ({
<AlertDialogDestructiveAction
onClick={(e) => {
const ids = runningNotebooks.get(filePath);
- assertExists(ids);
+ assertExists(ids?.sessionId);
shutdownSession({
- sessionId: ids.sessionId as SessionId,
+ sessionId: ids.sessionId,
}).then((response) => {
setRunningNotebooks(
Maps.keyBy(response.files, (file) => file.path),
diff --git a/frontend/src/core/mode.ts b/frontend/src/core/mode.ts
index 9d9878b65a..9131be1dd6 100644
--- a/frontend/src/core/mode.ts
+++ b/frontend/src/core/mode.ts
@@ -5,6 +5,7 @@ import type { CellId } from "./cells/ids";
import { store } from "./state/jotai";
import { assertExists } from "@/utils/assertExists";
import { isIslands } from "@/core/islands/utils";
+import { invariant } from "@/utils/invariant";
/**
* This is the internal mode.
@@ -18,7 +19,11 @@ export type AppMode = "read" | "edit" | "present" | "home";
export function getInitialAppMode(): Exclude<AppMode, "present"> {
const initialMode = store.get(initialModeAtom);
assertExists(initialMode, "internal-error: initial mode not found");
- return initialMode as Exclude<AppMode, "present">;
+ invariant(
+ initialMode !== "present",
+ "internal-error: initial mode cannot be 'present'",
+ );
+ return initialMode;
}
export function toggleAppMode(mode: AppMode): AppMode {
diff --git a/frontend/src/mount.tsx b/frontend/src/mount.tsx
index 626328c7ba..af9bead574 100644
--- a/frontend/src/mount.tsx
+++ b/frontend/src/mount.tsx
@@ -223,7 +223,7 @@ function initStore(options: unknown) {
Logger.error("Invalid marimo mount options", parsedOptions.error);
throw new Error("Invalid marimo mount options");
}
- const mode = parsedOptions.data.mode as AppMode;
+ const mode = parsedOptions.data.mode;
preloadPage(mode);
// Initialize file stores if provided
diff --git a/frontend/src/utils/invariant.ts b/frontend/src/utils/invariant.ts
index 0227d4ff09..5829e8a5e5 100644
--- a/frontend/src/utils/invariant.ts
+++ b/frontend/src/utils/invariant.ts
@@ -1,17 +1,30 @@
/* Copyright 2024 Marimo. All rights reserved. */
+/**
+ * Make an assertion.
+ *
+ * If the expression is falsy, an error is thrown with the provided message.
+ *
+ * @example
+ * ```ts
+ * const value: boolean = Math.random() <= 0.5;
+ * invariant(value, "Value is greater than 0.5");
+ * value; // true
+ * ```
+ *
+ * @example
+ * ```ts
+ * const user: { name?: string } = await fetchUser();
+ * invariant(user.name, "User missing name");
+ * user.name; // string
+ * ```
+ *
+ * @param expression - The condition to check.
+ * @param msg - The error message to throw if the assertion fails.
+ * @throws {Error} If `expression` is falsy.
+ */
export function invariant(
- condition: boolean,
- message: string,
-): asserts condition;
-export function invariant<T>(
- condition: T,
- message: string,
-): asserts condition is NonNullable<T>;
-export function invariant(
- condition: boolean,
- message: string,
-): asserts condition {
- if (!condition) {
- throw new Error(message);
- }
+ expression: unknown,
+ msg: string,
+): asserts expression {
+ if (!expression) throw new Error(msg);
}