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.

fix: add alt.LayeredChart to types (#4966)

Myles Scolnick3059058e92d8
diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py
index 543a46e7f5..c2d10b73f0 100644
--- a/marimo/_output/formatters/altair_formatters.py
+++ b/marimo/_output/formatters/altair_formatters.py
@@ -2,7 +2,7 @@
from __future__ import annotations
import json
-from typing import TYPE_CHECKING, Literal
+from typing import TYPE_CHECKING, Literal, Union
from marimo._config.config import Theme
from marimo._messaging.mimetypes import KnownMimeType, MimeBundleOrTuple
@@ -34,7 +34,9 @@ class AltairFormatter(FormatterFactory):
register_transformers()
@formatting.formatter(altair.TopLevelMixin)
- def _show_chart(chart: altair.Chart) -> tuple[KnownMimeType, str]:
+ def _show_chart(
+ chart: Union[altair.Chart, altair.LayerChart],
+ ) -> tuple[KnownMimeType, str]:
import altair as alt
# Try to get the _repr_mimebundle_ method from the chart
@@ -104,7 +106,9 @@ class AltairFormatter(FormatterFactory):
# This is only needed since it seems that altair does not
# handle this internally.
# https://github.com/marimo-team/marimo/issues/2302
-def _apply_embed_options(chart: altair.Chart) -> altair.Chart:
+def _apply_embed_options(
+ chart: Union[altair.Chart, altair.LayerChart],
+) -> Union[altair.Chart, altair.LayerChart]:
import altair as alt
# Respect user-set embed options
@@ -124,7 +128,7 @@ def _apply_embed_options(chart: altair.Chart) -> altair.Chart:
def chart_to_json(
- chart: altair.Chart,
+ chart: Union[altair.Chart, altair.LayerChart],
spec_format: Literal["vega", "vega-lite"] = "vega-lite",
validate: bool = True,
) -> str:
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index 90c0382d16..c0d94dac33 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -294,7 +294,7 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
def __init__(
self,
- chart: altair.Chart,
+ chart: Union[altair.Chart, altair.LayerChart],
chart_selection: Literal["point"] | Literal["interval"] | bool = True,
legend_selection: list[str] | bool = True,
*,
@@ -420,7 +420,7 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
@staticmethod
def _get_dataframe_from_chart(
- chart: altair.Chart,
+ chart: Union[altair.Chart, altair.LayerChart],
) -> Optional[ChartDataType]:
if not isinstance(chart.data, str):
return cast(ChartDataType, chart.data)
@@ -578,7 +578,9 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
raise RuntimeError("Setting the value of a UIElement is not allowed.")
-def maybe_make_full_width(chart: altair.Chart) -> altair.Chart:
+def maybe_make_full_width(
+ chart: Union[altair.Chart, altair.LayerChart],
+) -> Union[altair.Chart, altair.LayerChart]:
import altair
try:
@@ -596,11 +598,16 @@ def maybe_make_full_width(chart: altair.Chart) -> altair.Chart:
return chart
-def _has_selection_param(chart: altair.Chart) -> bool:
+def _has_selection_param(
+ chart: Union[altair.Chart, altair.LayerChart],
+) -> bool:
import altair as alt
+ if not hasattr(chart, "params"):
+ return False
+
try:
- for param in chart.params:
+ for param in chart.params: # type: ignore
try:
if isinstance(
param,
@@ -615,11 +622,16 @@ def _has_selection_param(chart: altair.Chart) -> bool:
return False
-def _has_legend_param(chart: altair.Chart) -> bool:
+def _has_legend_param(
+ chart: Union[altair.Chart, altair.LayerChart],
+) -> bool:
import altair as alt
+ if not hasattr(chart, "params"):
+ return False
+
try:
- for param in chart.params:
+ for param in chart.params: # type: ignore
try:
if isinstance(
param,