From a5b4431cf307fff3fa2abb096bb9d519c5df2a60 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 29 Sep 2026 15:50:21 +0000 Subject: [PATCH 1/2] --calc aimnet2: fall back to aimnet2calc only when aimnet is not installed The import fallback caught every ImportError from aimnet. An installed aimnet that failed to import (a missing or incompatible dependency) was then reported as a missing aimnet2calc, or silently replaced by an old aimnet2calc install. Fall back only when the missing module is aimnet itself; otherwise report aimnet's own import error as an input error. Raised in the review of #18. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K49r16vaYeUsGCHD26vmM2 --- CHANGELOG.md | 5 +++- kinisot/backends/ase.py | 10 +++++++- tests/test_ase.py | 51 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53eb1e3..4789262 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 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..0679401 100644 --- a/kinisot/backends/ase.py +++ b/kinisot/backends/ase.py @@ -183,6 +183,9 @@ def sevennet_calculator(model="7net-0", modal=None, device="auto"): raise KinisotInputError("--calc sevennet:%s: %s%s" % (model, err, hint)) from None +_AIMNET_MODULES = ("aimnet", "aimnet.calculators", "aimnet.calculators.aimnet2ase") + + def aimnet2_calculator(model="aimnet2"): """AIMNet2 calculator for ``--calc aimnet2[:model]``; ``model`` is a name from aimnet's model registry (``aimnet2``, the wB97M-D3 model; ``aimnet2-rxn``, the model for reactive chemistry; ...) @@ -193,7 +196,12 @@ 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 aimnet fails to import + if not (isinstance(err, ModuleNotFoundError) and err.name in _AIMNET_MODULES): + 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..55a826e 100644 --- a/tests/test_ase.py +++ b/tests/test_ase.py @@ -272,12 +272,12 @@ 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"): @@ -293,6 +293,47 @@ 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"), + ], +) +def test_aimnet2_calculator_reports_a_broken_aimnet(monkeypatch, error): + # aimnet installed but failing to import (a missing or incompatible dependency) must be reported as such, + # not hidden by falling back to aimnet2calc, even when aimnet2calc is installed too + 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 .*torch_"): + build_calculator("aimnet2") + + 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. From b90924834fd308e33001397fe214c5c20a2670f6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 29 Sep 2026 16:37:33 +0000 Subject: [PATCH 2/2] --calc aimnet2: fall back only when the aimnet package itself is missing The fallback to aimnet2calc also fired when aimnet was installed but aimnet.calculators or aimnet.calculators.aimnet2ase could not be found, contrary to "only when aimnet is not installed". It now requires the missing module to be aimnet itself; an installed aimnet without its calculator module is reported like any other broken aimnet. The test of the fallback now simulates an absent aimnet as Python reports it, and the missing-submodule case joins the broken-aimnet tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K49r16vaYeUsGCHD26vmM2 --- CHANGELOG.md | 4 ++-- kinisot/backends/ase.py | 8 +++----- tests/test_ase.py | 21 +++++++++++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4789262..56475e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -326,8 +326,8 @@ Notable changes to Kinisot. Format follows [Keep a Changelog](https://keepachang name from aimnet's registry, such as `aimnet2-rxn`, or a model file. A 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 or incompatible dependency) is reported with its own error - rather than as a missing `aimnet2calc`. + (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 0679401..f2c4313 100644 --- a/kinisot/backends/ase.py +++ b/kinisot/backends/ase.py @@ -183,9 +183,6 @@ def sevennet_calculator(model="7net-0", modal=None, device="auto"): raise KinisotInputError("--calc sevennet:%s: %s%s" % (model, err, hint)) from None -_AIMNET_MODULES = ("aimnet", "aimnet.calculators", "aimnet.calculators.aimnet2ase") - - def aimnet2_calculator(model="aimnet2"): """AIMNet2 calculator for ``--calc aimnet2[:model]``; ``model`` is a name from aimnet's model registry (``aimnet2``, the wB97M-D3 model; ``aimnet2-rxn``, the model for reactive chemistry; ...) @@ -197,8 +194,9 @@ def aimnet2_calculator(model="aimnet2"): try: from aimnet.calculators.aimnet2ase import AIMNet2ASE except ImportError as err: - # fall back to aimnet2calc only when aimnet itself is absent, not when aimnet fails to import - if not (isinstance(err, ModuleNotFoundError) and err.name in _AIMNET_MODULES): + # 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 diff --git a/tests/test_ase.py b/tests/test_ase.py index 55a826e..1fcfaa1 100644 --- a/tests/test_ase.py +++ b/tests/test_ase.py @@ -283,7 +283,18 @@ def __init__(self, base_calc="aimnet2", charge=0, mult=1): 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) @@ -298,11 +309,12 @@ def __init__(self, base_calc="aimnet2", charge=0, mult=1): [ 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) must be reported as such, - # not hidden by falling back to aimnet2calc, even when aimnet2calc is installed too + # 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 @@ -330,8 +342,9 @@ def exec_module(self, module): 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 .*torch_"): + 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):