Skip to content

Commit feef19f

Browse files
committed
Warn when the pkg_resource metadata backend is used
with Python 3.11, 3.12, 3.13.
1 parent 06c8024 commit feef19f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

news/13318.removal.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A warning is emitted when the deprecated ``pkg_resources`` library is used to
2+
inspect and discover installed packages. This warning should only be visible to
3+
users who set an undocumented environment variable to disable the default
4+
``importlib.metadata`` backend.

src/pip/_internal/metadata/__init__.py

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from typing import List, Literal, Optional, Protocol, Type, cast
66

7+
from pip._internal.utils.deprecation import deprecated
78
from pip._internal.utils.misc import strtobool
89

910
from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
@@ -54,6 +55,31 @@ def _should_use_importlib_metadata() -> bool:
5455
return bool(getattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA", True))
5556

5657

58+
def _emit_pkg_resources_deprecation_if_needed() -> None:
59+
if sys.version_info < (3, 11):
60+
# All pip versions supporting Python<=3.11 will support pkg_resources,
61+
# and pkg_resources is the default for these, so let's not bother users.
62+
return
63+
64+
import importlib.metadata
65+
66+
if hasattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA"):
67+
# The Python distributor has set the global constant, so we don't
68+
# warn, since it is not a user decision.
69+
return
70+
71+
# The user has decided to use pkg_resources, so we warn.
72+
deprecated(
73+
reason="Using the pkg_resources metadata backend is deprecated.",
74+
replacement=(
75+
"to use the default importlib.metadata backend, "
76+
"by unsetting the _PIP_USE_IMPORTLIB_METADATA environment variable"
77+
),
78+
gone_in="26.3",
79+
issue=13317,
80+
)
81+
82+
5783
class Backend(Protocol):
5884
NAME: 'Literal["importlib", "pkg_resources"]'
5985
Distribution: Type[BaseDistribution]
@@ -66,6 +92,9 @@ def select_backend() -> Backend:
6692
from . import importlib
6793

6894
return cast(Backend, importlib)
95+
96+
_emit_pkg_resources_deprecation_if_needed()
97+
6998
from . import pkg_resources
7099

71100
return cast(Backend, pkg_resources)

0 commit comments

Comments
 (0)