Skip to content

Commit 1899087

Browse files
authored
bpo-42136: Deprecate module_repr() as found in importlib (GH-25022)
1 parent 21a2cab commit 1899087

File tree

11 files changed

+1543
-1476
lines changed

11 files changed

+1543
-1476
lines changed

Doc/reference/import.rst

+7
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,13 @@ Here are the exact rules used:
675675
:meth:`~importlib.abc.Loader.module_repr` method, if defined, before
676676
trying either approach described above. However, the method is deprecated.
677677

678+
.. versionchanged:: 3.10
679+
680+
Calling :meth:`~importlib.abc.Loader.module_repr` now occurs after trying to
681+
use a module's ``__spec__`` attribute but before falling back on
682+
``__file__``. Use of :meth:`~importlib.abc.Loader.module_repr` is slated to
683+
stop in Python 3.12.
684+
678685
.. _pyc-invalidation:
679686

680687
Cached bytecode invalidation

Doc/whatsnew/3.10.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,12 @@ Deprecated
10101010
for Python 3.12.
10111011
(Contributed by Brett Cannon in :issue:`42137`.)
10121012
1013+
* :meth:`importlib.abc.Loader.module_repr`,
1014+
:meth:`importlib.machinery.FrozenLoader.module_repr`, and
1015+
:meth:`importlib.machinery.BuiltinLoader.module_repr` are deprecated and
1016+
slated for removal in Python 3.12.
1017+
(Contributed by Brett Cannon in :issue:`42136`.)
1018+
10131019
* ``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
10141020
3.3, when it was made an alias to :class:`str`. It is now deprecated,
10151021
scheduled for removal in Python 3.12.

Lib/importlib/_abc.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Subset of importlib.abc used to reduce importlib.util imports."""
22
from . import _bootstrap
33
import abc
4+
import warnings
45

56

67
class Loader(metaclass=abc.ABCMeta):
@@ -47,5 +48,7 @@ def module_repr(self, module):
4748
This method is deprecated.
4849
4950
"""
51+
warnings.warn("importlib.abc.Loader.module_repr() is deprecated and "
52+
"slated for removal in Python 3.12", DeprecationWarning)
5053
# The exception will cause ModuleType.__repr__ to ignore this method.
5154
raise NotImplementedError

Lib/importlib/_bootstrap.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,6 @@ def _module_repr(module):
302302
except Exception:
303303
pass
304304
# Fall through to a catch-all which always succeeds.
305-
# We could use module.__class__.__name__ instead of 'module' in the
306-
# various repr permutations.
307305
try:
308306
name = module.__name__
309307
except AttributeError:
@@ -741,6 +739,8 @@ def module_repr(module):
741739
The method is deprecated. The import machinery does the job itself.
742740
743741
"""
742+
_warnings.warn("BuiltinImporter.module_repr() is deprecated and "
743+
"slated for removal in Python 3.12", DeprecationWarning)
744744
return f'<module {module.__name__!r} ({BuiltinImporter._ORIGIN})>'
745745

746746
@classmethod
@@ -816,6 +816,8 @@ def module_repr(m):
816816
The method is deprecated. The import machinery does the job itself.
817817
818818
"""
819+
_warnings.warn("FrozenImporter.module_repr() is deprecated and "
820+
"slated for removal in Python 3.12", DeprecationWarning)
819821
return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN)
820822

821823
@classmethod

Lib/importlib/_bootstrap_external.py

+2
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,8 @@ def module_repr(module):
12301230
The method is deprecated. The import machinery does the job itself.
12311231
12321232
"""
1233+
_warnings.warn("_NamespaceLoader.module_repr() is deprecated and "
1234+
"slated for removal in Python 3.12", DeprecationWarning)
12331235
return '<module {!r} (namespace)>'.format(module.__name__)
12341236

12351237
def is_package(self, fullname):

Lib/test/test_importlib/test_abc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ def test_load_module(self):
220220

221221
def test_module_repr(self):
222222
mod = types.ModuleType('blah')
223-
with self.assertRaises(NotImplementedError):
224-
self.ins.module_repr(mod)
223+
with warnings.catch_warnings():
224+
warnings.simplefilter("ignore")
225+
with self.assertRaises(NotImplementedError):
226+
self.ins.module_repr(mod)
225227
original_repr = repr(mod)
226228
mod.__loader__ = self.ins
227229
# Should still return a proper repr.

Lib/test/test_importlib/test_namespace_pkgs.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import unittest
6+
import warnings
67

78
from test.test_importlib import util
89

@@ -82,8 +83,10 @@ def test_cant_import_other(self):
8283

8384
def test_module_repr(self):
8485
import foo.one
85-
self.assertEqual(foo.__spec__.loader.module_repr(foo),
86-
"<module 'foo' (namespace)>")
86+
with warnings.catch_warnings():
87+
warnings.simplefilter("ignore")
88+
self.assertEqual(foo.__spec__.loader.module_repr(foo),
89+
"<module 'foo' (namespace)>")
8790

8891

8992
class DynamicPathNamespacePackage(NamespacePackageTest):

Lib/test/test_importlib/test_windows.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import unittest
88
from test import support
99
from test.support import import_helper
10-
from distutils.util import get_platform
1110
from contextlib import contextmanager
1211
from .util import temp_module
1312

@@ -18,6 +17,25 @@
1817
EnumKey, CloseKey, DeleteKey, OpenKey
1918
)
2019

20+
def get_platform():
21+
# Port of distutils.util.get_platform().
22+
TARGET_TO_PLAT = {
23+
'x86' : 'win32',
24+
'x64' : 'win-amd64',
25+
'arm' : 'win-arm32',
26+
}
27+
if ('VSCMD_ARG_TGT_ARCH' in os.environ and
28+
os.environ['VSCMD_ARG_TGT_ARCH'] in TARGET_TO_PLAT):
29+
return TARGET_TO_PLAT[os.environ['VSCMD_ARG_TGT_ARCH']]
30+
elif 'amd64' in sys.version.lower():
31+
return 'win-amd64'
32+
elif '(arm)' in sys.version.lower():
33+
return 'win-arm32'
34+
elif '(arm64)' in sys.version.lower():
35+
return 'win-arm64'
36+
else:
37+
return sys.platform
38+
2139
def delete_registry_tree(root, subkey):
2240
try:
2341
hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
@@ -101,7 +119,7 @@ def test_tagged_suffix(self):
101119

102120
self.assertIn(expected_tag, suffixes)
103121

104-
# Ensure the tags are in the correct order
122+
# Ensure the tags are in the correct order.
105123
tagged_i = suffixes.index(expected_tag)
106124
self.assertLess(tagged_i, untagged_i)
107125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate all module_repr() methods found in importlib as their use is being
2+
phased out by Python 3.12.

0 commit comments

Comments
 (0)