Skip to content

Commit

Permalink
Only use importlib.metadata on Python 3.10+
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jul 19, 2024
1 parent 6395653 commit c5434d4
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions babel/messages/_compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from functools import partial


Expand All @@ -9,19 +10,20 @@ def find_entrypoints(group_name: str):
Yields tuples of the entrypoint name and a callable function that will
load the actual entrypoint.
"""
try:
from importlib.metadata import entry_points
except ImportError:
pass
else:
eps = entry_points()
if isinstance(eps, dict): # Old structure before Python 3.10
group_eps = eps.get(group_name, [])
else: # New structure in Python 3.10+
group_eps = (ep for ep in eps if ep.group == group_name)
for entry_point in group_eps:
yield (entry_point.name, entry_point.load)
return
if sys.version_info >= (3, 10):
# "Changed in version 3.10: importlib.metadata is no longer provisional."
try:
from importlib.metadata import entry_points
except ImportError:
pass

Check warning on line 18 in babel/messages/_compat.py

View check run for this annotation

Codecov / codecov/patch

babel/messages/_compat.py#L17-L18

Added lines #L17 - L18 were not covered by tests
else:
eps = entry_points(group=group_name)
# Only do this if this implementation of `importlib.metadata` is
# modern enough to not return a dict.
if not isinstance(eps, dict):
for entry_point in eps:
yield (entry_point.name, entry_point.load)
return

try:
from pkg_resources import working_set
Expand Down

0 comments on commit c5434d4

Please sign in to comment.