Skip to content

Commit e29e557

Browse files
authored
Merge pull request #408 from python/debt/simpler-compat-wrapper
Simplify compatibility wrappers
2 parents 009ace3 + ecb363c commit e29e557

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

importlib_metadata/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ def select(self, **params):
293293
Select entry points from self that match the
294294
given parameters (typically group and/or name).
295295
"""
296-
candidates = (_py39compat.ep_matches(ep, **params) for ep in self)
297-
return EntryPoints(ep for ep, predicate in candidates if predicate)
296+
return EntryPoints(ep for ep in self if _py39compat.ep_matches(ep, **params))
298297

299298
@property
300299
def names(self):

importlib_metadata/_py39compat.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Compatibility layer with Python 3.8/3.9
33
"""
4-
from typing import TYPE_CHECKING, Any, Optional, Tuple
4+
from typing import TYPE_CHECKING, Any, Optional
55

66
if TYPE_CHECKING: # pragma: no cover
77
# Prevent circular imports on runtime.
@@ -22,27 +22,14 @@ def normalized_name(dist: Distribution) -> Optional[str]:
2222
return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])
2323

2424

25-
def ep_matches(ep: EntryPoint, **params) -> Tuple[EntryPoint, bool]:
25+
def ep_matches(ep: EntryPoint, **params) -> bool:
2626
"""
2727
Workaround for ``EntryPoint`` objects without the ``matches`` method.
28-
For the sake of convenience, a tuple is returned containing not only the
29-
boolean value corresponding to the predicate evalutation, but also a compatible
30-
``EntryPoint`` object that can be safely used at a later stage.
31-
32-
For example, the following sequences of expressions should be compatible:
33-
34-
# Sequence 1: using the compatibility layer
35-
candidates = (_py39compat.ep_matches(ep, **params) for ep in entry_points)
36-
[ep for ep, predicate in candidates if predicate]
37-
38-
# Sequence 2: using Python 3.9+
39-
[ep for ep in entry_points if ep.matches(**params)]
4028
"""
4129
try:
42-
return ep, ep.matches(**params)
30+
return ep.matches(**params)
4331
except AttributeError:
4432
from . import EntryPoint # -> delay to prevent circular imports.
4533

4634
# Reconstruct the EntryPoint object to make sure it is compatible.
47-
_ep = EntryPoint(ep.name, ep.value, ep.group)
48-
return _ep, _ep.matches(**params)
35+
return EntryPoint(ep.name, ep.value, ep.group).matches(**params)

0 commit comments

Comments
 (0)