1"""
2Compatibility layer with Python 3.8/3.9
3"""
4
5from typing import TYPE_CHECKING, Any, Optional
6
7if TYPE_CHECKING: # pragma: no cover
8 # Prevent circular imports on runtime.
9 from .. import Distribution, EntryPoint
10else:
11 Distribution = EntryPoint = Any
12
13
14def normalized_name(dist: Distribution) -> Optional[str]:
15 """
16 Honor name normalization for distributions that don't provide ``_normalized_name``.
17 """
18 try:
19 return dist._normalized_name
20 except AttributeError:
21 from .. import Prepared # -> delay to prevent circular imports.
22
23 return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])
24
25
26def ep_matches(ep: EntryPoint, **params) -> bool:
27 """
28 Workaround for ``EntryPoint`` objects without the ``matches`` method.
29 """
30 try:
31 return ep.matches(**params)
32 except AttributeError:
33 from .. import EntryPoint # -> delay to prevent circular imports.
34
35 # Reconstruct the EntryPoint object to make sure it is compatible.
36 return EntryPoint(ep.name, ep.value, ep.group).matches(**params)