Skip to content

Commit b2d7ee8

Browse files
committed
fix: mypy 1.12.0 errors
# Related - #3630 (comment) ``` tests\vegalite\v5\test_renderers.py:14: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment] vlc = None ^~~~ tests\vegalite\v5\test_api.py:32: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment] vlc = None ^~~~ tests\utils\test_mimebundle.py:10: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment] vlc = None ^~~~ tests\utils\test_compiler.py:10: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment] vlc = None ^~~~ Found 4 errors in 4 files (checked 332 source files) ```
1 parent d4c3bcf commit b2d7ee8

File tree

5 files changed

+24
-43
lines changed

5 files changed

+24
-43
lines changed

tests/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
https://github.com/vega/vl-convert
5757
"""
5858

59+
skip_requires_vegafusion: pytest.MarkDecorator = pytest.mark.skipif(
60+
find_spec("vegafusion") is None, reason="`vegafusion` not installed."
61+
)
62+
"""
63+
``pytest.mark.skipif`` decorator.
64+
65+
Applies when `vegafusion`_ import would fail.
66+
67+
.. _vegafusion:
68+
https://github.com/vega/vegafusion
69+
"""
70+
5971

6072
skip_requires_pyarrow: pytest.MarkDecorator = pytest.mark.skipif(
6173
find_spec("pyarrow") is None, reason="`pyarrow` not installed."

tests/utils/test_compiler.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import pytest
44

55
from altair import Chart, vegalite_compilers
6-
7-
try:
8-
import vl_convert as vlc
9-
except ImportError:
10-
vlc = None
6+
from tests import skip_requires_vl_convert
117

128

139
@pytest.fixture
@@ -30,27 +26,21 @@ def assert_is_vega_spec(vega_spec):
3026
assert "axes" in vega_spec
3127

3228

29+
@skip_requires_vl_convert
3330
def test_vegalite_compiler(chart):
34-
if vlc is None:
35-
pytest.skip("vl_convert is not installed")
36-
3731
vegalite_spec = chart.to_dict()
3832
vega_spec = vegalite_compilers.get()(vegalite_spec)
3933
assert_is_vega_spec(vega_spec)
4034

4135

36+
@skip_requires_vl_convert
4237
def test_to_dict_with_format_vega(chart):
43-
if vlc is None:
44-
pytest.skip("vl_convert is not installed")
45-
4638
vega_spec = chart.to_dict(format="vega")
4739
assert_is_vega_spec(vega_spec)
4840

4941

42+
@skip_requires_vl_convert
5043
def test_to_json_with_format_vega(chart):
51-
if vlc is None:
52-
pytest.skip("vl_convert is not installed")
53-
5444
json_spec = chart.to_json(format="vega")
5545
assert isinstance(json_spec, str)
5646
spec = json.loads(json_spec)

tests/utils/test_mimebundle.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
import altair as alt
44
from altair import VEGA_VERSION
55
from altair.utils.mimebundle import spec_to_mimebundle
6-
7-
try:
8-
import vl_convert as vlc
9-
except ImportError:
10-
vlc = None
11-
12-
try:
13-
import vegafusion as vf # type: ignore
14-
except ImportError:
15-
vf = None
6+
from tests import skip_requires_vegafusion, skip_requires_vl_convert
167

178

189
@pytest.fixture
@@ -168,10 +159,8 @@ def vega_spec():
168159
}
169160

170161

162+
@skip_requires_vl_convert
171163
def test_vegalite_to_vega_mimebundle(vegalite_spec, vega_spec):
172-
if vlc is None:
173-
pytest.skip("vl_convert not importable; cannot run mimebundle tests")
174-
175164
bundle = spec_to_mimebundle(
176165
spec=vegalite_spec,
177166
format="vega",
@@ -233,7 +222,7 @@ def check_pre_transformed_vega_spec(vega_spec):
233222
assert len(data_0.get("transform", [])) == 0
234223

235224

236-
@pytest.mark.skipif(vf is None, reason="vegafusion is not installed")
225+
@skip_requires_vegafusion
237226
def test_vegafusion_spec_to_vega_mime_bundle(vegalite_spec):
238227
with alt.data_transformers.enable("vegafusion"):
239228
bundle = spec_to_mimebundle(
@@ -246,7 +235,7 @@ def test_vegafusion_spec_to_vega_mime_bundle(vegalite_spec):
246235
check_pre_transformed_vega_spec(vega_spec)
247236

248237

249-
@pytest.mark.skipif(vf is None, reason="vegafusion is not installed")
238+
@skip_requires_vegafusion
250239
def test_vegafusion_chart_to_vega_mime_bundle(vegalite_spec):
251240
chart = alt.Chart.from_dict(vegalite_spec)
252241
with alt.data_transformers.enable("vegafusion"), alt.renderers.enable("json"):

tests/vegalite/v5/test_api.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import tempfile
1313
from datetime import date, datetime
1414
from importlib.metadata import version as importlib_version
15+
from importlib.util import find_spec
1516
from typing import TYPE_CHECKING
1617

1718
import ibis
@@ -26,11 +27,6 @@
2627
from altair.utils.schemapi import Optional, SchemaValidationError, Undefined
2728
from tests import skip_requires_vl_convert, slow
2829

29-
try:
30-
import vl_convert as vlc
31-
except ImportError:
32-
vlc = None
33-
3430
if TYPE_CHECKING:
3531
from altair.vegalite.v5.api import _Conditional, _Conditions
3632
from altair.vegalite.v5.schema._typing import Map
@@ -833,7 +829,7 @@ def test_save(format, engine, basic_chart):
833829
basic_chart.save(out, format=format, engine=engine)
834830
assert f"Unsupported format: '{format}'" in str(err.value)
835831
return
836-
elif vlc is None:
832+
elif find_spec("vl_convert") is None:
837833
with pytest.raises(ValueError) as err: # noqa: PT011
838834
basic_chart.save(out, format=format, engine=engine)
839835
assert "vl-convert-python" in str(err.value)

tests/vegalite/v5/test_renderers.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
from packaging.version import Version
88

99
import altair.vegalite.v5 as alt
10-
11-
try:
12-
import vl_convert as vlc
13-
except ImportError:
14-
vlc = None
10+
from tests import skip_requires_vl_convert
1511

1612
try:
1713
import anywidget
@@ -92,10 +88,8 @@ def test_json_renderer_embed_options(chart, renderer="json"):
9288
assert metadata == {mimetype: {"option": "foo"}}
9389

9490

91+
@skip_requires_vl_convert
9592
def test_renderer_with_none_embed_options(chart, renderer="mimetype"):
96-
if vlc is None:
97-
pytest.skip("vl_convert not importable; cannot run this test")
98-
9993
# Check that setting embed_options to None doesn't crash
10094
from altair.utils.mimebundle import spec_to_mimebundle
10195

0 commit comments

Comments
 (0)