44import sys
55from typing import List , Literal , Optional , Protocol , Type , cast
66
7+ from pip ._internal .utils .deprecation import deprecated
78from pip ._internal .utils .misc import strtobool
89
910from .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+
5783class 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