Skip to content

Commit c86f9f1

Browse files
authored
Merge pull request #11044 from uranusjr/importlib-metadata-backend-in-3.11
2 parents cb24fb4 + bd9bcef commit c86f9f1

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,12 @@ jobs:
219219
env:
220220
TEMP: "R:\\Temp"
221221

222+
# TODO: Remove this when we add Python 3.11 to CI.
222223
tests-importlib-metadata:
223224
name: tests for importlib.metadata backend
224225
runs-on: ubuntu-latest
225226
env:
226-
_PIP_METADATA_BACKEND_IMPORTLIB: egg-compat
227+
_PIP_USE_IMPORTLIB_METADATA: 'true'
227228

228229
needs: [pre-commit, packaging, determine-changes]
229230
if: >-
@@ -241,7 +242,6 @@ jobs:
241242

242243
- run: pip install nox 'virtualenv<20'
243244

244-
# Main check
245245
- name: Run unit tests
246246
run: >-
247247
nox -s test-3.10 --

news/11044.process.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Enable the ``importlib.metadata`` metadata implementation by default on
2+
Python 3.11 (or later). The environment variable ``_PIP_USE_IMPORTLIB_METADATA``
3+
can still be used to enable the implementation on 3.10 and earlier, or disable
4+
it on 3.11 (by setting it to ``0`` or ``false``).

src/pip/_internal/metadata/__init__.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import contextlib
12
import functools
23
import os
4+
import sys
35
from typing import TYPE_CHECKING, List, Optional, Type, cast
46

7+
from pip._internal.utils.misc import strtobool
8+
59
from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
610

711
if TYPE_CHECKING:
@@ -22,14 +26,37 @@
2226
]
2327

2428

29+
def _should_use_importlib_metadata() -> bool:
30+
"""Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend.
31+
32+
By default, pip uses ``importlib.metadata`` on Python 3.11+, and
33+
``pkg_resourcess`` otherwise. This can be overriden by a couple of ways:
34+
35+
* If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
36+
dictates whether ``importlib.metadata`` is used, regardless of Python
37+
version.
38+
* On Python 3.11+, Python distributors can patch ``importlib.metadata``
39+
to add a global constant ``_PIP_USE_IMPORTLIB_METADATA = False``. This
40+
makes pip use ``pkg_resources`` (unless the user set the aforementioned
41+
environment variable to *True*).
42+
"""
43+
with contextlib.suppress(KeyError, ValueError):
44+
return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"]))
45+
if sys.version_info < (3, 11):
46+
return False
47+
import importlib.metadata
48+
49+
return bool(getattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA", True))
50+
51+
2552
class Backend(Protocol):
2653
Distribution: Type[BaseDistribution]
2754
Environment: Type[BaseEnvironment]
2855

2956

3057
@functools.lru_cache(maxsize=None)
3158
def select_backend() -> Backend:
32-
if os.environ.get("_PIP_METADATA_BACKEND_IMPORTLIB"):
59+
if _should_use_importlib_metadata():
3360
from . import importlib
3461

3562
return cast(Backend, importlib)

0 commit comments

Comments
 (0)