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: handle list-type tooltip encoding in altair chart (#9167) (#9175)
Myles Scolnick386cba687fa9
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.pyindex 59abbc353f..ec4c3b6d4c 100644--- a/marimo/_plugins/ui/_impl/altair_chart.py+++ b/marimo/_plugins/ui/_impl/altair_chart.py@@ -69,6 +69,8 @@ def _has_binning(spec: VegaSpec) -> bool:if "encoding" not in spec:return Falsefor encoding in spec["encoding"].values():+ if isinstance(encoding, list):+ continueif "bin" in encoding:return Truereturn False@@ -85,6 +87,8 @@ def _get_binned_fields(spec: VegaSpec) -> dict[str, Any]:return binned_fieldsfor encoding in spec["encoding"].values():+ if isinstance(encoding, list):+ continueif encoding.get("bin"):# Get the field namefield = encoding.get("field")diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.pyindex cf80d89170..bc4e20bbb3 100644--- a/tests/_plugins/ui/_impl/test_altair_chart.py+++ b/tests/_plugins/ui/_impl/test_altair_chart.py@@ -993,6 +993,62 @@ def test_get_binned_fields() -> None:assert binned_fields["values"]["extent"] == [0, 50][email protected](not HAS_DEPS, reason="optional dependencies not installed")+def test_get_binned_fields_with_tooltip_list() -> None:+ """Test _get_binned_fields handles tooltip encoded as a list (#9167)."""+ import altair as alt++ # Tooltip as a list should not crash _get_binned_fields or _has_binning+ spec = _parse_spec(+ alt.Chart(pd.DataFrame({"x": range(10), "y": range(10)}))+ .mark_point()+ .encode(+ x="x",+ y="y",+ tooltip=["x", "y"],+ )+ )+ assert _get_binned_fields(spec) == {}+ assert _has_binning(spec) is False++ # Tooltip list with binned fields on other channels+ spec_with_bins = _parse_spec(+ alt.Chart(pd.DataFrame({"x": range(10), "y": range(10)}))+ .mark_bar()+ .encode(+ x=alt.X("x", bin=True),+ y="count()",+ tooltip=["x", alt.Tooltip("y", format=".2f")],+ )+ )+ binned = _get_binned_fields(spec_with_bins)+ assert "x" in binned+ assert _has_binning(spec_with_bins) is True++[email protected](not HAS_DEPS, reason="optional dependencies not installed")+def test_altair_chart_with_tooltip_list() -> None:+ """Smoke test: altair_chart with tooltip list should not error (#9167)."""+ import altair as alt++ df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": ["x", "y", "z"]})+ chart = (+ alt.Chart(df)+ .mark_arc()+ .encode(+ theta="a",+ color="c",+ tooltip=[+ "c",+ alt.Tooltip("a", format=".2f"),+ alt.Tooltip("b", format="$.2f"),+ ],+ )+ )+ # This should not raise AttributeError+ altair_chart(chart)++@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")def test_has_geoshape() -> None:import altair as alt