Skip to content

Commit

Permalink
feat(DRAFT): add experimental _str_as
Browse files Browse the repository at this point in the history
See vega#3427 (comment)

Not yet exposed as an option
  • Loading branch information
dangotbanned committed Jul 6, 2024
1 parent f95be1a commit 31bad3f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,15 +732,27 @@ def _parse_when(


def _parse_literal(val: Any, *, str_as_lit: bool) -> dict[str, Any]:
if isinstance(val, str) and not str_as_lit:
return utils.parse_shorthand(val)
if isinstance(val, str):
return _str_as(val, "value" if str_as_lit else "field")
elif _is_one_or_seq_literal_value(val):
return {"value": val}
else:
msg = f"Expected one or more literal values, but got: {type(val).__name__!r}"
raise TypeError(msg)


def _str_as(val: str, to: Literal["datum", "field", "value"], /):
if to == "value":
return value(val)
elif to == "field":
return utils.parse_shorthand(val)
elif to == "datum":
return _ConditionClosed(test=_expr_core.GetAttrExpression(to, val))
else:
msg = f"Unsupported str_as={to!r}"
raise NotImplementedError(msg)


def _parse_then(
statement: _StatementOrLiteralType,
kwargs: dict[str, Any],
Expand Down

0 comments on commit 31bad3f

Please sign in to comment.