Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch guessers to catching and raising NoDataErrors #4755

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions package/MDAnalysis/guesser/default_guesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

import re

from ..exceptions import NoDataError
from ..lib import distances
from . import tables

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 4 additions & 3 deletions testsuite/MDAnalysisTests/guesser/test_default_guesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])

Expand Down Expand Up @@ -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'])


Expand Down
Loading