Skip to content

Commit

Permalink
remove test_normal
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Oct 16, 2023
1 parent 7ea9e56 commit 7d1a787
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
15 changes: 12 additions & 3 deletions sparc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
conda build and CI where not all dependencies are present
"""


def _missing_deps_func(*args, **kwargs):
raise ImportError("Importing fails for ase / numpy!")


class SPARCMissingDeps:
def __init__(self, *args, **kwargs):
raise ImportError("Cannot initialize sparc.SPARC because the required dependencies (ase and numpy) are not available.")
raise ImportError(
"Cannot initialize sparc.SPARC because the required dependencies (ase and numpy) are not available."
)

def __getattr__(self, name):
raise ImportError(f"Cannot access '{name}' on sparc.SPARC because the required dependencies (ase and numpy) are not available.")
raise ImportError(
f"Cannot access '{name}' on sparc.SPARC because the required dependencies (ase and numpy) are not available."
)


try:
import ase
import numpy

_import_complete = True
except ImportError:
_import_complete = False
Expand All @@ -25,11 +34,11 @@ def __getattr__(self, name):
from .io import read_sparc, write_sparc
from .io import register_ase_io_sparc
from .calculator import SPARC

register_ase_io_sparc()
else:
# If importing is not complete, any code trying to directly import
# the following attributes will raise ImportError
read_sparc = _missing_deps_func
write_sparc = _missing_deps_func
SPARC = SPARCMissingDeps

4 changes: 1 addition & 3 deletions sparc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def deprecated(message):
def decorator(func):
def new_func(*args, **kwargs):
warn(
"Function {} is deprecated! {}".format(
func.__name__, message
),
"Function {} is deprecated! {}".format(func.__name__, message),
category=DeprecationWarning,
)
return func(*args, **kwargs)
Expand Down
39 changes: 10 additions & 29 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,25 @@
"""
import pytest
import sys
import ase

def test_download_data():
import ase
original_ase = sys.modules.get('ase')
sys.modules['ase'] = None

def test_download_data(monkeypatch):
monkeypatch.setitem(sys.modules, "ase", None)
with pytest.raises(ImportError):
import ase
from sparc.download_data import download_psp
# Recover sys ase
sys.modules['ase'] = original_ase

def test_api():
import ase
original_ase = sys.modules.get('ase')
sys.modules['ase'] = None

def test_api(monkeypatch):
monkeypatch.setitem(sys.modules, "ase", None)
with pytest.raises(ImportError):
import ase
from sparc.api import SparcAPI
# Recover sys ase
sys.modules['ase'] = original_ase

def test_docparser():
import ase
original_ase = sys.modules.get('ase')
sys.modules['ase'] = None
with pytest.raises(ImportError):
import ase
from sparc.docparser import SPARCDocParser
# Recover sys ase
sys.modules['ase'] = original_ase

def test_normal():
import ase
original_ase = sys.modules.get('ase')
sys.modules['ase'] = None
def test_docparser(monkeypatch):
monkeypatch.setitem(sys.modules, "ase", None)
with pytest.raises(ImportError):
import ase
with pytest.raises(ImportError):
from sparc.io import read_sparc
# Recover sys ase
sys.modules['ase'] = original_ase
from sparc.docparser import SPARCDocParser

0 comments on commit 7d1a787

Please sign in to comment.