Skip to content

Commit

Permalink
Make ChartType and is_chart_type public. Use typing.get_args to reduc…
Browse files Browse the repository at this point in the history
…e duplication of list of charts
  • Loading branch information
binste committed May 20, 2024
1 parent 8dd2cde commit a4de6d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 deletions altair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"Categorical",
"Chart",
"ChartDataType",
"ChartType",
"Color",
"ColorDatum",
"ColorDef",
Expand Down Expand Up @@ -579,6 +580,7 @@
"expr",
"graticule",
"hconcat",
"is_chart_type",
"jupyter",
"layer",
"limit_rows",
Expand Down
25 changes: 10 additions & 15 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import itertools
import sys
import pathlib
import typing
from typing import cast, List, Optional, Any, Iterable, Union, Literal, IO

# Have to rename it here as else it overlaps with schema.core.Type and schema.core.Dict
Expand Down Expand Up @@ -236,9 +237,9 @@ def to_dict(self) -> TypingDict[str, Union[str, dict]]:
return {"expr": self.name}
elif self.param_type == "selection":
return {
"param": self.name.to_dict()
if hasattr(self.name, "to_dict")
else self.name
"param": (
self.name.to_dict() if hasattr(self.name, "to_dict") else self.name
)
}
else:
raise ValueError(f"Unrecognized parameter type: {self.param_type}")
Expand Down Expand Up @@ -4096,22 +4097,16 @@ def sphere() -> core.SphereGenerator:
return core.SphereGenerator(sphere=True)


_ChartType = Union[
ChartType = Union[
Chart, RepeatChart, ConcatChart, HConcatChart, VConcatChart, FacetChart, LayerChart
]


def _is_chart_type(obj: Any) -> TypeIs[_ChartType]:
"""Return `True` if the object is basic or compound `Chart`."""
def is_chart_type(obj: Any) -> TypeIs[ChartType]:
"""Return `True` if the object is an Altair chart. This can be a basic chart
but also a repeat, concat, or facet chart.
"""
return isinstance(
obj,
(
Chart,
RepeatChart,
ConcatChart,
HConcatChart,
VConcatChart,
FacetChart,
LayerChart,
),
typing.get_args(ChartType),
)

0 comments on commit a4de6d1

Please sign in to comment.