Skip to content

Commit

Permalink
Merge pull request #382 from python/feature/unique-distributions-func…
Browse files Browse the repository at this point in the history
…tion

Extract _unique function for testing and potential public exposure.
  • Loading branch information
jaraco committed May 21, 2022
2 parents 2df8b5a + da98092 commit afdb973
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions importlib_metadata/__init__.py
Expand Up @@ -1015,6 +1015,15 @@ def version(distribution_name):
return distribution(distribution_name).version


_unique = functools.partial(
unique_everseen,
key=operator.attrgetter('_normalized_name'),
)
"""
Wrapper for ``distributions`` to return unique distributions by name.
"""


def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
"""Return EntryPoint objects for all installed packages.
Expand All @@ -1032,10 +1041,8 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
:return: EntryPoints or SelectableGroups for all installed packages.
"""
norm_name = operator.attrgetter('_normalized_name')
unique = functools.partial(unique_everseen, key=norm_name)
eps = itertools.chain.from_iterable(
dist.entry_points for dist in unique(distributions())
dist.entry_points for dist in _unique(distributions())
)
return SelectableGroups.load(eps).select(**params)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_main.py
Expand Up @@ -13,6 +13,7 @@
EntryPoint,
MetadataPathFinder,
PackageNotFoundError,
_unique,
distributions,
entry_points,
metadata,
Expand Down Expand Up @@ -105,6 +106,21 @@ def test_dist_name_found_as_any_case(self):
assert version(pkg_name.lower()) == '1.0'
assert version(pkg_name.upper()) == '1.0'

def test_unique_distributions(self):
"""
Two distributions varying only by non-normalized name on
the file system should resolve as the same.
"""
fixtures.build_files(self.make_pkg('abc'), self.site_dir)
before = list(_unique(distributions()))

alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
fixtures.build_files(self.make_pkg('ABC'), alt_site_dir)
after = list(_unique(distributions()))

assert len(after) == len(before)


class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
Expand Down

0 comments on commit afdb973

Please sign in to comment.