Skip to content

Commit ebe1333

Browse files
committed
Use normalized names to distinguish unique distributions for performance. Fixes #283.
1 parent 277d669 commit ebe1333

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

importlib_metadata/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
NullFinder,
2121
PyPy_repr,
2222
install,
23+
singledispatch,
2324
)
2425
from ._functools import method_cache
2526
from ._itertools import unique_everseen
@@ -842,6 +843,18 @@ def version(distribution_name):
842843
return distribution(distribution_name).version
843844

844845

846+
@singledispatch
847+
def normalized_name(dist: Distribution):
848+
return Prepared.normalize(dist.name)
849+
850+
851+
@normalized_name.register
852+
def _(dist: PathDistribution):
853+
stem = os.path.basename(str(dist._path))
854+
name, sep, rest = stem.partition('-')
855+
return name
856+
857+
845858
def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
846859
"""Return EntryPoint objects for all installed packages.
847860
@@ -859,7 +872,7 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
859872
860873
:return: EntryPoints or SelectableGroups for all installed packages.
861874
"""
862-
unique = functools.partial(unique_everseen, key=operator.attrgetter('name'))
875+
unique = functools.partial(unique_everseen, key=normalized_name)
863876
eps = itertools.chain.from_iterable(
864877
dist.entry_points for dist in unique(distributions())
865878
)

importlib_metadata/_compat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
from typing_extensions import Protocol # type: ignore
1616

1717

18+
if sys.version_info < (3, 7):
19+
from singledispatch import singledispatch
20+
else:
21+
from functools import singledispatch # noqa: F401
22+
23+
1824
def install(cls):
1925
"""
2026
Class decorator for installation on sys.meta_path.

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ python_requires = >=3.6
2121
install_requires =
2222
zipp>=0.5
2323
typing-extensions>=3.6.4; python_version < "3.8"
24+
singledispatch; python_version < "3.7"
2425

2526
[options.packages.find]
2627
exclude =

0 commit comments

Comments
 (0)