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
Strengthen `createReducer` types to enforce action shapes (#8884)
Trevor Manz81bc960c4e4c
diff --git a/frontend/src/core/cells/__tests__/document-changes.test.ts b/frontend/src/core/cells/__tests__/document-changes.test.tsindex 2dffa166bd..13a0cfeb87 100644--- a/frontend/src/core/cells/__tests__/document-changes.test.ts+++ b/frontend/src/core/cells/__tests__/document-changes.test.ts@@ -51,10 +51,7 @@ function createEditor(code: string) {}/** Dispatch an action through the reducer and auto-create editor handles. */-function dispatch(- state: NotebookState,- action: { type: string; payload: unknown },-): NotebookState {+function dispatch(state: NotebookState, action: CellAction): NotebookState {const next = reducer(state, action);for (const [cellIdString, handle] of Object.entries(next.cellHandles)) {const cid = cellIdString as CellId;diff --git a/frontend/src/core/cells/__tests__/runs.test.ts b/frontend/src/core/cells/__tests__/runs.test.tsindex 493aa8a85a..82272435ab 100644--- a/frontend/src/core/cells/__tests__/runs.test.ts+++ b/frontend/src/core/cells/__tests__/runs.test.ts@@ -148,7 +148,7 @@ describe("RunsState Reducer", () => {run_id: runId,cell_id: testCellId,timestamp: runStartTimestamp + 5000,- status: "success",+ status: "idle",},code: "console.log('Hello World');",},@@ -226,7 +226,8 @@ describe("RunsState Reducer", () => {status: "running",output: {channel: "stderr",- text: "Error occurred",+ data: "Error occurred",+ mimetype: "text/plain",},},code: "console.log('Hello World');",@@ -254,7 +255,8 @@ describe("RunsState Reducer", () => {status: "running",output: {channel: "marimo-error",- text: "Error occurred",+ data: "Error occurred",+ mimetype: "text/plain",},},code: "console.log('Hello World');",@@ -278,6 +280,8 @@ describe("RunsState Reducer", () => {timestamp,output: {channel: "marimo-error",+ data: "",+ mimetype: "text/plain",},},code: "console.log('Hello World');",diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.tsindex bf8bee26fc..34993281b4 100644--- a/frontend/src/core/cells/cells.ts+++ b/frontend/src/core/cells/cells.ts@@ -1425,7 +1425,6 @@ const {// We apply the middleware here (rather than inline in createReducerAndAtoms)// so that the document transaction middleware can import CellActions and// strictly type the dispatched actions without creating a circular dependency.-// @ts-expect-error - TODO: We should have better types for the middleware that are strictaddMiddleware(documentTransactionMiddleware);function isCellCodeHidden(state: NotebookState, cellId: CellId): boolean {diff --git a/frontend/src/utils/__tests__/createReducer.test.ts b/frontend/src/utils/__tests__/createReducer.test.tsindex 8c729dd946..1c22bc22cd 100644--- a/frontend/src/utils/__tests__/createReducer.test.ts+++ b/frontend/src/utils/__tests__/createReducer.test.ts@@ -133,7 +133,7 @@ describe("createReducerAndAtoms", () => {]);const state = { count: 0 };- const action = { type: "increment", payload: 5 };+ const action = { type: "increment", payload: 5 } as const;reducer(state, action);expect(middleware).toHaveBeenCalledWith(state, { count: 5 }, action);@@ -150,6 +150,7 @@ describe("createReducerAndAtoms", () => {const { reducer } = createReducerAndAtoms(initialState, reducers);const state = { count: 0 };+ // @ts-expect-error - we are testing thisconst newState = reducer(state, { type: "nonexistent", payload: null });expect(newState).toBe(state);diff --git a/frontend/src/utils/createReducer.ts b/frontend/src/utils/createReducer.tsindex 9e2134b97a..df50ba1acd 100644--- a/frontend/src/utils/createReducer.ts+++ b/frontend/src/utils/createReducer.ts@@ -7,12 +7,7 @@ import { Logger } from "@/utils/Logger";export type NoInfer<T> = [T][T extends any ? 0 : never];-interface ReducerAction<T> {- type: string;- payload: T;-}--type Dispatch = (action: ReducerAction<any>) => void;+type Dispatch<RH> = (action: ReducerActionOf<RH>) => void;type IfUnknown<T, Y, N> = unknown extends T ? Y : N;type ReducerHandler<State, Payload> = (state: State, payload: Payload) => State;@@ -34,6 +29,12 @@ export type DispatchedActionOf<T> = {: never;}[keyof T & string];+type ReducerActionOf<RH> = {+ [Type in keyof RH]: RH[Type] extends ReducerHandler<any, infer Payload>+ ? { type: Type; payload: Payload }+ : never;+}[keyof RH & string];+export interface ReducerCreatorResult<State,RH extends ReducerHandlers<State>,@@ -45,7 +46,7 @@ export interface ReducerCreatorResult</*** Given a dispatch function, returns an object containing all the actions.*/- createActions: (dispatch: Dispatch) => ReducerActions<RH>;+ createActions: (dispatch: Dispatch<RH>) => ReducerActions<RH>;}/**@@ -56,7 +57,7 @@ export function createReducer<RH extends ReducerHandlers<NoInfer<State>>,>(initialState: () => State, reducers: RH): ReducerCreatorResult<State, RH> {return {- reducer: (state, action: ReducerAction<RH>) => {+ reducer: (state, action: ReducerActionOf<RH>) => {state = state || initialState();if (action.type in reducers) {return reducers[action.type](state, action.payload);@@ -65,11 +66,11 @@ export function createReducer<Logger.error(`Action type ${action.type} is not defined in reducers.`);return state;},- createActions: (dispatch: Dispatch) => {+ createActions: (dispatch: Dispatch<RH>) => {const actions = {} as ReducerActions<RH>;for (const type in reducers) {(actions as any)[type] = (payload: any) => {- dispatch({ type, payload });+ dispatch({ type, payload } as any);};}return actions;@@ -77,23 +78,27 @@ export function createReducer<};}-type Middleware<State> = (+type Middleware<State, RH> = (prevState: State,newState: State,- action: ReducerAction<any>,+ action: ReducerActionOf<RH>,) => void;export function createReducerAndAtoms<State,RH extends ReducerHandlers<NoInfer<State>>,->(initialState: () => State, reducers: RH, middleware?: Middleware<State>[]) {+>(+ initialState: () => State,+ reducers: RH,+ middleware?: Middleware<State, NoInfer<RH>>[],+) {const allMiddleware = [...(middleware ?? [])];- const addMiddleware = (mw: Middleware<State>) => {+ const addMiddleware = (mw: Middleware<State, RH>) => {allMiddleware.push(mw);};const { reducer, createActions } = createReducer(initialState, reducers);- const reducerWithMiddleware = (state: State, action: ReducerAction<any>) => {+ const reducerWithMiddleware = (state: State, action: ReducerActionOf<RH>) => {try {const newState = reducer(state, action);for (const mw of allMiddleware) {@@ -120,7 +125,7 @@ export function createReducerAndAtoms<const setState = useSetAtom(valueAtom);if (options.skipMiddleware === true) {- return createActions((action: ReducerAction<any>) => {+ return createActions((action: ReducerActionOf<RH>) => {setState((state: State) => reducer(state, action));});}@@ -128,7 +133,7 @@ export function createReducerAndAtoms<if (!actionsMap.has(setState)) {actionsMap.set(setState,- createActions((action: ReducerAction<any>) => {+ createActions((action: ReducerActionOf<RH>) => {setState((state: State) => reducerWithMiddleware(state, action));}),);diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f00af7300e..b854d3517c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -14900,14 +14900,14 @@ snapshots:chai: 5.3.3tinyrainbow: 2.0.0- '@vitest/[email protected]([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))':+ '@vitest/[email protected]([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))':dependencies:'@vitest/spy': 3.2.4estree-walker: 3.0.3magic-string: 0.30.21optionalDependencies:msw: 2.12.7(@types/[email protected])([email protected])- vite: [email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])+ vite: [email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])'@vitest/[email protected]([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))':dependencies:@@ -21732,7 +21732,7 @@ snapshots:dependencies:'@types/chai': 5.2.3'@vitest/expect': 3.2.4- '@vitest/mocker': 3.2.4([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))+ '@vitest/mocker': 3.2.4([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))'@vitest/pretty-format': 3.2.4'@vitest/runner': 3.2.4'@vitest/snapshot': 3.2.4