Skip to content

Commit

Permalink
modify importorskip logic
Browse files Browse the repository at this point in the history
  • Loading branch information
OriolAbril committed Apr 5, 2024
1 parent 089c542 commit a7418c7
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 54 deletions.
10 changes: 7 additions & 3 deletions .azure-pipelines/azure-pipelines-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ jobs:
variables:
- name: NUMBA_DISABLE_JIT
value: 1
- name: ARVIZ_CI_MACHINE
value: 1
timeoutInMinutes: 360
strategy:
matrix:
Expand Down Expand Up @@ -82,9 +80,15 @@ jobs:
python -m pip freeze
displayName: 'Print packages'
- script: |
ARVIZ_REQUIRE_ALL_DEPS=TRUE python -m pytest arviz/tests/base_tests --cov arviz --cov-report=xml
condition: and(succeeded(), eq(variables.OptionalRequirements, true))
displayName: 'pytest (require all dependencies)'
- script: |
python -m pytest arviz/tests/base_tests --cov arviz --cov-report=xml
displayName: 'pytest'
condition: and(succeeded(), eq(variables.OptionalRequirements, false))
displayName: 'pytest (skip if dependency missing)'
- script: |
ls -ahl
Expand Down
2 changes: 1 addition & 1 deletion .azure-pipelines/azure-pipelines-external.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
variables:
- name: NUMBA_DISABLE_JIT
value: 1
- name: ARVIZ_CI_MACHINE
- name: ARVIZ_REQUIRE_ALL_DEPS
value: 1
timeoutInMinutes: 360
strategy:
Expand Down
3 changes: 1 addition & 2 deletions arviz/tests/base_tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
draws,
eight_schools_params,
models,
running_on_ci,
)


Expand Down Expand Up @@ -1469,7 +1468,7 @@ def test_json_converters(self, models):


@pytest.mark.skipif(
not (importlib.util.find_spec("datatree") or running_on_ci()),
not (importlib.util.find_spec("datatree") or "ARVIZ_REQUIRE_ALL_DEPS" in os.environ),
reason="test requires xarray-datatree library",
)
class TestDataTree:
Expand Down
1 change: 0 additions & 1 deletion arviz/tests/base_tests/test_data_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
draws,
eight_schools_params,
importorskip,
running_on_ci,
)

zarr = importorskip("zarr") # pylint: disable=invalid-name
Expand Down
10 changes: 2 additions & 8 deletions arviz/tests/base_tests/test_diagnostics_numba.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""Test Diagnostic methods"""

import importlib

# pylint: disable=redefined-outer-name, no-member, too-many-public-methods
import numpy as np
import pytest
Expand All @@ -11,13 +8,10 @@
from ...stats import bfmi, mcse, rhat
from ...stats.diagnostics import _mc_error, ks_summary
from ...utils import Numba
from ..helpers import running_on_ci
from ..helpers import importorskip
from .test_diagnostics import data # pylint: disable=unused-import

pytestmark = pytest.mark.skipif( # pylint: disable=invalid-name
(importlib.util.find_spec("numba") is None) and not running_on_ci(),
reason="test requires numba which is not installed",
)
importorskip("numba")

rcParams["data.load"] = "eager"

Expand Down
18 changes: 5 additions & 13 deletions arviz/tests/base_tests/test_plot_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=redefined-outer-name
import importlib
import os

import numpy as np
import pytest
Expand All @@ -20,10 +21,10 @@
from ...sel_utils import xarray_sel_iter, xarray_to_ndarray
from ...stats.density_utils import get_bins
from ...utils import get_coords
from ..helpers import running_on_ci

# Check if Bokeh is installed
bokeh_installed = importlib.util.find_spec("bokeh") is not None # pylint: disable=invalid-name
skip_tests = (not bokeh_installed) and ("ARVIZ_REQUIRE_ALL_DEPS" not in os.environ)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -212,10 +213,7 @@ def test_filter_plotter_list_warning():
assert len(plotters_filtered) == 5


@pytest.mark.skipif(
not (bokeh_installed or running_on_ci()),
reason="test requires bokeh which is not installed",
)
@pytest.mark.skipif(skip_tests, reason="test requires bokeh which is not installed")
def test_bokeh_import():
"""Tests that correct method is returned on bokeh import"""
plot = get_plotting_function("plot_dist", "distplot", "bokeh")
Expand Down Expand Up @@ -290,10 +288,7 @@ def test_mpl_dealiase_sel_kwargs():
assert res["line_color"] == "red"


@pytest.mark.skipif(
not (bokeh_installed or running_on_ci()),
reason="test requires bokeh which is not installed",
)
@pytest.mark.skipif(skip_tests, reason="test requires bokeh which is not installed")
def test_bokeh_dealiase_sel_kwargs():
"""Check bokeh dealiase_sel_kwargs behaviour.
Expand All @@ -315,10 +310,7 @@ def test_bokeh_dealiase_sel_kwargs():
assert res["line_color"] == "red"


