Skip to content

Commit

Permalink
test: Fix deprecation warning from ipywidgets
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned committed Aug 25, 2024
1 parent bf7fc29 commit 01a1b13
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
51 changes: 26 additions & 25 deletions tests/test_jupyter_chart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from importlib.metadata import version as importlib_version

import pandas as pd
import pytest
from packaging.version import Version

import altair as alt
from vega_datasets import data
Expand All @@ -17,6 +20,10 @@
else:
jupyter_chart = None # type: ignore

skip_requires_anywidget = pytest.mark.skipif(
not has_anywidget, reason="anywidget not importable"
)


try:
import vegafusion # type: ignore # noqa: F401
Expand All @@ -25,13 +32,23 @@
except ImportError:
transformers = ["default"]

param_transformers = pytest.mark.parametrize("transformer", transformers)


if Version(importlib_version("ipywidgets")) < Version("8.1.4"):
# See https://github.com/vega/altair/issues/3234#issuecomment-2268515312
_filterwarn = pytest.mark.filterwarnings(
"ignore:Deprecated in traitlets 4.1.*:DeprecationWarning"
)
jupyter_marks: pytest.MarkDecorator = skip_requires_anywidget(
_filterwarn(param_transformers)
)
else:
jupyter_marks = skip_requires_anywidget(param_transformers)

@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@pytest.mark.parametrize("transformer", transformers)
def test_chart_with_no_interactivity(transformer):
if not has_anywidget:
pytest.skip("anywidget not importable; skipping test")

@jupyter_marks
def test_chart_with_no_interactivity(transformer):
with alt.data_transformers.enable(transformer):
source = pd.DataFrame(
{
Expand All @@ -56,12 +73,8 @@ def test_chart_with_no_interactivity(transformer):
assert len(widget.params.trait_values()) == 0


@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@pytest.mark.parametrize("transformer", transformers)
@jupyter_marks
def test_interval_selection_example(transformer):
if not has_anywidget:
pytest.skip("anywidget not importable; skipping test")

with alt.data_transformers.enable(transformer):
source = data.cars()
brush = alt.selection_interval(name="interval")
Expand Down Expand Up @@ -128,12 +141,8 @@ def test_interval_selection_example(transformer):
assert selection.store == store


@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@pytest.mark.parametrize("transformer", transformers)
@jupyter_marks
def test_index_selection_example(transformer):
if not has_anywidget:
pytest.skip("anywidget not importable; skipping test")

with alt.data_transformers.enable(transformer):
source = data.cars()
brush = alt.selection_point(name="index")
Expand Down Expand Up @@ -192,12 +201,8 @@ def test_index_selection_example(transformer):
assert selection.store == store


@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@pytest.mark.parametrize("transformer", transformers)
@jupyter_marks
def test_point_selection(transformer):
if not has_anywidget:
pytest.skip("anywidget not importable; skipping test")

with alt.data_transformers.enable(transformer):
source = data.cars()
brush = alt.selection_point(name="point", encodings=["color"], bind="legend")
Expand Down Expand Up @@ -259,12 +264,8 @@ def test_point_selection(transformer):
assert selection.store == store


@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@pytest.mark.parametrize("transformer", transformers)
@jupyter_marks
def test_param_updates(transformer):
if not has_anywidget:
pytest.skip("anywidget not importable; skipping test")

with alt.data_transformers.enable(transformer):
source = data.cars()
size_param = alt.param(
Expand Down
21 changes: 17 additions & 4 deletions tests/vegalite/v5/test_renderers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests of various renderers."""

import json
from importlib.metadata import version as importlib_version

import pytest
from packaging.version import Version

import altair.vegalite.v5 as alt

Expand All @@ -18,6 +20,20 @@
anywidget = None # type: ignore


skip_requires_anywidget = pytest.mark.skipif(
not anywidget, reason="anywidget not importable"
)
if Version(importlib_version("ipywidgets")) < Version("8.1.4"):
# See https://github.com/vega/altair/issues/3234#issuecomment-2268515312
jupyter_marks = skip_requires_anywidget(
pytest.mark.filterwarnings(
"ignore:Deprecated in traitlets 4.1.*:DeprecationWarning"
)
)
else:
jupyter_marks = skip_requires_anywidget


@pytest.fixture
def chart():
return alt.Chart("data.csv").mark_point()
Expand Down Expand Up @@ -94,12 +110,9 @@ def test_renderer_with_none_embed_options(chart, renderer="mimetype"):
assert bundle["image/svg+xml"].startswith("<svg")


@pytest.mark.filterwarnings("ignore:Deprecated in traitlets 4.1.*:DeprecationWarning")
@jupyter_marks
def test_jupyter_renderer_mimetype(chart, renderer="jupyter") -> None:
"""Test that we get the expected widget mimetype when the jupyter renderer is enabled."""
if not anywidget:
pytest.skip("anywidget not importable; skipping test")

with alt.renderers.enable(renderer):
assert (
"application/vnd.jupyter.widget-view+json"
Expand Down

0 comments on commit 01a1b13

Please sign in to comment.