Skip to content

Commit

Permalink
refactor: Reuse literals, add _typing dicts to _config.__all__
Browse files Browse the repository at this point in the history
Trying to make this easier to maintain in the future

vega#3536 (comment)
  • Loading branch information
dangotbanned committed Sep 16, 2024
1 parent ffb3214 commit aaffebd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
8 changes: 5 additions & 3 deletions altair/vegalite/v5/schema/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"MultiPolygonKwds",
"NumberLocaleKwds",
"OverlayMarkDefKwds",
"PaddingKwds",
"PointKwds",
"PointSelectionConfigKwds",
"PointSelectionConfigWithoutTypeKwds",
Expand All @@ -69,6 +70,7 @@
"RangeConfigKwds",
"RectConfigKwds",
"ResolveKwds",
"RowColKwds",
"ScaleConfigKwds",
"ScaleInvalidDataConfigKwds",
"ScaleResolveMapKwds",
Expand Down Expand Up @@ -7753,11 +7755,11 @@ class ThemeConfig(TypedDict, total=False):
documentation.
"""

align: RowCol[LayoutAlign_T] | LayoutAlign_T
align: RowColKwds[LayoutAlign_T] | LayoutAlign_T
autosize: AutoSizeParamsKwds | AutosizeType_T
background: ColorHex | ColorName_T
bounds: Literal["full", "flush"]
center: bool | RowCol[bool]
center: bool | RowColKwds[bool]
config: ConfigKwds
description: str
height: float | StepKwds | Literal["container"]
Expand All @@ -7766,7 +7768,7 @@ class ThemeConfig(TypedDict, total=False):
params: Sequence[VariableParameterKwds | TopLevelSelectionParameterKwds]
projection: ProjectionKwds
resolve: ResolveKwds
spacing: float | RowCol[float]
spacing: float | RowColKwds[float]
title: str | Sequence[str] | TitleParamsKwds
usermeta: Map
view: ViewBackgroundKwds
Expand Down
4 changes: 2 additions & 2 deletions altair/vegalite/v5/schema/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"ProjectionType_T",
"RangeEnum_T",
"ResolveMode_T",
"RowCol",
"RowColKwds",
"ScaleInterpolateEnum_T",
"ScaleType_T",
"SelectionResolution_T",
Expand Down Expand Up @@ -172,7 +172,7 @@ def is_color_hex(obj: Any) -> TypeIs[ColorHex]:
return bool(pattern.fullmatch(obj))


class RowCol(TypedDict, Generic[T], total=False):
class RowColKwds(TypedDict, Generic[T], total=False):
"""
A `Generic`_ two-item ``dict``.
Expand Down
12 changes: 7 additions & 5 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ class {name}(TypedDict{metaclass_kwds}):{comment}
'''
ENCODE_KWDS: Literal["EncodeKwds"] = "EncodeKwds"
THEME_CONFIG: Literal["ThemeConfig"] = "ThemeConfig"
PADDING_KWDS: Literal["PaddingKwds"] = "PaddingKwds"
ROW_COL_KWDS: Literal["RowColKwds"] = "RowColKwds"
ENCODE_KWDS_SUMMARY: Final = (
"Encoding channels map properties of the data to visual properties of the chart."
)
Expand Down Expand Up @@ -368,7 +370,7 @@ def is_color_hex(obj: Any) -> TypeIs[ColorHex]:
class RowCol(TypedDict, Generic[T], total=False):
class RowColKwds(TypedDict, Generic[T], total=False):
"""
A `Generic`_ two-item ``dict``.
Expand Down Expand Up @@ -944,15 +946,15 @@ def generate_config_typed_dicts(fp: Path, /) -> Iterator[str]:
}

SchemaInfo._remap_title.update(
{"HexColor": ("ColorHex",), "Padding": ("float", "PaddingKwds")}
{"HexColor": ("ColorHex",), "Padding": ("float", PADDING_KWDS)}
)
SchemaInfo._remap_title.update((k, (f"{k}{KWDS}",)) for k in relevant)
config_sub: Iterator[str] = (
generate_typed_dict(info, name=f"{info.title}{KWDS}")
for info in relevant.values()
)
config_sub_names = (f"{nm}{KWDS}" for nm in relevant)
yield f"__all__ = {[*config_sub_names, THEME_CONFIG]}\n\n"
yield f"__all__ = {[*config_sub_names, PADDING_KWDS, ROW_COL_KWDS, THEME_CONFIG]}\n\n"
yield "\n".join(config_sub)
yield generate_typed_dict(
SchemaInfo.from_refname(TOP_LEVEL, root),
Expand Down Expand Up @@ -1094,8 +1096,8 @@ def vegalite_main(skip_download: bool = False) -> None:
"Value",
"ColorHex",
"is_color_hex",
"RowCol",
"PaddingKwds",
ROW_COL_KWDS,
PADDING_KWDS,
header=HEADER,
extra=TYPING_EXTRA,
)
Expand Down
4 changes: 2 additions & 2 deletions tools/schemapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def to_type_repr( # noqa: C901
raise ValueError(msg)

if use_concrete:
if tps >= {"ColorHex", "ColorName_T", "str"}:
if tps >= {"ColorHex", TypeAliasTracer.fmt.format("ColorName"), "str"}:
# HACK: Remove regular `str` if HEX & CSS color codes are present as well
tps.discard("str")
elif len(tps) == 0 and as_str:
Expand Down Expand Up @@ -577,7 +577,7 @@ def title_to_type_reprs(self, *, use_concrete: bool) -> set[str]:
elif self.is_rowcol():
row = self.properties["row"]
t = row.to_type_repr(target="annotation", use_concrete=use_concrete)
tps.add(f"RowCol[{t}]")
tps.add(f"RowColKwds[{t}]")
elif title in REMAP_TITLE:
tps.update(REMAP_TITLE[title])
elif (
Expand Down

0 comments on commit aaffebd

Please sign in to comment.