Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/importlib_metadata/_py39compat.py: 27%
15 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1"""
2Compatibility layer with Python 3.8/3.9
3"""
4from typing import TYPE_CHECKING, Any, Optional, Tuple
6if TYPE_CHECKING: # pragma: no cover
7 # Prevent circular imports on runtime.
8 from . import Distribution, EntryPoint
9else:
10 Distribution = EntryPoint = Any
13def normalized_name(dist: Distribution) -> Optional[str]:
14 """
15 Honor name normalization for distributions that don't provide ``_normalized_name``.
16 """
17 try:
18 return dist._normalized_name
19 except AttributeError:
20 from . import Prepared # -> delay to prevent circular imports.
22 return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])
25def ep_matches(ep: EntryPoint, **params) -> Tuple[EntryPoint, bool]:
26 """
27 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.
32 For example, the following sequences of expressions should be compatible:
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]
38 # Sequence 2: using Python 3.9+
39 [ep for ep in entry_points if ep.matches(**params)]
40 """
41 try:
42 return ep, ep.matches(**params)
43 except AttributeError:
44 from . import EntryPoint # -> delay to prevent circular imports.
46 # 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)