Skip to content

Commit ffb4947

Browse files
kiukchungfacebook-github-bot
authored andcommitted
(torchx/entrypoints) Use importlib.metadata instead of the backported importlib_metadata (#1087)
Summary: torchx dropped support for python<=3.7 2 years ago (see: #728). But we kept using the backported `importlib_metadata` module to load configurations from `entry_points`. Reviewed By: highker Differential Revision: D77619564
1 parent 7fabab4 commit ffb4947

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pyre-extensions
22
docstring-parser>=0.8.1
3-
importlib-metadata
43
pyyaml
54
docker
65
filelock

torchx/util/entrypoints.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
# pyre-strict
88

9+
from importlib import metadata
10+
from importlib.metadata import EntryPoint
911
from typing import Any, Dict, Optional
1012

11-
import importlib_metadata as metadata
12-
from importlib_metadata import EntryPoint
13+
14+
# pyre-ignore-all-errors[3, 2, 16]
1315

1416

15-
# pyre-ignore-all-errors[3, 2]
1617
def load(group: str, name: str, default=None):
1718
"""
1819
Loads the entry point specified by
@@ -30,13 +31,30 @@ def load(group: str, name: str, default=None):
3031
raises an error.
3132
"""
3233

33-
entrypoints = metadata.entry_points().select(group=group)
34+
# return type of importlib.metadata.entry_points() is different between python-3.9 and python-3.10
35+
# https://docs.python.org/3.9/library/importlib.metadata.html#importlib.metadata.entry_points
36+
# https://docs.python.org/3.10/library/importlib.metadata.html#importlib.metadata.entry_points
37+
if hasattr(metadata.entry_points(), "select"):
38+
# python>=3.10
39+
entrypoints = metadata.entry_points().select(group=group)
3440

35-
if name not in entrypoints.names and default is not None:
36-
return default
41+
if name not in entrypoints.names and default is not None:
42+
return default
43+
44+
ep = entrypoints[name]
45+
return ep.load()
3746

38-
ep = entrypoints[name]
39-
return ep.load()
47+
else:
48+
# python<3.10 (e.g. 3.9)
49+
# metadata.entry_points() returns tuple[EntryPoint] (not EntryPoints) in python-3.9
50+
entrypoints = metadata.entry_points().get(group, ())
51+
52+
for ep in entrypoints:
53+
if ep.name == name:
54+
return ep.load()
55+
56+
# [group].name not found
57+
return default
4058

4159

4260
def _defer_load_ep(ep: EntryPoint) -> object:
@@ -49,7 +67,6 @@ def run(*args: object, **kwargs: object) -> object:
4967
return run
5068

5169

52-
# pyre-ignore-all-errors[3, 2]
5370
def load_group(
5471
group: str, default: Optional[Dict[str, Any]] = None, skip_defaults: bool = False
5572
):

torchx/util/test/entrypoints_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import unittest
1010
from configparser import ConfigParser
11+
12+
from importlib.metadata import EntryPoint, EntryPoints
1113
from types import ModuleType
1214
from typing import List
1315
from unittest.mock import MagicMock, patch
1416

15-
from importlib_metadata import EntryPoint, EntryPoints
16-
1717
from torchx.util.entrypoints import load, load_group
1818

1919

0 commit comments

Comments
 (0)