Skip to content

Commit b2bec49

Browse files
committed
Improve msd_type error handling
1 parent 5f62866 commit b2bec49

2 files changed

Lines changed: 7 additions & 15 deletions

File tree

package/MDAnalysis/analysis/msd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ def _parse_msd_type(self):
385385
try:
386386
self._dim = keys[self.msd_type.lower()]
387387
except (AttributeError, KeyError):
388-
raise TypeError(
389-
"msd_type must be a string and one of: xyz, xy, xz, yz, x, y, z"
388+
raise ValueError(
389+
f"Invalid msd_type {self.msd_type.lower()} must be a string and one of: xyz, xy, xz, yz, x, y, z"
390390
)
391391

392392
self.dim_fac = len(self._dim)

testsuite/MDAnalysisTests/analysis/test_msd.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,12 @@ def test_updating_ag_rejected(self, u):
118118
m = MSD(updating_ag, msd_type="xyz", fft=False)
119119

120120
@pytest.mark.parametrize(
121-
"msd_type, exc",
122-
[
123-
("Xz", None), # valid, mixed case.
124-
(123, TypeError), # non-string.
125-
],
121+
"msdtype", ["foo", "bar", "yx", "zyx", 123, "", " xy "]
126122
)
127-
def test_msdtype_error(self, u, SELECTION, msd_type, exc):
128-
if exc is None:
129-
m = MSD(u, SELECTION, msd_type=msd_type, fft=False)
130-
assert m.dim_fac == 2
131-
assert m._dim == [0, 2]
132-
else:
133-
with pytest.raises(exc):
134-
MSD(u, SELECTION, msd_type=msd_type, fft=False)
123+
def test_msdtype_error(self, u, SELECTION, msdtype):
124+
errmsg = f"invalid msd_type: {msdtype}"
125+
with pytest.raises(ValueError, match=errmsg):
126+
m = MSD(u, SELECTION, msd_type=msdtype)
135127

136128
@pytest.mark.parametrize(
137129
"dim, dim_factor",

0 commit comments

Comments
 (0)