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