Skip to content

Warn when the pkg_resource metadata backend is used with Python 3.11, 3.12, 3.13 #13318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
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
4 changes: 4 additions & 0 deletions news/13318.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A warning is emitted when the deprecated ``pkg_resources`` library is used to
inspect and discover installed packages. This warning should only be visible to
users who set an undocumented environment variable to disable the default
``importlib.metadata`` backend.
29 changes: 29 additions & 0 deletions src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from typing import List, Literal, Optional, Protocol, Type, cast

from pip._internal.utils.deprecation import deprecated
from pip._internal.utils.misc import strtobool

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


def _emit_pkg_resources_deprecation_if_needed() -> None:
if sys.version_info < (3, 11):
# All pip versions supporting Python<=3.11 will support pkg_resources,
# and pkg_resources is the default for these, so let's not bother users.
return

import importlib.metadata

if hasattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA"):
# The Python distributor has set the global constant, so we don't
# warn, since it is not a user decision.
return
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not entirely sure we should not warn the user here.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not aware of any redistributor setting this attribute. Last time we reached out to our distro redistributors, none said they needed to use pkg_resources which implies that they didn't use this attribute either.


# The user has decided to use pkg_resources, so we warn.
deprecated(
reason="Using the pkg_resources metadata backend is deprecated.",
replacement=(
"to use the default importlib.metadata backend, "
"by unsetting the _PIP_USE_IMPORTLIB_METADATA environment variable"
),
gone_in="26.3",
issue=13317,
)


class Backend(Protocol):
NAME: 'Literal["importlib", "pkg_resources"]'
Distribution: Type[BaseDistribution]
Expand All @@ -66,6 +92,9 @@ def select_backend() -> Backend:
from . import importlib

return cast(Backend, importlib)

_emit_pkg_resources_deprecation_if_needed()

from . import pkg_resources

return cast(Backend, pkg_resources)
Expand Down