Skip to content

Commit

Permalink
Silently ignore pkg_resources override on 3.12+
Browse files Browse the repository at this point in the history
This customization is generally provided by the distributor, and the
user can do nothing if the environment is configured incorrectly. So
instead of breaking pip entirely, we silently ignore the customization
and let the distributor correct the situation.
  • Loading branch information
uranusjr committed Jan 3, 2023
1 parent 5b1afe5 commit 7cfc915
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
10 changes: 5 additions & 5 deletions news/11501.process.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Emit an error to prevent the ``pkg_resources`` metadata backend from being used
on Python 3.12 or later. This backend exists for backward compatibility, and
should only be used on newer Python versions by Python distributors that cannot
correctly implement ``importlib.metadata`` support in a timely fashion. Time is
up for transition since CPython is now removing mechanisms that ``pkg_resource``
Prevent the ``pkg_resources`` metadata backend from being used on Python 3.12
or later. This backend exists for backward compatibility, and should only be
used on newer Python versions by Python distributors that cannot correctly
implement ``importlib.metadata`` support in a timely fashion. Time is up for
transition since CPython is now removing mechanisms that ``pkg_resource``
relies on, and the backend will no longer work on Python 3.12 or later.
11 changes: 4 additions & 7 deletions src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys
from typing import TYPE_CHECKING, List, Optional, Type, cast

from pip._internal.exceptions import PipError
from pip._internal.utils.misc import strtobool

from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
Expand All @@ -31,7 +30,8 @@ def _should_use_importlib_metadata() -> bool:
"""Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend.
By default, pip uses ``importlib.metadata`` on Python 3.11+, and
``pkg_resourcess`` otherwise. This can be overridden by a couple of ways:
``pkg_resources`` otherwise. On Python versions prior to 3.12, this can be
overridden by a couple of ways for transitional purposes:
* If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
dictates whether ``importlib.metadata`` is used, regardless of Python
Expand All @@ -41,6 +41,8 @@ def _should_use_importlib_metadata() -> bool:
makes pip use ``pkg_resources`` (unless the user set the aforementioned
environment variable to *True*).
"""
if sys.version_info >= (3, 12):
return True
with contextlib.suppress(KeyError, ValueError):
return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"]))
if sys.version_info < (3, 11):
Expand All @@ -61,11 +63,6 @@ def select_backend() -> Backend:
from . import importlib

return cast(Backend, importlib)

if sys.version_info >= (3, 12):
message = "Cannot use pkg_resources as metadata backend on Python 3.12+"
raise PipError(message)

from . import pkg_resources

return cast(Backend, pkg_resources)
Expand Down

0 comments on commit 7cfc915

Please sign in to comment.