Skip to content

Commit ab154a2

Browse files
committed
Make entry_points behave the same across Python versions
Fixes open-telemetry#3167
1 parent f05fa77 commit ab154a2

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,51 @@
1717
# FIXME remove this when support for 3.7 is dropped.
1818
if version_info.minor == 7:
1919
# pylint: disable=import-error
20-
from importlib_metadata import entry_points, version # type: ignore
20+
from importlib_metadata import ( # type: ignore
21+
EntryPoint,
22+
entry_points,
23+
version,
24+
)
2125

2226
# FIXME remove this file when support for 3.9 is dropped.
2327
elif version_info.minor in (8, 9):
2428
# pylint: disable=import-error
29+
from importlib.metadata import EntryPoint
2530
from importlib.metadata import (
2631
entry_points as importlib_metadata_entry_points,
2732
)
2833
from importlib.metadata import version
2934

30-
def entry_points(group: str, name: str): # type: ignore
35+
def entry_points(group: str = None, name: str = None): # type: ignore
36+
37+
if group is None:
38+
return importlib_metadata_entry_points()
39+
40+
if name is None:
41+
return importlib_metadata_entry_points()[group]
42+
3143
for entry_point in importlib_metadata_entry_points()[group]:
3244
if entry_point.name == name:
33-
yield entry_point
45+
return [entry_point]
46+
47+
__all__ = [
48+
"entry_points",
49+
"version",
50+
]
3451

3552
else:
36-
from importlib.metadata import entry_points, version
53+
from importlib.metadata import (
54+
EntryPoint,
55+
EntryPoints,
56+
SelectableGroups,
57+
entry_points,
58+
version,
59+
)
3760

38-
__all__ = ["entry_points", "version"]
61+
__all__ = [
62+
"entry_points",
63+
"version",
64+
"EntryPoint",
65+
"SelectableGroups",
66+
"EntryPoints",
67+
]

opentelemetry-api/tests/util/test__importlib_metadata.py

+62-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
# limitations under the License.
1414

1515
from unittest import TestCase
16+
from warnings import catch_warnings, filterwarnings
1617

1718
from opentelemetry.metrics import MeterProvider
18-
from opentelemetry.util._importlib_metadata import entry_points
19+
from opentelemetry.util._importlib_metadata import EntryPoint
20+
from opentelemetry.util._importlib_metadata import (
21+
entry_points as entry_points_function, # SelectableGroups,; EntryPoints
22+
)
1923

2024

2125
class TestEntryPoints(TestCase):
@@ -24,11 +28,67 @@ def test_entry_points(self):
2428
self.assertIsInstance(
2529
next(
2630
iter(
27-
entry_points(
31+
entry_points_function(
2832
group="opentelemetry_meter_provider",
2933
name="default_meter_provider",
3034
)
3135
)
3236
).load()(),
3337
MeterProvider,
3438
)
39+
40+
def test_uniform_behavior(self):
41+
"""
42+
Test that entry_points behaves the same regardless of the Python
43+
version.
44+
"""
45+
46+
selectable_groups = entry_points_function()
47+
48+
# self.assertIsInstance(selectable_groups, SelectableGroups)
49+
50+
# Supressing the following warning here:
51+
# DeprecationWarning: SelectableGroups dict interface is deprecated. Use select.
52+
# The behavior of the importlib metadata library is hard to understand,
53+
# this is True: selectable_groups is selectable_groups.select(). So,
54+
# using select, as the warning says yields the same problem. Also
55+
# select does not accept any parameters.
56+
57+
with catch_warnings():
58+
filterwarnings("ignore", category=DeprecationWarning)
59+
entry_points = selectable_groups.select()[
60+
"opentelemetry_propagator"
61+
]
62+
63+
# Supressing the following warning here:
64+
# DeprecationWarning: DeprecationWarning: Accessing entry points by index is deprecated. Cast to tuple if needed.
65+
# The behavior of the importlib metadata library is hard to understand,
66+
# this is True: entry_points == .select(). So, using select, as the
67+
# warning says yields the same problem. Also select does not accept any
68+
# parameters.
69+
with catch_warnings():
70+
filterwarnings("ignore", category=DeprecationWarning)
71+
72+
self.assertIsInstance(entry_points.select()[0], EntryPoint)
73+
74+
entry_points = entry_points_function(
75+
group="opentelemetry_propagator"
76+
)
77+
78+
self.assertIsInstance(entry_points.select()[0], EntryPoint)
79+
80+
entry_points = entry_points_function(
81+
group="opentelemetry_propagator", name="baggage"
82+
)
83+
84+
self.assertIsInstance(entry_points.select()[0], EntryPoint)
85+
86+
entry_points = entry_points_function(group="abc")
87+
88+
self.assertEqual(entry_points, [])
89+
90+
entry_points = entry_points_function(
91+
group="opentelemetry_propagator", name="abc"
92+
)
93+
94+
self.assertEqual(entry_points, [])

0 commit comments

Comments
 (0)