Skip to content

Commit 342a94b

Browse files
committed
Add deprecated .get to GroupedEntryPoints and test to capture expectation.
1 parent 28adeb8 commit 342a94b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

importlib_metadata/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ def __getitem__(self, group) -> EntryPoints:
183183
def groups(self):
184184
return set(ep.group for ep in self)
185185

186+
def get(self, group, default=None):
187+
"""
188+
For backward compatibility, supply .get
189+
"""
190+
msg = "GroupedEntryPoints.get is deprecated. Just use __getitem__."
191+
warnings.warn(msg, DeprecationWarning)
192+
return self[group] or default
193+
186194

187195
class PackagePath(pathlib.PurePosixPath):
188196
"""A reference to a path in a package"""

tests/test_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ def test_entry_points_dict_construction(self):
104104
assert expected.category is DeprecationWarning
105105
assert "Construction of dict of EntryPoints is deprecated" in str(expected)
106106

107+
def test_entry_points_groups_get(self):
108+
"""
109+
Prior versions of entry_points() returned a dict. Ensure
110+
that callers using '.get()' are supported but warned to
111+
migrate.
112+
"""
113+
with warnings.catch_warnings(record=True):
114+
entry_points().get('missing', 'default') == 'default'
115+
entry_points().get('entries', 'default') == entry_points()['entries']
116+
entry_points().get('missing', ()) == entry_points()['missing']
117+
107118
def test_metadata_for_this_package(self):
108119
md = metadata('egginfo-pkg')
109120
assert md['author'] == 'Steven Ma'

0 commit comments

Comments
 (0)