From d2729f71809f1f5fc2d3644ef48ee351e3b28f3d Mon Sep 17 00:00:00 2001 From: Lily Wang <31115101+lilyminium@users.noreply.github.com> Date: Fri, 25 Oct 2024 06:36:02 +1100 Subject: [PATCH] Add deprecation warning for ITPParser (#4744) * add deprecation warning for ITPParser * add test for no deprecation warning in itp without valid elements --------- Co-authored-by: Irfan Alibay Co-authored-by: Rocco Meli --- package/CHANGELOG | 2 ++ package/MDAnalysis/topology/ITPParser.py | 10 +++++++++- testsuite/MDAnalysisTests/topology/test_itp.py | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 85a7208627..52e8eb077b 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -112,6 +112,8 @@ Changes numpy.testing.assert_allclose #4438) Deprecations + * Element guessing in the ITPParser is deprecated and will be removed in version 3.0 + (Issue #4698) * Unknown masses are set to 0.0 for current version, this will be depracated in version 3.0.0 and replaced by :class:`Masses`' no_value_label attribute(np.nan) (PR #3753) diff --git a/package/MDAnalysis/topology/ITPParser.py b/package/MDAnalysis/topology/ITPParser.py index 9c9dd37976..5649e5cb38 100644 --- a/package/MDAnalysis/topology/ITPParser.py +++ b/package/MDAnalysis/topology/ITPParser.py @@ -601,7 +601,15 @@ def parse(self, include_dir='/usr/local/gromacs/share/gromacs/top/', if all(e.capitalize() in SYMB2Z for e in self.elements): attrs.append(Elements(np.array(self.elements, dtype=object), guessed=True)) - + warnings.warn( + "The elements attribute has been populated by guessing " + "elements from atom types. This behaviour has been " + "temporarily added to the ITPParser as we transition " + "to the new guessing API. " + "This behavior will be removed in release 3.0. " + "Please see issue #4698 for more information. ", + DeprecationWarning + ) else: warnings.warn("Element information is missing, elements attribute " "will not be populated. If needed these can be " diff --git a/testsuite/MDAnalysisTests/topology/test_itp.py b/testsuite/MDAnalysisTests/topology/test_itp.py index e5cea0e215..9702141ee5 100644 --- a/testsuite/MDAnalysisTests/topology/test_itp.py +++ b/testsuite/MDAnalysisTests/topology/test_itp.py @@ -490,3 +490,20 @@ def test_missing_elements_no_attribute(): u = mda.Universe(ITP_atomtypes) with pytest.raises(AttributeError): _ = u.atoms.elements + + +def test_elements_deprecation_warning(): + """Test deprecation warning is present""" + with pytest.warns(DeprecationWarning, match="removed in release 3.0"): + mda.Universe(ITP_nomass) + + +def test_elements_nodeprecation_warning(): + """Test deprecation warning is not present if elements isn't guessed""" + with pytest.warns(UserWarning) as record: + mda.Universe(ITP_atomtypes) + assert len(record) == 2 + + warned = [warn.message.args[0] for warn in record] + assert "Element information is missing" in warned[0] + assert "No coordinate reader found" in warned[1]