Skip to content

Commit

Permalink
use importlib.util.find_spec rather than sys.modules to check whe…
Browse files Browse the repository at this point in the history
…ther a python package is installed, appears more robust
  • Loading branch information
lukashergt committed Sep 26, 2024
1 parent 4c54fd9 commit a6b4e66
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from importlib.util import find_spec
import pytest
import sys

try:
import astropy # noqa: F401
except ImportError:
pass

condition = 'astropy' not in sys.modules
condition = find_spec('astropy') is None
reason = "requires astropy package"
raises = ImportError
astropy_mark_skip = pytest.mark.skipif(condition, reason=reason)
Expand All @@ -22,7 +22,7 @@ def skipif_no_astropy(param):
except ImportError:
pass
reason = "requires fastkde package"
condition = 'fastkde' not in sys.modules
condition = find_spec('fastkde') is None
raises = ImportError
fastkde_mark_skip = pytest.mark.skipif(condition, reason=reason)
fastkde_mark_xfail = pytest.mark.xfail(condition, raises=raises, reason=reason)
Expand All @@ -36,7 +36,7 @@ def skipif_no_fastkde(param):
import getdist # noqa: F401
except ImportError:
pass
condition = 'getdist' not in sys.modules
condition = find_spec('getdist') is None
reason = "requires getdist package"
raises = ImportError
getdist_mark_skip = pytest.mark.skipif(condition, reason=reason)
Expand Down Expand Up @@ -65,7 +65,7 @@ def skipif_no_getdist(param):
except ImportError:
pass

condition = 'h5py' not in sys.modules
condition = find_spec('h5py') is None
reason = "requires h5py package"
raises = ImportError
h5py_mark_skip = pytest.mark.skipif(condition, reason=reason)
Expand Down

0 comments on commit a6b4e66

Please sign in to comment.