Skip to content

Commit afdb973

Browse files
authored
Merge pull request #382 from python/feature/unique-distributions-function
Extract _unique function for testing and potential public exposure.
2 parents 2df8b5a + da98092 commit afdb973

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

importlib_metadata/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,15 @@ def version(distribution_name):
10151015
return distribution(distribution_name).version
10161016

10171017

1018+
_unique = functools.partial(
1019+
unique_everseen,
1020+
key=operator.attrgetter('_normalized_name'),
1021+
)
1022+
"""
1023+
Wrapper for ``distributions`` to return unique distributions by name.
1024+
"""
1025+
1026+
10181027
def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
10191028
"""Return EntryPoint objects for all installed packages.
10201029
@@ -1032,10 +1041,8 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
10321041
10331042
:return: EntryPoints or SelectableGroups for all installed packages.
10341043
"""
1035-
norm_name = operator.attrgetter('_normalized_name')
1036-
unique = functools.partial(unique_everseen, key=norm_name)
10371044
eps = itertools.chain.from_iterable(
1038-
dist.entry_points for dist in unique(distributions())
1045+
dist.entry_points for dist in _unique(distributions())
10391046
)
10401047
return SelectableGroups.load(eps).select(**params)
10411048

tests/test_main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
EntryPoint,
1414
MetadataPathFinder,
1515
PackageNotFoundError,
16+
_unique,
1617
distributions,
1718
entry_points,
1819
metadata,
@@ -105,6 +106,21 @@ def test_dist_name_found_as_any_case(self):
105106
assert version(pkg_name.lower()) == '1.0'
106107
assert version(pkg_name.upper()) == '1.0'
107108

109+
def test_unique_distributions(self):
110+
"""
111+
Two distributions varying only by non-normalized name on
112+
the file system should resolve as the same.
113+
"""
114+
fixtures.build_files(self.make_pkg('abc'), self.site_dir)
115+
before = list(_unique(distributions()))
116+
117+
alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
118+
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
119+
fixtures.build_files(self.make_pkg('ABC'), alt_site_dir)
120+
after = list(_unique(distributions()))
121+
122+
assert len(after) == len(before)
123+
108124

109125
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
110126
@staticmethod

0 commit comments

Comments
 (0)