diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index 97e4953b..a7b89ead 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -5,6 +5,7 @@ Changelog Unreleased ---------- +* Ignore unrelated warnings from MDAnalysis * Added argcomplete for tab completion * Updated docs diff --git a/src/mdacli/__main__.py b/src/mdacli/__main__.py index 07d142fb..71b3b3c0 100644 --- a/src/mdacli/__main__.py +++ b/src/mdacli/__main__.py @@ -40,7 +40,7 @@ def main(): mdacli.cli( name="MDAnalysis", - module_list=[f"MDAnalysis.analysis.{m}" for m in __all__], + module_list=[f"MDAnalysis.analysis.{m}" for m in __all__ if m != "hbonds"], version=mdacli.__version__, description=__doc__, skip_modules=skip_mods, diff --git a/src/mdacli/libcli.py b/src/mdacli/libcli.py index fad9212d..ecdada19 100644 --- a/src/mdacli/libcli.py +++ b/src/mdacli/libcli.py @@ -127,7 +127,7 @@ def find_cls_members(cls, modules, ignore_warnings=False): Flag to ignore warnings """ with warnings.catch_warnings(): - if not ignore_warnings: + if ignore_warnings: warnings.simplefilter("ignore") return find_classes_in_modules(cls, *[m for m in modules]) diff --git a/tests/test_libcli.py b/tests/test_libcli.py index 0645321b..9c66710d 100644 --- a/tests/test_libcli.py +++ b/tests/test_libcli.py @@ -10,6 +10,7 @@ import logging import os import sys +import warnings from io import StringIO from json.decoder import JSONDecodeError from pathlib import Path @@ -125,6 +126,50 @@ def test_find_cls_members_single(cls): assert members[1] is InterRDF_s +def test_find_cls_members_ignore_warnings_true(): + """Test that warnings are suppressed when ignore_warnings=True.""" + with patch("mdacli.libcli.find_classes_in_modules") as mocked: + + def warn_func(*_args, **_kwargs): + warnings.warn("warning", DeprecationWarning, stacklevel=2) + return ["OK"] + + mocked.side_effect = warn_func + + with warnings.catch_warnings(): + warnings.simplefilter("error") + + members = find_cls_members( + AnalysisBase, + modules=["dummy.mod"], + ignore_warnings=True, + ) + + assert members == ["OK"] + + +def test_find_cls_members_ignore_warnings_false(): + """Test that warnings are emitted when ignore_warnings=False.""" + with patch("mdacli.libcli.find_classes_in_modules") as mocked: + + def warn_func(*_args, **_kwargs): + warnings.warn( + "This module was moved to ...", DeprecationWarning, stacklevel=2 + ) + return ["OK"] + + mocked.side_effect = warn_func + + with pytest.warns(DeprecationWarning, match="moved to"): + members = find_cls_members( + AnalysisBase, + modules=["dummy.mod"], + ignore_warnings=False, + ) + + assert members == ["OK"] + + @pytest.mark.parametrize( "cls", [AnalysisBase, [AnalysisBase, AnalysisBase], (AnalysisBase, AnalysisBase)] )