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 union for narwhals files (#9156)

Shahmir Varqha58543f6b5eee
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index ec4c3b6d4c..c5edff4609 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -10,6 +10,7 @@ from typing import (
Final,
Literal,
TypeAlias,
+ Union,
cast,
)
@@ -56,9 +57,10 @@ VegaSpec = dict[str, Any]
RowOrientedData = list[dict[str, Any]]
ColumnOrientedData = dict[str, list[Any]]
-ChartDataType = (
- IntoDataFrame | IntoLazyFrame | RowOrientedData | ColumnOrientedData
-)
+# Use Union[] instead of X | Y — see altair_transformer.py for rationale.
+ChartDataType = Union[
+ IntoDataFrame, IntoLazyFrame, RowOrientedData, ColumnOrientedData
+]
# Union of all possible chart types
AltairChartType: TypeAlias = "altair.vegalite.v6.api.ChartType"
diff --git a/marimo/_plugins/ui/_impl/charts/altair_transformer.py b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
index bcf403dc1f..000b053f3b 100644
--- a/marimo/_plugins/ui/_impl/charts/altair_transformer.py
+++ b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
@@ -2,7 +2,7 @@
from __future__ import annotations
import base64
-from typing import Any, Literal, TypedDict
+from typing import Any, Literal, TypedDict, Union
import narwhals.stable.v2 as nw
from narwhals.typing import IntoDataFrame
@@ -22,8 +22,11 @@ from marimo._utils.narwhals_utils import (
LOGGER = _loggers.marimo_logger()
-Data = dict[Any, Any] | IntoDataFrame | nw.DataFrame[Any]
-_DataType = dict[Any, Any] | IntoDataFrame | nw.DataFrame[Any]
+# Use Union[] instead of X | Y for narwhals types — in older narwhals versions
+# (e.g. 2.6.0 shipped in Pyodide), IntoDataFrame is a string at runtime and
+# str does not support the | operator.
+Data = Union[dict[Any, Any], IntoDataFrame, nw.DataFrame[Any]]
+_DataType = Union[dict[Any, Any], IntoDataFrame, nw.DataFrame[Any]]
class _JsonFormatDict(TypedDict):
diff --git a/marimo/_plugins/ui/_impl/data_editor.py b/marimo/_plugins/ui/_impl/data_editor.py
index a0b2150fa3..eea0660936 100644
--- a/marimo/_plugins/ui/_impl/data_editor.py
+++ b/marimo/_plugins/ui/_impl/data_editor.py
@@ -11,6 +11,7 @@ from typing import (
Final,
Literal,
TypedDict,
+ Union,
cast,
)
@@ -111,11 +112,13 @@ def experimental_data_editor(
return data_editor(*args, **kwargs)
+# Use Union[] instead of X | Y in class base — see altair_transformer.py
+# for rationale.
@mddoc
class data_editor(
UIElement[
DataEdits,
- RowOrientedData | ColumnOrientedData | IntoDataFrame,
+ Union[RowOrientedData, ColumnOrientedData, IntoDataFrame],
]
):
"""A data editor component for editing tabular data.
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
index b8335fdf8f..7ae324f013 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
@@ -4,12 +4,13 @@ from __future__ import annotations
import abc
from dataclasses import dataclass
from enum import Enum
-from typing import Any, Generic, Literal, TypeVar
+from typing import Any, Generic, Literal, TypeVar, Union
from narwhals.typing import IntoDataFrame, IntoLazyFrame
# Could be a DataFrame from pandas, polars, pyarrow, DataFrameProtocol, etc.
-DataFrameType = IntoDataFrame | IntoLazyFrame
+# Use Union[] instead of X | Y — see altair_transformer.py for rationale.
+DataFrameType = Union[IntoDataFrame, IntoLazyFrame]
ColumnId = str
ColumnIds = list[ColumnId]
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 057d8ef51f..f7dc0feecb 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -9,6 +9,7 @@ from typing import (
Any,
Final,
Literal,
+ Union,
cast,
)
@@ -261,11 +262,13 @@ def _filter_valid_columns(
)
+# Use Union[] instead of X | Y in class base — see altair_transformer.py
+# for rationale.
@mddoc
class table(
UIElement[
- list[str] | list[int] | list[dict[str, Any]],
- list[JSONType] | IntoDataFrame | list[TableCell],
+ Union[list[str], list[int], list[dict[str, Any]]],
+ Union[list[JSONType], IntoDataFrame, list[TableCell]],
]
):
"""A table component with selectable rows.
diff --git a/marimo/_plugins/ui/_impl/utils/dataframe.py b/marimo/_plugins/ui/_impl/utils/dataframe.py
index 52bfea0fc9..f44a40b7a3 100644
--- a/marimo/_plugins/ui/_impl/utils/dataframe.py
+++ b/marimo/_plugins/ui/_impl/utils/dataframe.py
@@ -1,7 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from typing import Any, TypeVar
+from typing import Any, TypeVar, Union
from narwhals.typing import IntoDataFrame
@@ -64,13 +64,14 @@ Numeric = int | float
ListOrTuple = list[T] | tuple[T, ...]
-TableData = (
- list[JSONType]
- | ListOrTuple[str | int | float | bool | MIME | None]
- | ListOrTuple[dict[str, JSONType]]
- | dict[str, ListOrTuple[JSONType]]
- | IntoDataFrame
-)
+# Use Union[] instead of X | Y — see altair_transformer.py for rationale.
+TableData = Union[
+ list[JSONType],
+ ListOrTuple[Union[str, int, float, bool, MIME, None]],
+ ListOrTuple[dict[str, JSONType]],
+ dict[str, ListOrTuple[JSONType]],
+ IntoDataFrame,
+]
def download_as(
diff --git a/tests/_plugins/ui/_impl/utils/test_dataframe_utils.py b/tests/_plugins/ui/_impl/utils/test_dataframe_utils.py
index 1468adcb33..b6a2a5c0bd 100644
--- a/tests/_plugins/ui/_impl/utils/test_dataframe_utils.py
+++ b/tests/_plugins/ui/_impl/utils/test_dataframe_utils.py
@@ -93,3 +93,32 @@ def test_get_table_manager() -> None:
def test_get_default_csv_encoding():
assert get_default_csv_encoding() == DEFAULT_CSV_ENCODING
+
+
+def test_union_tolerates_string_type_aliases() -> None:
+ """Verify that Union[] handles string-valued type aliases (narwhals compat).
+
+ In narwhals <2.9.0 (e.g. 2.6.0, shipped in Pyodide), types like
+ IntoDataFrame are plain strings at runtime:
+ IntoDataFrame: TypeAlias = "NativeDataFrame"
+
+ The X | Y syntax raises TypeError when one operand is a string, while
+ Union[X, Y] gracefully wraps it in a ForwardRef. This test guards
+ against regressions if someone converts Union[] back to X | Y in the
+ affected module-level type aliases.
+
+ See: https://github.com/marimo-team/marimo/issues/9152
+ """
+ from typing import Any, ForwardRef, Union
+
+ fake_into_df = "NativeDataFrame" # simulates narwhals 2.6.0
+
+ # Union[] works with string operands
+ result = Union[dict[Any, Any], fake_into_df] # type: ignore[valid-type]
+ args = result.__args__
+ assert dict[Any, Any] in args
+ assert ForwardRef("NativeDataFrame") in args
+
+ # X | Y fails with string operands — this is the bug we're guarding against
+ with pytest.raises(TypeError, match="unsupported operand type"):
+ dict[Any, Any] | fake_into_df # type: ignore[operator]