Skip to content

Commit

Permalink
Raise TypeError when calling AtomGroup[None] (#3517)
Browse files Browse the repository at this point in the history
Fixes #3092
* Raise TypeError when a group is indexed with None
  • Loading branch information
jbarnoud authored Feb 3, 2022
1 parent e482c39 commit 18bad9d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Enhancements
* Added MDAnalysis.lib.distances.minimize_vectors

Changes
* Indexing a group (AtomGroup, ResidueGroup, or SegmentGroup) with None
raises a TypeError (Issue #3092, PR #3517)
* TRZReader now defaults to a `dt` of 1.0 (instead of 0.0 previously) when
it cannot be obtained from file (Issue #3257)
* Dropped python 3.6 support and raised minimum numpy version to 1.18.0
Expand Down
10 changes: 9 additions & 1 deletion package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ def __getitem__(self, item):
# because our _ix attribute is a numpy array
# it can be sliced by all of these already,
# so just return ourselves sliced by the item
if isinstance(item, numbers.Integral):
if item is None:
raise TypeError('None cannot be used to index a group.')
elif isinstance(item, numbers.Integral):
return self.level.singular(self.ix[item], self.universe)
else:
if isinstance(item, list) and item: # check for empty list
Expand Down Expand Up @@ -2488,6 +2490,8 @@ class AtomGroup(GroupBase):
.. versionchanged:: 2.0.0
:class:`AtomGroup` can always be pickled with or without its universe,
instead of failing when not finding its anchored universe.
.. versionchanged:: 2.1.0
Indexing an AtomGroup with ``None`` raises a ``TypeError``.
"""

def __reduce__(self):
Expand Down Expand Up @@ -3582,6 +3586,8 @@ class ResidueGroup(GroupBase):
*Instant selectors* of Segments will be removed in the 1.0 release.
.. versionchanged:: 1.0.0
Removed instant selectors, use select_atoms instead
.. versionchanged:: 2.1.0
Indexing an ResidueGroup with ``None`` raises a ``TypeError``.
"""

@property
Expand Down Expand Up @@ -3773,6 +3779,8 @@ class SegmentGroup(GroupBase):
*Instant selectors* of Segments will be removed in the 1.0 release.
.. versionchanged:: 1.0.0
Removed instant selectors, use select_atoms instead
.. versionchanged:: 2.1.0
Indexing an SegmentGroup with ``None`` raises a ``TypeError``.
"""

@property
Expand Down
4 changes: 4 additions & 0 deletions testsuite/MDAnalysisTests/core/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ def test_integer_getitem(self, group, nparray, idx, singular):

assert a.ix == ref
assert isinstance(a, singular)

def test_none_getitem(self, group):
with pytest.raises(TypeError):
group[None]


def _yield_groups(group_dict, singles, levels, groupclasses, repeat):
Expand Down

0 comments on commit 18bad9d

Please sign in to comment.