Skip to content

Commit

Permalink
test: Update test_when_typing
Browse files Browse the repository at this point in the history
Added a lot of notes here, since I was surpised `Then` would not be allowed in the cases I had planned originally
  • Loading branch information
dangotbanned committed Sep 15, 2024
1 parent e470a98 commit f49c9f5
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import altair as alt
from altair import SchemaBase
from altair.utils.schemapi import Optional, Undefined
from altair.utils.schemapi import Optional, SchemaValidationError, Undefined
from tests import skip_requires_vl_convert, slow

try:
Expand All @@ -35,6 +35,7 @@

if TYPE_CHECKING:
from altair import Color, ColorDatum, ColorValue, Then
from altair.vegalite.v5.api import _Conditional, _Conditions
from altair.vegalite.v5.schema._typing import Map

ibis.set_backend("polars")
Expand Down Expand Up @@ -653,28 +654,42 @@ def test_when_multiple_fields():

def test_when_typing(cars) -> None:
chart = alt.Chart(cars).mark_rect()
predicate = alt.datum.Weight_in_lbs >= 3500
statement = alt.value("black")
default = alt.value("white")

then: alt.Then[_Conditions] = alt.when(predicate).then(statement)
otherwise: _Conditional[_Conditions] = then.otherwise(default)
condition: Map = alt.condition(predicate, statement, default)

# NOTE: both `condition()` and `when-then-otherwise` are allowed in these three locations
chart.encode(
color=condition,
x=alt.X("Cylinders:N").axis(labelColor=condition),
y=alt.Y("Origin:N", axis=alt.Axis(tickColor=condition)),
).to_dict()

color_then = alt.when(alt.datum.Weight_in_lbs >= 3500).then(alt.value("black"))
color = color_then.otherwise(alt.value("white"))
color_condition = alt.condition(
alt.datum.Weight_in_lbs >= 3500, alt.value("black"), alt.value("white")
)
chart.encode(
color=otherwise,
x=alt.X("Cylinders:N").axis(labelColor=otherwise),
y=alt.Y("Origin:N", axis=alt.Axis(tickColor=otherwise)),
).to_dict()

chart_condition = chart.encode( # noqa: F841
x=alt.X("Cylinders:N").axis(labelColor=color_condition),
y=alt.Y("Origin:N", axis=alt.Axis(tickColor=color_condition)),
color=color_condition,
)
chart_otherwise = chart.encode( # noqa: F841
x=alt.X("Cylinders:N").axis(labelColor=color),
y=alt.Y("Origin:N", axis=alt.Axis(tickColor=color)),
color=color,
)
chart_then = chart.encode( # noqa: F841
x=alt.X("Cylinders:N").axis(labelColor=color_then),
y=alt.Y("Origin:N", axis=alt.Axis(tickColor=color_then)),
color=color_then,
)
with pytest.raises(SchemaValidationError):
# NOTE: `when-then` is allowed as an encoding, but not as a `ConditionalAxisProperty`
# The latter fails validation since it does not have a default `value`
chart.encode(
color=then,
x=alt.X("Cylinders:N").axis(labelColor=then), # type: ignore[call-overload]
y=alt.Y("Origin:N", axis=alt.Axis(labelColor=then)), # type: ignore[arg-type]
)

# NOTE: Passing validation then requires an `.otherwise()` **only** for the property cases
chart.encode(
color=then,
x=alt.X("Cylinders:N").axis(labelColor=otherwise),
y=alt.Y("Origin:N", axis=alt.Axis(labelColor=otherwise)),
).to_dict()


@pytest.mark.parametrize(
Expand Down

0 comments on commit f49c9f5

Please sign in to comment.