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: remove NaN from number plugin (#7364)
Myles Scolnick7a4da5248a23
diff --git a/frontend/src/hooks/useDebounce.ts b/frontend/src/hooks/useDebounce.tsindex 1229443b90..af92d756bd 100644--- a/frontend/src/hooks/useDebounce.ts+++ b/frontend/src/hooks/useDebounce.ts@@ -74,9 +74,7 @@ export function useDebounceControlledState<T>(opts: {return {value: internalValue,debouncedValue,- onChange: (value: T) => {- setInternalValue(value);- },+ onChange: setInternalValue,};}diff --git a/frontend/src/plugins/impl/NumberPlugin.tsx b/frontend/src/plugins/impl/NumberPlugin.tsxindex 6c6e98116c..b31bc339bd 100644--- a/frontend/src/plugins/impl/NumberPlugin.tsx+++ b/frontend/src/plugins/impl/NumberPlugin.tsx@@ -55,16 +55,20 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {id = "test-id";}+ const initialValue = withoutNaN(props.value);+// Create a debounced value of 200const { value, onChange } = useDebounceControlledState({- initialValue: props.value,+ initialValue: initialValue,delay: 200,disabled: !props.debounce,- onChange: (v) => {- props.setValue(v);- },+ onChange: props.setValue,});+ const handleChange = (newValue: number) => {+ onChange(withoutNaN(newValue));+ };+return (<Labeled label={props.label} id={id} fullWidth={props.fullWidth}><NumberField@@ -72,9 +76,12 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {className={cn("min-w-[3em]", props.fullWidth && "w-full")}minValue={props.start ?? undefined}maxValue={props.stop ?? undefined}- value={value ?? undefined}+ // This needs to be `?? NaN` since `?? undefined` makes uncontrolled component+ // and can lead to leaving the old value in forms (https://github.com/marimo-team/marimo/issues/7352)+ // We out NaNs later+ value={value ?? NaN}step={props.step}- onChange={onChange}+ onChange={handleChange}id={id}aria-label={props.label || "Number input"}isDisabled={props.disabled}@@ -82,3 +89,10 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {</Labeled>);};++function withoutNaN(value: number | null | undefined): number | null {+ if (value == null || Number.isNaN(value)) {+ return null;+ }+ return value;+}diff --git a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsxindex a9197b7cc4..f5a1ba4b45 100644--- a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx+++ b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx@@ -65,4 +65,413 @@ describe("NumberPlugin", () => {rerender(plugin.render(resetProps));expect(input.value).toBe("0");});++ it("handles null values correctly", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: null,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ // Null values should render as empty (NaN is used internally)+ expect(input.value).toBe("");+ });++ it("handles NaN values correctly", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: NaN,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ // NaN values should be filtered to null and render as empty+ expect(input.value).toBe("");+ });++ it("handles debounce correctly", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: true,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole, rerender } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ // setValue should not be called yet+ expect(setValue).not.toHaveBeenCalled();++ // Update the value prop to simulate a change+ const updatedProps = { ...props, value: 8 };+ rerender(plugin.render(updatedProps));++ // setValue should not be called immediately due to debounce+ expect(setValue).not.toHaveBeenCalled();++ // Advance timers by debounce delay (200ms)+ act(() => {+ vi.advanceTimersByTime(200);+ });++ // Now verify the component can handle debounced state+ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;+ expect(input.value).toBe("8");+ });++ it("handles debounce disabled (immediate updates)", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole, rerender } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ // setValue should not be called yet+ expect(setValue).not.toHaveBeenCalled();++ // Update the value prop to simulate a change+ const updatedProps = { ...props, value: 8 };+ rerender(plugin.render(updatedProps));++ // With debounce disabled, the value should update immediately without delay+ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;+ expect(input.value).toBe("8");++ // No need to advance timers since debounce is disabled+ expect(setValue).not.toHaveBeenCalled(); // setValue is only called when user interacts, not on prop changes+ });++ it("respects min and max values", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole, rerender } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ expect(input.value).toBe("5");++ // Test that values above max are clamped to max+ const aboveMaxProps = { ...props, value: 15 };+ rerender(plugin.render(aboveMaxProps));+ expect(input.value).toBe("10"); // NumberField clamps to max value++ // Test that values below min are clamped to min+ const belowMinProps = { ...props, value: -5 };+ rerender(plugin.render(belowMinProps));+ expect(input.value).toBe("0"); // NumberField clamps to min value+ });++ it("handles disabled state", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ disabled: true,+ },+ functions: {},+ };++ const { getByRole } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ expect(input.disabled).toBe(true);+ });++ it("handles step values", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 0.5,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ expect(input).toBeTruthy();+ expect(input.value).toBe("5");+ });++ it("renders with custom label", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: "Custom Label",+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Custom Label",+ }) as HTMLInputElement;++ expect(input).toBeTruthy();+ });++ it("handles transitions from null to number and back", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: null,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: false,+ },+ functions: {},+ };++ const { getByRole, rerender } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;++ expect(input.value).toBe("");++ // Update to a number+ const updatedProps = { ...props, value: 5 };+ rerender(plugin.render(updatedProps));+ expect(input.value).toBe("5");++ // Back to null+ const nullProps = { ...props, value: null };+ rerender(plugin.render(nullProps));+ expect(input.value).toBe("");+ });++ it("handles fullWidth prop", () => {+ const plugin = new NumberPlugin();+ const host = document.createElement("div");+ const setValue = vi.fn();++ const props: IPluginProps<+ number | null,+ z.infer<(typeof plugin)["validator"]>+ > = {+ host,+ value: 5,+ setValue,+ data: {+ start: 0,+ stop: 10,+ step: 1,+ label: null,+ debounce: false,+ fullWidth: true,+ },+ functions: {},+ };++ const { container } = render(plugin.render(props));++ act(() => {+ vi.advanceTimersByTime(0);+ });++ // Check that the NumberField has the full width class+ const numberField = container.querySelector(+ '[data-testid="marimo-plugin-number-input"]',+ );+ expect(numberField?.classList.contains("w-full")).toBe(true);+ });});