Skip to content

Commit c446a06

Browse files
authored
Merge pull request #3843 from pypa/debt/deprecate-pkg_resources
Officially deprecate pkg_resources
2 parents f04a29a + eb46a88 commit c446a06

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

changelog.d/3843.change.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Although pkg_resources has been discouraged for use, some projects still consider pkg_resources viable for usage. This change makes it clear that pkg_resources should not be used, emitting a DeprecationWarning when imported.

docs/pkg_resources.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ subpackages, and APIs for managing Python's current "working set" of active
1111
packages.
1212

1313
.. attention::
14-
Use of ``pkg_resources`` is discouraged in favor of
14+
Use of ``pkg_resources`` is deprecated in favor of
1515
`importlib.resources <https://docs.python.org/3/library/importlib.html#module-importlib.resources>`_,
1616
`importlib.metadata <https://docs.python.org/3/library/importlib.metadata.html>`_,
1717
and their backports (:pypi:`importlib_resources`,
1818
:pypi:`importlib_metadata`).
19-
Please consider using those libraries instead of pkg_resources.
19+
Users should refrain from new usage of ``pkg_resources`` and
20+
should work to port to importlib-based solutions.
2021

2122

2223
--------

pkg_resources/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
.egg files, and unpacked .egg files. It can also work in a limited way with
1313
.zip files and with custom PEP 302 loaders that support the ``get_data()``
1414
method.
15+
16+
This module is deprecated. Users are directed to
17+
`importlib.resources <https://docs.python.org/3/library/importlib.resources.html>`_
18+
and
19+
`importlib.metadata <https://docs.python.org/3/library/importlib.metadata.html>`_
20+
instead.
1521
"""
1622

1723
import sys
@@ -112,6 +118,9 @@
112118
_namespace_packages = None
113119

114120

121+
warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
122+
123+
115124
class PEP440Warning(RuntimeWarning):
116125
"""
117126
Used when there is an issue with a version or specifier not complying with
@@ -914,9 +923,7 @@ def find_plugins(self, plugin_env, full_env=None, installer=None, fallback=True)
914923
list(map(shadow_set.add, self))
915924

916925
for project_name in plugin_projects:
917-
918926
for dist in plugin_env[project_name]:
919-
920927
req = [dist.as_requirement()]
921928

922929
try:
@@ -1822,7 +1829,6 @@ def _get_date_and_size(zip_stat):
18221829

18231830
# FIXME: 'ZipProvider._extract_resource' is too complex (12)
18241831
def _extract_resource(self, manager, zip_path): # noqa: C901
1825-
18261832
if zip_path in self._index():
18271833
for name in self._index()[zip_path]:
18281834
last = self._extract_resource(manager, os.path.join(zip_path, name))
@@ -1836,7 +1842,6 @@ def _extract_resource(self, manager, zip_path): # noqa: C901
18361842
'"os.rename" and "os.unlink" are not supported ' 'on this platform'
18371843
)
18381844
try:
1839-
18401845
real_path = manager.get_cache_path(self.egg_name, self._parts(zip_path))
18411846

18421847
if self._is_current(real_path, zip_path):

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ filterwarnings=
8686

8787
# Avoid errors when testing pkg_resources.declare_namespace
8888
ignore:.*pkg_resources\.declare_namespace.*:DeprecationWarning
89+
90+
# suppress known deprecation
91+
ignore:pkg_resources is deprecated:DeprecationWarning

setuptools/tests/contexts.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import site
77
import io
88

9-
import pkg_resources
109
from filelock import FileLock
1110

1211

@@ -28,11 +27,7 @@ def environment(**replacements):
2827
In a context, patch the environment with replacements. Pass None values
2928
to clear the values.
3029
"""
31-
saved = dict(
32-
(key, os.environ[key])
33-
for key in replacements
34-
if key in os.environ
35-
)
30+
saved = dict((key, os.environ[key]) for key in replacements if key in os.environ)
3631

3732
# remove values that are null
3833
remove = (key for (key, value) in replacements.items() if value is None)
@@ -81,6 +76,8 @@ def save_user_site_setting():
8176

8277
@contextlib.contextmanager
8378
def save_pkg_resources_state():
79+
import pkg_resources
80+
8481
pr_state = pkg_resources.__getstate__()
8582
# also save sys.path
8683
sys_path = sys.path[:]

0 commit comments

Comments
 (0)