forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_importlib_metadata.py
More file actions
67 lines (55 loc) · 2.11 KB
/
Copy path_importlib_metadata.py
File metadata and controls
67 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
"""
Caching and compatibility wrapper for standard library ``importlib.metadata``.
This module caches ``entry_points()`` to avoid reloading entry points from disk on every call.
It also normalizes minor differences across python versions 3.10+. References to issues:
- https://github.com/open-telemetry/opentelemetry-python/pull/4735
- https://github.com/open-telemetry/opentelemetry-python/pull/5203
- https://github.com/open-telemetry/opentelemetry-python/issues/5231
"""
import warnings
from functools import cache
from importlib.metadata import (
Distribution,
EntryPoint,
EntryPoints,
PackageNotFoundError,
distributions,
requires,
version,
)
from importlib.metadata import entry_points as original_entry_points
from typing import Any
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)
@cache
def _original_entry_points_cached() -> EntryPoints:
# On Python 3.10 and 3.11, calling entry_points() without arguments returns
# a SelectableGroups object. Its .values() method emits a DeprecationWarning
# ("SelectableGroups dict interface is deprecated. Use select.") that cannot
# be avoided while caching all entry points in a single upfront call.
# The @cache decorator ensures this path runs only once per interpreter
# session, so suppressing the warning here is both safe and targeted.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message="SelectableGroups dict interface is deprecated",
)
return _as_entry_points(original_entry_points())
def entry_points(**params) -> EntryPoints:
return _original_entry_points_cached().select(**params)
__all__ = [
"entry_points",
"version",
"EntryPoint",
"EntryPoints",
"requires",
"Distribution",
"distributions",
"PackageNotFoundError",
]