@pytest.mark.skipif(
not (bokeh_installed or running_on_ci()),
reason="test requires bokeh which is not installed",
)
@pytest.mark.skipif(skip_tests, reason="test requires bokeh which is not installed")
def test_set_bokeh_circular_ticks_labels():
"""Assert the axes returned after placing ticks and tick labels for circular plots."""
import bokeh.plotting as bkp
Expand Down
9 changes: 2 additions & 7 deletions arviz/tests/base_tests/test_stats_numba.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# pylint: disable=redefined-outer-name, no-member
import importlib

import numpy as np
import pytest

Expand All @@ -9,15 +7,12 @@
from ...utils import Numba
from ..helpers import ( # pylint: disable=unused-import
check_multiple_attrs,
importorskip,
multidim_models,
running_on_ci,
)
from .test_stats import centered_eight, non_centered_eight # pylint: disable=unused-import

pytestmark = pytest.mark.skipif( # pylint: disable=invalid-name
(importlib.util.find_spec("numba") is None) and not running_on_ci(),
reason="test requires numba which is not installed",
)
numba = importorskip("numba")

rcParams["data.load"] = "eager"

Expand Down
8 changes: 2 additions & 6 deletions arviz/tests/base_tests/test_utils_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

from ...stats.stats_utils import stats_variance_2d as svar
from ...utils import Numba, _numba_var, numba_check
from ..helpers import running_on_ci
from ..helpers import importorskip
from .test_utils import utils_with_numba_import_fail # pylint: disable=unused-import

pytestmark = pytest.mark.skipif( # pylint: disable=invalid-name
(importlib.util.find_spec("numba") is None) and not running_on_ci(),
reason="test requires numba which is not installed",
)

importorskip("numba")

def test_utils_fixture(utils_with_numba_import_fail):
"""Test of utils fixture to ensure mock is applied correctly"""
Expand Down
10 changes: 5 additions & 5 deletions arviz/tests/external_tests/test_data_pystan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=no-member, invalid-name, redefined-outer-name, too-many-function-args
import importlib
from collections import OrderedDict
import os

import numpy as np
import pytest
Expand All @@ -16,19 +17,18 @@
importorskip,
load_cached_models,
pystan_version,
running_on_ci,
)

# Check if either pystan or pystan3 is installed
pystan_installed = (importlib.util.find_spec("pystan") is not None) or (
importlib.util.find_spec("stan") is not None
)
pytestmark = pytest.mark.skipif(
not (pystan_installed | running_on_ci()),
reason="test requires pystan/pystan3 which is not installed",
)


@pytest.mark.skipif(
not (pystan_installed or "ARVIZ_REQUIRE_ALL_DEPS" in os.environ),
reason="test requires pystan/pystan3 which is not installed"
)
class TestDataPyStan:
@pytest.fixture(scope="class")
def data(self, eight_schools_params, draws, chains):
Expand Down
11 changes: 3 additions & 8 deletions arviz/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,6 @@ def test_precompile_models(eight_schools_params, draws, chains):
load_cached_models(eight_schools_params, draws, chains)


def running_on_ci() -> bool:
"""Return True if running on CI machine."""
return os.environ.get("ARVIZ_CI_MACHINE") is not None


def importorskip(
modname: str, minversion: Optional[str] = None, reason: Optional[str] = None
) -> Any:
Expand All @@ -643,9 +638,9 @@ def importorskip(
Example::
docutils = pytest.importorskip("docutils")
"""
# ARVIZ_CI_MACHINE is True if tests run on CI, where ARVIZ_CI_MACHINE env variable exists
ARVIZ_CI_MACHINE = running_on_ci()
if not ARVIZ_CI_MACHINE:
# Unless ARVIZ_REQUIRE_ALL_DEPS is defined, tests that require a missing dependency are skipped
# if set, missing optional dependencies trigger failed tests.
if "ARVIZ_REQUIRE_ALL_DEPS" not in os.environ:
return pytest.importorskip(modname=modname, minversion=minversion, reason=reason)
import warnings

Expand Down

0 comments on commit a7418c7

Please sign in to comment.