diff --git a/CHANGELOG.md b/CHANGELOG.md index 53eb1e3..56475e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -324,7 +324,10 @@ Notable changes to Kinisot. Format follows [Keep a Changelog](https://keepachang - `--calc aimnet2[:model]` uses the `aimnet` package (AIMNet2's current distribution) and falls back to the older `aimnet2calc`. `model` is a name from aimnet's registry, such as `aimnet2-rxn`, or a model file. A - model that cannot be loaded is an input error. + model that cannot be loaded is an input error. The fallback applies only + when aimnet is not installed: an installed aimnet that fails to import + (a missing submodule, or a missing or incompatible dependency) is + reported with its own error rather than as a missing `aimnet2calc`. - The calculator registry records which keyword receives the `:model` part of a `--calc` specification (`method` for tblite, `model` elsewhere). - The benchmark runner averages equivalent positions (`iso_average`, diff --git a/kinisot/backends/ase.py b/kinisot/backends/ase.py index 7e281bf..f2c4313 100644 --- a/kinisot/backends/ase.py +++ b/kinisot/backends/ase.py @@ -193,7 +193,13 @@ def aimnet2_calculator(model="aimnet2"): """ try: from aimnet.calculators.aimnet2ase import AIMNet2ASE - except ImportError: + except ImportError as err: + # fall back to aimnet2calc only when aimnet itself is absent, not when an installed aimnet fails + # to import (a missing submodule or dependency) + if not (isinstance(err, ModuleNotFoundError) and err.name == "aimnet"): + raise KinisotInputError( + "aimnet is installed but cannot be imported for --calc aimnet2 (%s: %s)" % (type(err).__name__, err) + ) from None try: from aimnet2calc import AIMNet2ASE except ImportError as err: diff --git a/tests/test_ase.py b/tests/test_ase.py index b9c1b8a..1fcfaa1 100644 --- a/tests/test_ase.py +++ b/tests/test_ase.py @@ -272,18 +272,29 @@ def __init__(self, base_calc="aimnet2", charge=0, mult=1): aimnet2ase = types.ModuleType("aimnet.calculators.aimnet2ase") aimnet2ase.AIMNet2ASE = AIMNet2ASE - for name, module in ( - ("aimnet", types.ModuleType("aimnet")), - ("aimnet.calculators", types.ModuleType("aimnet.calculators")), - ("aimnet.calculators.aimnet2ase", aimnet2ase), - ): + packages = types.ModuleType("aimnet"), types.ModuleType("aimnet.calculators") + for package in packages: + package.__path__ = [] + for name, module in (("aimnet", packages[0]), ("aimnet.calculators", packages[1])): monkeypatch.setitem(sys.modules, name, module) + monkeypatch.setitem(sys.modules, "aimnet.calculators.aimnet2ase", aimnet2ase) assert build_calculator("aimnet2").base_calc == "aimnet2" assert build_calculator("aimnet2:aimnet2-rxn").base_calc == "aimnet2-rxn" with pytest.raises(KinisotInputError, match="cannot load the AIMNet2 model missing"): build_calculator("aimnet2:missing") - monkeypatch.setitem(sys.modules, "aimnet.calculators.aimnet2ase", None) # an install from before aimnet + # an install from before aimnet: no aimnet package at all, as Python reports it (even if aimnet is installed) + import importlib.abc + + class NoAimnet(importlib.abc.MetaPathFinder): + def find_spec(self, fullname, path=None, target=None): + if fullname == "aimnet": + raise ModuleNotFoundError("No module named 'aimnet'", name="aimnet") + return None + + for name in ("aimnet", "aimnet.calculators", "aimnet.calculators.aimnet2ase"): + monkeypatch.delitem(sys.modules, name) + monkeypatch.setattr(sys, "meta_path", [NoAimnet()] + sys.meta_path) old = types.ModuleType("aimnet2calc") old.AIMNet2ASE = AIMNet2ASE monkeypatch.setitem(sys.modules, "aimnet2calc", old) @@ -293,6 +304,49 @@ def __init__(self, base_calc="aimnet2", charge=0, mult=1): build_calculator("aimnet2") +@pytest.mark.parametrize( + "error", + [ + ModuleNotFoundError("No module named 'torch_cluster'", name="torch_cluster"), + ImportError("cannot import name 'segment_coo' from 'torch_scatter'", name="torch_scatter"), + ModuleNotFoundError("No module named 'aimnet.calculators.aimnet2ase'", name="aimnet.calculators.aimnet2ase"), + ], +) +def test_aimnet2_calculator_reports_a_broken_aimnet(monkeypatch, error): + # aimnet installed but failing to import (a missing or incompatible dependency, or a missing submodule) + # must be reported as such, not hidden by falling back to aimnet2calc, even when aimnet2calc is installed + import importlib.abc + import importlib.util + import sys + import types + + class BrokenAimnet(importlib.abc.MetaPathFinder, importlib.abc.Loader): + def find_spec(self, fullname, path=None, target=None): + if fullname == "aimnet.calculators.aimnet2ase": + return importlib.util.spec_from_loader(fullname, self) + return None + + def create_module(self, spec): + return None + + def exec_module(self, module): + raise error + + packages = types.ModuleType("aimnet"), types.ModuleType("aimnet.calculators") + for package in packages: + package.__path__ = [] + for name, module in (("aimnet", packages[0]), ("aimnet.calculators", packages[1])): + monkeypatch.setitem(sys.modules, name, module) + monkeypatch.delitem(sys.modules, "aimnet.calculators.aimnet2ase", raising=False) + monkeypatch.setattr(sys, "meta_path", [BrokenAimnet()] + sys.meta_path) + old = types.ModuleType("aimnet2calc") + old.AIMNet2ASE = lambda model: model + monkeypatch.setitem(sys.modules, "aimnet2calc", old) + with pytest.raises(KinisotInputError, match="aimnet is installed but cannot be imported") as info: + build_calculator("aimnet2") + assert str(error) in str(info.value) + + def test_uma_calculator_loads_a_double_precision_model(monkeypatch, tmp_path): # --calc uma must ask fairchem for float64 without torch.compile, use the omol (molecule) head, and turn # a failed download of the gated checkpoint into an input error. Fake modules stand in for fairchem and torch.