py39.py 1.2 KB

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