Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5238.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid import-time deprecation warnings on Python 3.10 and 3.11.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- https://github.com/open-telemetry/opentelemetry-python/pull/5203
"""

import warnings
from functools import cache
from importlib.metadata import (
Distribution,
Expand All @@ -29,7 +30,13 @@ def _as_entry_points(eps: Any) -> EntryPoints:
if isinstance(eps, EntryPoints):
return eps
# Handle Python 3.10 SelectableGroups (dict-like)
return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)
with warnings.catch_warnings():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for going back here - I just realized that this context manager is not thread safe. Just to be extra cautious, it's probably best to go back to calling select on each group like you had previously, but still without the conditional using "hasattr".

warnings.filterwarnings(
"ignore",
message=r"SelectableGroups dict interface is deprecated\. Use select\.",
category=DeprecationWarning,
)
return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)


@cache
Expand Down
22 changes: 22 additions & 0 deletions opentelemetry-api/tests/util/test__importlib_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

import importlib.metadata
import warnings
from unittest import TestCase

from opentelemetry.metrics import MeterProvider
Expand Down Expand Up @@ -117,3 +119,23 @@ def test_as_entry_points_selectable_groups_compat(self):
self.assertIsInstance(normalized, EntryPoints)
self.assertEqual(len(normalized), 2)
self.assertEqual(list(normalized), [ep1, ep2])

def test_as_entry_points_selectable_groups_without_warning(self):
selectable_groups_class = getattr(
importlib.metadata, "SelectableGroups", None
)
if selectable_groups_class is None:
self.skipTest("SelectableGroups is not available")

ep1 = EntryPoint(name="foo", value="bar:baz", group="gp")
ep2 = EntryPoint(name="foo2", value="bar2:baz2", group="gp2")
selectable_groups = selectable_groups_class(
{"gp": [ep1], "gp2": [ep2]}
)

with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
normalized = _as_entry_points(selectable_groups)

self.assertIsInstance(normalized, EntryPoints)
self.assertCountEqual(normalized, [ep1, ep2])