Skip to content

Commit

Permalink
feat(typing): Adds RowCol generic
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned committed Sep 12, 2024
1 parent af7c17c commit 072f512
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions altair/vegalite/v5/schema/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ def is_color_hex(obj: Any) -> TypeIs[ColorHex]:
return bool(pattern.fullmatch(obj))


CT = TypeVar("CT")
RT = TypeVar("RT")


class RowCol(TypedDict, Generic[CT, RT], total=False):
"""
A `Generic`_ two-item ``dict``.
Parameters
----------
column: CT
row: RT
.. _Generic:
https://typing.readthedocs.io/en/latest/spec/generics.html#generics
"""

column: CT
row: RT


VegaThemes: TypeAlias = Literal[
"carbong10",
"carbong100",
Expand Down
20 changes: 20 additions & 0 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ def is_color_hex(obj: Any) -> TypeIs[ColorHex]:
pattern: re.Pattern[str] = next(it)
return bool(pattern.fullmatch(obj))
CT = TypeVar("CT")
RT = TypeVar("RT")
class RowCol(TypedDict, Generic[CT, RT], total=False):
"""
A `Generic`_ two-item ``dict``.
Parameters
----------
column: CT
row: RT
.. _Generic:
https://typing.readthedocs.io/en/latest/spec/generics.html#generics
"""
column: CT
row: RT
'''

_ChannelType = Literal["field", "datum", "value"]
Expand Down
12 changes: 12 additions & 0 deletions tools/schemapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ def title_to_type_reprs(self, *, use_concrete: bool) -> set[str]:
value = self.properties["value"]
t = value.to_type_repr(target="annotation", use_concrete=use_concrete)
tps.add(f"Value[{t}]")
elif self.is_rowcol():
row = self.properties["row"]
t = row.to_type_repr(target="annotation", use_concrete=use_concrete)
tps.add(f"RowCol[{t}, {t}]")
elif title in REMAP_TITLE:
tps.update(REMAP_TITLE[title])
elif (
Expand Down Expand Up @@ -714,6 +718,14 @@ def is_object(self) -> bool:
def is_value(self) -> bool:
return self.is_object() and self.properties.keys() == {"value"}

def is_rowcol(self) -> bool:
props = self.properties
return (
self.is_object()
and props.keys() == {"column", "row"}
and props["column"] == props["row"]
)

def is_array(self) -> bool:
return self.type == "array"

Expand Down

0 comments on commit 072f512

Please sign in to comment.