Skip to content

fix(api): avoid SelectableGroups.values() DeprecationWarning on Python 3.10/3.11#5255

Closed
me-saurabhkohli wants to merge 1 commit into
open-telemetry:mainfrom
me-saurabhkohli:fix/importlib-metadata-deprecation-warning-py310-py311
Closed

fix(api): avoid SelectableGroups.values() DeprecationWarning on Python 3.10/3.11#5255
me-saurabhkohli wants to merge 1 commit into
open-telemetry:mainfrom
me-saurabhkohli:fix/importlib-metadata-deprecation-warning-py310-py311

Conversation

@me-saurabhkohli
Copy link
Copy Markdown

Fixes #5231.

Problem

opentelemetry-api v1.42.0 introduced _as_entry_points() in _importlib_metadata.py (via #5203) to normalise the return value of importlib.metadata.entry_points() across Python versions. On Python 3.10 and 3.11, entry_points() returns a SelectableGroups object whose dict interface is deprecated. The current implementation iterates it via .values(), which unconditionally emits:

DeprecationWarning: SelectableGroups dict interface is deprecated. Use select.

Projects that run CI with -W error (warnings-as-errors) — which is common best practice — see this as an import-time error, breaking their test suites after upgrading from v1.41.1 to v1.42.0.

# Reproduces the error:
uv run --isolated -p 3.11 --with opentelemetry-api==1.42.0 python -W error -c 'import opentelemetry.context'

Root cause

In _as_entry_points():

# Before (triggers DeprecationWarning on 3.10/3.11)
return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)

Fix

Prefer .select() (called without filters) over .values(), which is the officially documented migration path for the SelectableGroups dict interface. Both SelectableGroups (3.10/3.11) and the newer EntryPoints (3.12+) expose .select(), so a hasattr guard cleanly handles both cases without relying on isinstance checks against a private type:

# After (no warning on any Python version)
if hasattr(eps, "select"):
    return eps.select()
return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)

Testing

# Should exit 0 after this fix (currently exits non-zero on 1.42.0):
uv run --isolated -p 3.10 --with opentelemetry-api python -W error -c 'import opentelemetry.context'
uv run --isolated -p 3.11 --with opentelemetry-api python -W error -c 'import opentelemetry.context'
uv run --isolated -p 3.12 --with opentelemetry-api python -W error -c 'import opentelemetry.context'

…n 3.10/3.11

Fixes open-telemetry#5231.

In v1.42.0, _as_entry_points() was added to normalise the return value of
importlib.metadata.entry_points() across Python versions.  On Python 3.10
and 3.11, entry_points() returns a SelectableGroups object whose dict
interface is deprecated; iterating it via .values() unconditionally emits:

  DeprecationWarning: SelectableGroups dict interface is deprecated.
  Use select.

This turns into an error for projects that run their test suites with
-W error (warnings-as-errors), which is a common CI configuration.

Fix: prefer .select() (available on both SelectableGroups and the newer
EntryPoints) over .values() when the object exposes it.  This is the
officially recommended migration path documented in importlib.metadata.
Copilot AI review requested due to automatic review settings May 29, 2026 04:31
@me-saurabhkohli me-saurabhkohli requested a review from a team as a code owner May 29, 2026 04:31
@linux-foundation-easycla
Copy link
Copy Markdown

CLA Not Signed

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates the entry points normalization helper to avoid DeprecationWarning on Python 3.10/3.11 by flattening SelectableGroups via .select() instead of iterating .values().

Changes:

  • Added a reference to issue #5231 in the module docstring.
  • Updated _as_entry_points to use eps.select() when available to avoid deprecated dict-like iteration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +32 to 42
# On Python 3.10 and 3.11, entry_points() returns a SelectableGroups
# object whose dict interface is deprecated and emits a DeprecationWarning
# when iterated via .values(). The correct way to flatten the groups is
# to call .select() without filters, which returns a flat EntryPoints list
# without triggering the deprecation warning.
# See: https://github.com/open-telemetry/opentelemetry-python/issues/5231
if hasattr(eps, "select"):
return eps.select()
# Fallback for any other dict-like structure (should not be reached in
# practice but kept for robustness).
return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)
@herin049
Copy link
Copy Markdown
Contributor

This fix is not correct, and we also already have a PR open which fixes this properly. Closing this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Importing opentelemetry.context issues a warning in Python 3.10 and 3.11

3 participants