diff --git a/package/MDAnalysis/guesser/default_guesser.py b/package/MDAnalysis/guesser/default_guesser.py index 87da87e12c..ee4ede1d7d 100644 --- a/package/MDAnalysis/guesser/default_guesser.py +++ b/package/MDAnalysis/guesser/default_guesser.py @@ -109,6 +109,7 @@ import re +from ..exceptions import NoDataError from ..lib import distances from . import tables @@ -218,18 +219,18 @@ def guess_masses(self, atom_types=None, indices_to_guess=None): if atom_types is None: try: atom_types = self._universe.atoms.elements - except AttributeError: + except NoDataError: try: atom_types = self._universe.atoms.types - except AttributeError: + except NoDataError: try: atom_types = self.guess_types( atom_types=self._universe.atoms.names) - except ValueError: - raise ValueError( + except NoDataError: + raise NoDataError( "there is no reference attributes" " (elements, types, or names)" - " in this universe to guess mass from") + " in this universe to guess mass from") from None if indices_to_guess is not None: atom_types = atom_types[indices_to_guess] @@ -291,10 +292,10 @@ def guess_types(self, atom_types=None, indices_to_guess=None): if atom_types is None: try: atom_types = self._universe.atoms.names - except AttributeError: - raise ValueError( + except NoDataError: + raise NoDataError( "there is no reference attributes in this universe " - "to guess types from") + "to guess types from") from None if indices_to_guess is not None: atom_types = atom_types[indices_to_guess] diff --git a/testsuite/MDAnalysisTests/guesser/test_default_guesser.py b/testsuite/MDAnalysisTests/guesser/test_default_guesser.py index 8eb55b6952..fe8e012c8c 100644 --- a/testsuite/MDAnalysisTests/guesser/test_default_guesser.py +++ b/testsuite/MDAnalysisTests/guesser/test_default_guesser.py @@ -26,7 +26,8 @@ from numpy.testing import assert_equal, assert_allclose import numpy as np -from MDAnalysis.core.topologyattrs import Angles, Atomtypes, Atomnames, Masses +from MDAnalysis.core.topologyattrs import Angles, Atomtypes, Atomnames +from MDAnalysis.exceptions import NoDataError from MDAnalysis.guesser.default_guesser import DefaultGuesser from MDAnalysis.core.topology import Topology from MDAnalysisTests import make_Universe @@ -82,7 +83,7 @@ def test_guess_atom_mass(self, default_guesser): def test_guess_masses_with_no_reference_elements(self): u = mda.Universe.empty(3) - with pytest.raises(ValueError, + with pytest.raises(NoDataError, match=('there is no reference attributes ')): u.guess_TopologyAttrs('default', ['masses']) @@ -150,7 +151,7 @@ def test_guess_charge(default_guesser): def test_guess_bonds_Error(): u = make_Universe(trajectory=True) msg = "This Universe does not contain name information" - with pytest.raises(ValueError, match=msg): + with pytest.raises(NoDataError, match=msg): u.guess_TopologyAttrs(to_guess=['bonds'])