Skip to content

Commit abc13a2

Browse files
committed
Prefer an OO approach for _normalized_name. Allows other providers to supply _normalized_name.
1 parent fddbf5d commit abc13a2

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

importlib_metadata/__init__.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
NullFinder,
2121
PyPy_repr,
2222
install,
23-
singledispatch,
2423
)
2524
from ._functools import method_cache
2625
from ._itertools import unique_everseen
@@ -499,6 +498,11 @@ def name(self):
499498
"""Return the 'Name' metadata for the distribution package."""
500499
return self.metadata['Name']
501500

501+
@property
502+
def _normalized_name(self):
503+
"""Return a normalized version of the name."""
504+
return Prepared.normalize(self.name)
505+
502506
@property
503507
def version(self):
504508
"""Return the 'Version' metadata for the distribution package."""
@@ -806,6 +810,12 @@ def read_text(self, filename):
806810
def locate_file(self, path):
807811
return self._path.parent / path
808812

813+
@property
814+
def _normalized_name(self):
815+
stem = os.path.basename(str(self._path))
816+
name, sep, rest = stem.partition('-')
817+
return name
818+
809819

810820
def distribution(distribution_name):
811821
"""Get the ``Distribution`` instance for the named package.
@@ -843,18 +853,6 @@ def version(distribution_name):
843853
return distribution(distribution_name).version
844854

845855

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-
858856
def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
859857
"""Return EntryPoint objects for all installed packages.
860858
@@ -872,7 +870,8 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
872870
873871
:return: EntryPoints or SelectableGroups for all installed packages.
874872
"""
875-
unique = functools.partial(unique_everseen, key=normalized_name)
873+
norm_name = operator.attrgetter('_normalized_name')
874+
unique = functools.partial(unique_everseen, key=norm_name)
876875
eps = itertools.chain.from_iterable(
877876
dist.entry_points for dist in unique(distributions())
878877
)

importlib_metadata/_compat.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
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-
2418
def install(cls):
2519
"""
2620
Class decorator for installation on sys.meta_path.

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ 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"
2524

2625
[options.packages.find]
2726
exclude =

0 commit comments

Comments
 (0)