Skip to content

Commit

Permalink
Issue a deprecation warning for Universe.<segid>
Browse files Browse the repository at this point in the history
Fixes #1478
  • Loading branch information
jbarnoud committed Jul 15, 2017
1 parent 277127c commit 6620143
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Fixes
ENCORE analysis package (issue #1390)
* Fix extra deprecation warnings for instant segment and residue selectors
(Issue #1476)
* Accessing segments from a universe with an instant selector now issues a
deprecation warning as expected (Issue #1478)

Changes
* remove deprecated TimeSeriesCollection
Expand Down
26 changes: 25 additions & 1 deletion package/MDAnalysis/core/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import logging
import copy
import uuid
import warnings

import MDAnalysis
import sys
Expand Down Expand Up @@ -205,6 +206,10 @@ class Universe(object):
"""

def __init__(self, *args, **kwargs):
# Store the segments for the deprecated instant selector feature.
# This attribute has to be defined early to avoid recursion in
# __getattr__.
self._instant_selectors = {}
# hold on to copy of kwargs; used by external libraries that
# reinitialize universes
self._kwargs = copy.deepcopy(kwargs)
Expand Down Expand Up @@ -342,7 +347,26 @@ def _generate_from_topology(self):
# if len 1 SegmentGroup, convert to Segment
if len(segment) == 1:
segment = segment[0]
self.__dict__[name] = segment
self._instant_selectors[name] = segment

def __getattr__(self, key):
# This implements the instant selector of segments from a Universe.
# It is implemented as __getattr__ so a deprecation warning can be
# issued when the feature is used. Instant selectors are deprecated
# since version 0.16.2 and are tareted to be deleted in version 1.0.
# self._instant_selectors is populated in self._process_attr and
# created at the beginning of __init__.
try:
segment = self._instant_selectors[key]
except KeyError:
raise AttributeError('No attribute "{}".'.format(key))
else:
warnings.warn("Instant selector Universe.<segid> "
"is deprecated and will be removed in 1.0. "
"Use SegmentGroup[SegmentGroup.segids == '<segid>'] "
"instead.",
DeprecationWarning)
return segment

@property
def universe(self):
Expand Down
3 changes: 1 addition & 2 deletions testsuite/MDAnalysisTests/core/test_topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ def universe():
'universe.atoms.CA',
'universe.residues.LYS',
'universe.segments.s4AKE',
pytest.param('universe.s4AKE',
marks=pytest.mark.xfail(reason="Issue #1478")),
'universe.s4AKE',
))
def test_deprecation(self, universe, instruction):
"""Test that the warnings are issued when required.
Expand Down

0 comments on commit 6620143

Please sign in to comment.