Skip to content

Commit 56b071f

Browse files
committed
Add compatibility for EntryPoint.matches in stdlib 3.8/3.9
1 parent eb621bd commit 56b071f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

importlib_metadata/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from importlib import import_module
3030
from importlib.abc import MetaPathFinder
3131
from itertools import starmap
32-
from typing import List, Mapping, Optional, Union
32+
from typing import List, Mapping, Optional, Tuple, Union
3333

3434

3535
__all__ = [
@@ -378,7 +378,8 @@ def select(self, **params):
378378
Select entry points from self that match the
379379
given parameters (typically group and/or name).
380380
"""
381-
return EntryPoints(ep for ep in self if ep.matches(**params))
381+
candidates = (_ep_matches(ep, **params) for ep in self)
382+
return EntryPoints(ep for ep, ep_matches in candidates if ep_matches)
382383

383384
@property
384385
def names(self):
@@ -410,6 +411,15 @@ def _from_text(text):
410411
)
411412

412413

414+
def _ep_matches(ep: EntryPoint, **params) -> Tuple[EntryPoint, bool]:
415+
"""Compatibility layer for EntryPoint objects in Python 3.8/3.9 stdlib."""
416+
try:
417+
return ep, ep.matches(**params)
418+
except AttributeError:
419+
_ep = EntryPoint(ep.name, ep.value, ep.group)
420+
return _ep, _ep.matches(**params)
421+
422+
413423
class Deprecated:
414424
"""
415425
Compatibility add-in for mapping to indicate that
@@ -1021,7 +1031,8 @@ def _compat_normalized_name(dist: Distribution) -> Optional[str]:
10211031
that don't provide ``_normalized_name``
10221032
(as in ``importlib.metadata`` for Python 3.8/3.9).
10231033
"""
1024-
return getattr(dist, '_normalized_name', None) or Prepared.normalize(dist.name)
1034+
normalized = getattr(dist, '_normalized_name', None)
1035+
return normalized or Prepared.normalize(getattr(dist, "name", ""))
10251036

10261037

10271038
_unique = functools.partial(

0 commit comments

Comments
 (0)