Skip to content

Commit 5f62866

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

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

package/CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The rules for this file:
1515

1616
-------------------------------------------------------------------------------
1717
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2, marinegor,
18-
spyke7, talagayev
18+
spyke7, talagayev, tanii1125
1919

2020
* 2.11.0
2121

package/MDAnalysis/analysis/msd.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,9 @@ def _parse_msd_type(self):
384384

385385
try:
386386
self._dim = keys[self.msd_type.lower()]
387-
except AttributeError:
388-
raise TypeError("msd_type must be a string")
389-
except KeyError:
390-
raise ValueError(
391-
"invalid msd_type: {} specified, please specify one of xyz, "
392-
"xy, xz, yz, x, y, z".format(self.msd_type)
387+
except (AttributeError, KeyError):
388+
raise TypeError(
389+
"msd_type must be a string and one of: xyz, xy, xz, yz, x, y, z"
393390
)
394391

395392
self.dim_fac = len(self._dim)

testsuite/MDAnalysisTests/analysis/test_msd.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,21 @@ def test_updating_ag_rejected(self, u):
117117
with pytest.raises(TypeError, match=errmsg):
118118
m = MSD(updating_ag, msd_type="xyz", fft=False)
119119

120-
@pytest.mark.parametrize("msdtype", ["foo", "bar", "yx", "zyx"])
121-
def test_msdtype_error(self, u, SELECTION, msdtype):
122-
errmsg = f"invalid msd_type: {msdtype}"
123-
with pytest.raises(ValueError, match=errmsg):
124-
m = MSD(u, SELECTION, msd_type=msdtype)
125-
126-
def test_msd_type_uppercase(self, u, SELECTION):
127-
m = MSD(u, SELECTION, msd_type="Xz", fft=False)
128-
assert m.dim_fac == 2
129-
assert m._dim == [0, 2]
130-
131-
def test_msd_type_nonstring(self, u, SELECTION):
132-
with pytest.raises(TypeError):
133-
MSD(u, SELECTION, msd_type=123, fft=False)
120+
@pytest.mark.parametrize(
121+
"msd_type, exc",
122+
[
123+
("Xz", None), # valid, mixed case.
124+
(123, TypeError), # non-string.
125+
],
126+
)
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)
134135

135136
@pytest.mark.parametrize(
136137
"dim, dim_factor",

0 commit comments

Comments
 (0)