diff --git a/doc/changes/dev/13870.bugfix.rst b/doc/changes/dev/13870.bugfix.rst new file mode 100644 index 00000000000..76b8d4debea --- /dev/null +++ b/doc/changes/dev/13870.bugfix.rst @@ -0,0 +1 @@ +Fix handling of optional dependencies that don't specify a version attribute, by :newcontrib:`Baris Talar`. diff --git a/doc/changes/names.inc b/doc/changes/names.inc index d7349b1eac9..48183de4645 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -36,6 +36,7 @@ .. _Asish Panda: https://github.com/kaichogami .. _Austin Hurst: https://github.com/a-hurst .. _Ayushi Satodiya: https://github.com/ayuclan +.. _Baris Talar: https://github.com/baris-talar .. _Beige Jin: https://github.com/BeiGeJin .. _Ben Beasley: https://github.com/musicinmybrain .. _Benedikt Ehinger: https://www.benediktehinger.de diff --git a/mne/utils/check.py b/mne/utils/check.py index 85c5c62bdf7..fe2d82eeb86 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -83,7 +83,9 @@ def check_version(library, min_version="0.0", *, strip=True, return_version=Fals Parameters ---------- library : str - The library name to import. Must have a ``__version__`` property. + The library name to import. Should have a ``__version__`` property; + if absent and ``min_version`` is specified, the version check will + fail. min_version : str The minimum version string. Anything that matches ``'(\d+ | [a-z]+ | \.)'``. Can also be empty to skip version @@ -120,11 +122,14 @@ def check_version(library, min_version="0.0", *, strip=True, return_version=Fals check_version = min_version and min_version != "0.0" get_version = check_version or return_version if get_version: - version = library.__version__ - if strip: + try: + version = library.__version__ + except AttributeError: + version = None + if version is not None and strip: version = _strip_dev(version) if check_version: - if _compare_version(version, "<", min_version): + if version is None or _compare_version(version, "<", min_version): ok = False out = (ok, version) if return_version else ok return out diff --git a/mne/utils/tests/test_check.py b/mne/utils/tests/test_check.py index af59ddf4e12..e8d1aa3ba10 100644 --- a/mne/utils/tests/test_check.py +++ b/mne/utils/tests/test_check.py @@ -415,3 +415,19 @@ def test_soft_import(): """Test _soft_import.""" with pytest.raises(RuntimeError, match=r".* the module mne>=999 \(found version.*"): _soft_import("mne", "testing", min_version="999") + + +def test_soft_import_missing_version(monkeypatch): + """Test _soft_import handles packages without __version__.""" + import types + + fake_mod = types.ModuleType("fake_no_version") + monkeypatch.setitem(sys.modules, "fake_no_version", fake_mod) + + # No min_version: should succeed even without __version__ + mod = _soft_import("fake_no_version", "testing", strict=True) + assert mod is fake_mod + + # With min_version: should fail because __version__ is absent + with pytest.raises(RuntimeError, match="module fake_no_version"): + _soft_import("fake_no_version", "testing", strict=True, min_version="1.0")