_py39compat.py 1.1 KB

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