Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve Then copy TypeError #3553

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ def _deep_copy(obj: Any, by_ref: set[str]) -> Any: ...
def _deep_copy(obj: _CopyImpl | Any, by_ref: set[str]) -> _CopyImpl | Any:
copy = partial(_deep_copy, by_ref=by_ref)
if isinstance(obj, SchemaBase):
if copier := getattr(obj, "__deepcopy__", None):
with debug_mode(False):
return copier(obj)
args = (copy(arg) for arg in obj._args)
kwds = {k: (copy(v) if k not in by_ref else v) for k, v in obj._kwds.items()}
with debug_mode(False):
Expand Down
3 changes: 3 additions & 0 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,9 @@ def to_dict(self, *args: Any, **kwds: Any) -> _Conditional[_C]: # type: ignore[
m = super().to_dict(*args, **kwds)
return _Conditional(condition=m["condition"])

def __deepcopy__(self, memo: Any) -> Self:
return type(self)(_Conditional(condition=_deepcopy(self.condition)))


class ChainedWhen(_BaseWhen):
"""
Expand Down
21 changes: 21 additions & 0 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,27 @@ def test_when_condition_parity(
assert chart_condition == chart_when


def test_when_then_interactive() -> None:
"""Copy-related regression found in https://github.com/vega/altair/pull/3394#issuecomment-2302995453."""
source = "https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/movies.json"
predicate = (alt.datum.IMDB_Rating == None) | ( # noqa: E711
alt.datum.Rotten_Tomatoes_Rating == None # noqa: E711
)

chart = (
alt.Chart(source)
.mark_point(invalid=None)
.encode(
x="IMDB_Rating:Q",
y="Rotten_Tomatoes_Rating:Q",
color=alt.when(predicate).then(alt.value("grey")), # type: ignore[arg-type]
)
)
assert chart.interactive()
assert chart.copy()
assert chart.to_dict()


def test_selection_to_dict():
brush = alt.selection_interval()

Expand Down
3 changes: 3 additions & 0 deletions tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ def _deep_copy(obj: Any, by_ref: set[str]) -> Any: ...
def _deep_copy(obj: _CopyImpl | Any, by_ref: set[str]) -> _CopyImpl | Any:
copy = partial(_deep_copy, by_ref=by_ref)
if isinstance(obj, SchemaBase):
if copier := getattr(obj, "__deepcopy__", None):
with debug_mode(False):
return copier(obj)
args = (copy(arg) for arg in obj._args)
kwds = {k: (copy(v) if k not in by_ref else v) for k, v in obj._kwds.items()}
with debug_mode(False):
Expand Down