mirror of https://github.com/pypa/pip
Enable importlib.metadata backend on Python 3.11
This commit is contained in:
parent
e195f7bd3b
commit
bd9bcef8b3
|
@ -219,11 +219,12 @@ jobs:
|
||||||
env:
|
env:
|
||||||
TEMP: "R:\\Temp"
|
TEMP: "R:\\Temp"
|
||||||
|
|
||||||
|
# TODO: Remove this when we add Python 3.11 to CI.
|
||||||
tests-importlib-metadata:
|
tests-importlib-metadata:
|
||||||
name: tests for importlib.metadata backend
|
name: tests for importlib.metadata backend
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
_PIP_METADATA_BACKEND_IMPORTLIB: egg-compat
|
_PIP_USE_IMPORTLIB_METADATA: 'true'
|
||||||
|
|
||||||
needs: [pre-commit, packaging, determine-changes]
|
needs: [pre-commit, packaging, determine-changes]
|
||||||
if: >-
|
if: >-
|
||||||
|
@ -241,7 +242,6 @@ jobs:
|
||||||
|
|
||||||
- run: pip install nox 'virtualenv<20'
|
- run: pip install nox 'virtualenv<20'
|
||||||
|
|
||||||
# Main check
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: >-
|
run: >-
|
||||||
nox -s test-3.10 --
|
nox -s test-3.10 --
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Enable the ``importlib.metadata`` metadata implementation by default on
|
||||||
|
Python 3.11 (or later). The environment variable ``_PIP_USE_IMPORTLIB_METADATA``
|
||||||
|
can still be used to enable the implementation on 3.10 and earlier, or disable
|
||||||
|
it on 3.11 (by setting it to ``0`` or ``false``).
|
|
@ -1,7 +1,11 @@
|
||||||
|
import contextlib
|
||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
from typing import TYPE_CHECKING, List, Optional, Type, cast
|
from typing import TYPE_CHECKING, List, Optional, Type, cast
|
||||||
|
|
||||||
|
from pip._internal.utils.misc import strtobool
|
||||||
|
|
||||||
from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
|
from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -22,6 +26,29 @@ __all__ = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
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 overriden by a couple of ways:
|
||||||
|
|
||||||
|
* If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
|
||||||
|
dictates whether ``importlib.metadata`` is used, regardless of Python
|
||||||
|
version.
|
||||||
|
* On Python 3.11+, Python distributors can patch ``importlib.metadata``
|
||||||
|
to add a global constant ``_PIP_USE_IMPORTLIB_METADATA = False``. This
|
||||||
|
makes pip use ``pkg_resources`` (unless the user set the aforementioned
|
||||||
|
environment variable to *True*).
|
||||||
|
"""
|
||||||
|
with contextlib.suppress(KeyError, ValueError):
|
||||||
|
return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"]))
|
||||||
|
if sys.version_info < (3, 11):
|
||||||
|
return False
|
||||||
|
import importlib.metadata
|
||||||
|
|
||||||
|
return bool(getattr(importlib.metadata, "_PIP_USE_IMPORTLIB_METADATA", True))
|
||||||
|
|
||||||
|
|
||||||
class Backend(Protocol):
|
class Backend(Protocol):
|
||||||
Distribution: Type[BaseDistribution]
|
Distribution: Type[BaseDistribution]
|
||||||
Environment: Type[BaseEnvironment]
|
Environment: Type[BaseEnvironment]
|
||||||
|
@ -29,7 +56,7 @@ class Backend(Protocol):
|
||||||
|
|
||||||
@functools.lru_cache(maxsize=None)
|
@functools.lru_cache(maxsize=None)
|
||||||
def select_backend() -> Backend:
|
def select_backend() -> Backend:
|
||||||
if os.environ.get("_PIP_METADATA_BACKEND_IMPORTLIB"):
|
if _should_use_importlib_metadata():
|
||||||
from . import importlib
|
from . import importlib
|
||||||
|
|
||||||
return cast(Backend, importlib)
|
return cast(Backend, importlib)
|
||||||
|
|
Loading…
Reference in New Issue