Skip to content

Commit

Permalink
Add docstrings to the compatibility shim. Give primacy to group looku…
Browse files Browse the repository at this point in the history
…p in compatibility shim.
  • Loading branch information
jaraco committed Feb 23, 2021
1 parent 9d55a33 commit d6f7c20
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,38 @@ def _from_text_for(cls, text, dist):


class LegacyGroupedEntryPoints(EntryPoints):
"""
Compatibility wrapper around EntryPoints to provide
much of the 'dict' interface previously returned by
entry_points.
"""

def __getitem__(self, name) -> Union[EntryPoint, 'EntryPoints']:
try:
return super().__getitem__(name)
except KeyError:
if name not in self.groups:
raise
"""
When accessed by name that matches a group, return the group.
"""
group = self.select(group=name)
if group:
msg = "GroupedEntryPoints.__getitem__ is deprecated for groups. Use select."
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return group

msg = "GroupedEntryPoints.__getitem__ is deprecated for groups. Use select."
warnings.warn(msg, DeprecationWarning)
return self.select(group=name)
return super().__getitem__(name)

def get(self, group, default=None):
"""
For backward compatibility, supply .get
For backward compatibility, supply .get.
"""
is_flake8 = any('flake8' in str(frame) for frame in inspect.stack())
msg = "GroupedEntryPoints.get is deprecated. Use select."
is_flake8 or warnings.warn(msg, DeprecationWarning)
is_flake8 or warnings.warn(msg, DeprecationWarning, stacklevel=2)
return self.select(group=group) or default

def select(self, **params):
"""
Prevent transform to EntryPoints during call to entry_points if
no selection parameters were passed.
"""
if not params:
return self
return super().select(**params)
Expand Down

0 comments on commit d6f7c20

Please sign in to comment.