From 8206105601c2afdd59180e16d31513e3d50258fa Mon Sep 17 00:00:00 2001 From: Baris Talar <250097450+baris-talar@users.noreply.github.com> Date: Sun, 26 Apr 2026 20:08:09 +0200 Subject: [PATCH 1/6] FIX: handle missing __version__ in check_version for soft imports --- doc/changes/dev/13863.bugfix.rst | 4 ++++ doc/changes/names.inc | 1 + mne/utils/check.py | 13 +++++++++---- mne/utils/tests/test_check.py | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 doc/changes/dev/13863.bugfix.rst diff --git a/doc/changes/dev/13863.bugfix.rst b/doc/changes/dev/13863.bugfix.rst new file mode 100644 index 00000000000..00f99df23ea --- /dev/null +++ b/doc/changes/dev/13863.bugfix.rst @@ -0,0 +1,4 @@ +:func:`mne.utils.check_version` now gracefully handles packages that do not +expose ``__version__`` at the top level when no minimum version is required. +This improves handling of optional dependencies in ``_soft_import``, by +:newcontrib:`Baris Talar`. diff --git a/doc/changes/names.inc b/doc/changes/names.inc index b87dbbfb04c..6a9f674b229 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -371,4 +371,5 @@ .. _Yu-Han Luo: https://github.com/yh-luo .. _Zhi Zhang: https://github.com/tczhangzhi/ .. _Ziyi ZENG: https://github.com/ZiyiTsang +.. _Baris Talar: https://github.com/baris-talar .. _Zvi Baratz: https://github.com/ZviBaratz 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") From 92b08c04b69b558a9e2607e2a4f28c6d45c21d18 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Apr 2026 18:11:54 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/changes/names.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/names.inc b/doc/changes/names.inc index 6a9f674b229..c8a9c5127ed 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -35,6 +35,7 @@ .. _Ashley Drew: https://github.com/ashdrew .. _Asish Panda: https://github.com/kaichogami .. _Austin Hurst: https://github.com/a-hurst +.. _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 @@ -371,5 +372,4 @@ .. _Yu-Han Luo: https://github.com/yh-luo .. _Zhi Zhang: https://github.com/tczhangzhi/ .. _Ziyi ZENG: https://github.com/ZiyiTsang -.. _Baris Talar: https://github.com/baris-talar .. _Zvi Baratz: https://github.com/ZviBaratz From ff8b85a1f1875828bb875e07aa9b5c5693f7910e Mon Sep 17 00:00:00 2001 From: Baris Talar <250097450+baris-talar@users.noreply.github.com> Date: Mon, 27 Apr 2026 00:43:52 +0200 Subject: [PATCH 3/6] =?UTF-8?q?DOC:=20fix=20changelog=20cross-reference=20?= =?UTF-8?q?=E2=80=94=20use=20plain=20code=20instead=20of=20broken=20:func:?= =?UTF-8?q?=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check_version is not listed in doc/api/ so :func:`mne.utils.check_version` fails the Sphinx docs build. Replace with plain inline ``check_version``. Co-Authored-By: Claude Sonnet 4.6 --- doc/changes/dev/13863.bugfix.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/changes/dev/13863.bugfix.rst b/doc/changes/dev/13863.bugfix.rst index 00f99df23ea..d5d298ccf82 100644 --- a/doc/changes/dev/13863.bugfix.rst +++ b/doc/changes/dev/13863.bugfix.rst @@ -1,4 +1,4 @@ -:func:`mne.utils.check_version` now gracefully handles packages that do not -expose ``__version__`` at the top level when no minimum version is required. -This improves handling of optional dependencies in ``_soft_import``, by +``check_version`` now gracefully handles packages that do not expose +``__version__`` at the top level when no minimum version is required, +improving handling of optional dependencies in ``_soft_import``, by :newcontrib:`Baris Talar`. From 42e65aa67d9296e914dc5360fa3cf46ef4acb278 Mon Sep 17 00:00:00 2001 From: Baris Talar <250097450+baris-talar@users.noreply.github.com> Date: Mon, 27 Apr 2026 00:57:51 +0200 Subject: [PATCH 4/6] DOC: rename changelog entry to match PR number #13870 Co-Authored-By: Claude Sonnet 4.6 --- doc/changes/dev/{13863.bugfix.rst => 13870.bugfix.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/changes/dev/{13863.bugfix.rst => 13870.bugfix.rst} (100%) diff --git a/doc/changes/dev/13863.bugfix.rst b/doc/changes/dev/13870.bugfix.rst similarity index 100% rename from doc/changes/dev/13863.bugfix.rst rename to doc/changes/dev/13870.bugfix.rst From 1845249ac8249c9c186a2b483c76dfe9853324b4 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 1 May 2026 10:00:55 -0500 Subject: [PATCH 5/6] Update doc/changes/dev/13870.bugfix.rst --- doc/changes/dev/13870.bugfix.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/changes/dev/13870.bugfix.rst b/doc/changes/dev/13870.bugfix.rst index d5d298ccf82..6c6b3e4d313 100644 --- a/doc/changes/dev/13870.bugfix.rst +++ b/doc/changes/dev/13870.bugfix.rst @@ -1,4 +1,2 @@ -``check_version`` now gracefully handles packages that do not expose -``__version__`` at the top level when no minimum version is required, -improving handling of optional dependencies in ``_soft_import``, by +Fix handling of optional dependencies that don't specify a version attribute, by :newcontrib:`Baris Talar`. From bdb0b1d8b422183375626805b8e9618e20c95b35 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Fri, 1 May 2026 11:54:23 -0400 Subject: [PATCH 6/6] Fix formatting in bugfix documentation --- doc/changes/dev/13870.bugfix.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/changes/dev/13870.bugfix.rst b/doc/changes/dev/13870.bugfix.rst index 6c6b3e4d313..76b8d4debea 100644 --- a/doc/changes/dev/13870.bugfix.rst +++ b/doc/changes/dev/13870.bugfix.rst @@ -1,2 +1 @@ -Fix handling of optional dependencies that don't specify a version attribute, by -:newcontrib:`Baris Talar`. +Fix handling of optional dependencies that don't specify a version attribute, by :newcontrib:`Baris Talar`.