Skip to content

Commit a7ff6df

Browse files
authored
bpo-42134: Raise ImportWarning when calling find_module() in the import system (GH-25044)
1 parent cf35e05 commit a7ff6df

File tree

9 files changed

+1012
-962
lines changed

9 files changed

+1012
-962
lines changed

Doc/reference/import.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ modules, and one that knows how to import modules from an :term:`import path`
329329
import machinery will try it only if the finder does not implement
330330
``find_spec()``.
331331

332+
.. versionchanged:: 3.10
333+
Use of :meth:`~importlib.abc.MetaPathFinder.find_module` by the import system
334+
now raises :exc:`ImportWarning`.
335+
332336

333337
Loading
334338
=======
@@ -470,6 +474,9 @@ import machinery will create the new module itself.
470474
An :exc:`ImportError` is raised when ``exec_module()`` is defined but
471475
``create_module()`` is not.
472476

477+
.. versionchanged:: 3.10
478+
Use of ``load_module()`` will raise :exc:`ImportWarning`.
479+
473480
Submodules
474481
----------
475482

@@ -896,6 +903,10 @@ a list containing the portion.
896903
exist on a path entry finder, the import system will always call
897904
``find_loader()`` in preference to ``find_module()``.
898905

906+
.. versionchanged:: 3.10
907+
Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` by the import
908+
system will raise :exc:`ImportWarning`.
909+
899910

900911
Replacing the standard import system
901912
====================================

Doc/whatsnew/3.10.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,15 @@ Deprecated
10281028
:meth:`~importlib.abc.Loader.exec_module` is preferred.
10291029
(Contributed by Brett Cannon in :issue:`26131`.)
10301030
1031+
* The use of :meth:`importlib.abc.MetaPathFinder.find_module` and
1032+
:meth:`importlib.abc.PathEntryFinder.find_module` by the import system now
1033+
trigger an :exc:`ImportWarning` as
1034+
:meth:`importlib.abc.MetaPathFinder.find_spec` and
1035+
:meth:`importlib.abc.PathEntryFinder.find_spec`
1036+
are preferred, respectively. You can use
1037+
:func:`importlib.util.spec_from_loader` to help in porting.
1038+
(Contributed by Brett Cannon in :issue:`42134`.)
1039+
10311040
* The import system now uses the ``__spec__`` attribute on modules before
10321041
falling back on :meth:`~importlib.abc.Loader.module_repr` for a module's
10331042
``__repr__()`` method. Removal of the use of ``module_repr()`` is scheduled

Lib/importlib/_bootstrap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,9 @@ def _resolve_name(name, package, level):
903903

904904

905905
def _find_spec_legacy(finder, name, path):
906-
# This would be a good place for a DeprecationWarning if
907-
# we ended up going that route.
906+
msg = (f"{_object_name(finder)}.find_spec() not found; "
907+
"falling back to find_module()")
908+
_warnings.warn(msg, ImportWarning)
908909
loader = finder.find_module(name, path)
909910
if loader is None:
910911
return None

Lib/importlib/_bootstrap_external.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,9 @@ def _legacy_get_spec(cls, fullname, finder):
13241324
if hasattr(finder, 'find_loader'):
13251325
loader, portions = finder.find_loader(fullname)
13261326
else:
1327+
msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
1328+
"falling back to find_module()")
1329+
_warnings.warn(msg, ImportWarning)
13271330
loader = finder.find_module(fullname)
13281331
portions = []
13291332
if loader is not None:

Lib/test/test_importlib/import_/test_path.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ def find_module(self, fullname):
123123
failing_finder.to_return = None
124124
path = 'testing path'
125125
with util.import_state(path_importer_cache={path: failing_finder}):
126-
self.assertIsNone(
126+
with warnings.catch_warnings():
127+
warnings.simplefilter("ignore", ImportWarning)
128+
self.assertIsNone(
127129
self.machinery.PathFinder.find_spec('whatever', [path]))
128130
success_finder = TestFinder()
129131
success_finder.to_return = __loader__
130132
with util.import_state(path_importer_cache={path: success_finder}):
131-
spec = self.machinery.PathFinder.find_spec('whatever', [path])
133+
with warnings.catch_warnings():
134+
warnings.simplefilter("ignore", ImportWarning)
135+
spec = self.machinery.PathFinder.find_spec('whatever', [path])
132136
self.assertEqual(spec.loader, __loader__)
133137

134138
def test_finder_with_find_loader(self):
@@ -248,7 +252,9 @@ def find_module(fullname):
248252

249253
with util.import_state(path=[Finder.path_location]+sys.path[:],
250254
path_hooks=[Finder]):
251-
self.machinery.PathFinder.find_spec('importlib')
255+
with warnings.catch_warnings():
256+
warnings.simplefilter("ignore", ImportWarning)
257+
self.machinery.PathFinder.find_spec('importlib')
252258

253259
def test_finder_with_failing_find_module(self):
254260
# PathEntryFinder with find_module() defined should work.
@@ -266,7 +272,9 @@ def find_module(fullname):
266272

267273
with util.import_state(path=[Finder.path_location]+sys.path[:],
268274
path_hooks=[Finder]):
269-
self.machinery.PathFinder.find_module('importlib')
275+
with warnings.catch_warnings():
276+
warnings.simplefilter("ignore", ImportWarning)
277+
self.machinery.PathFinder.find_module('importlib')
270278

271279

272280
(Frozen_PEFTests,

Lib/test/test_importlib/test_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def test_success(self):
151151
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
152152
with warnings.catch_warnings():
153153
warnings.simplefilter('ignore', DeprecationWarning)
154+
warnings.simplefilter('ignore', ImportWarning)
154155
self.assertEqual((name, None), self.init.find_loader(name))
155156

156157
def test_success_path(self):
@@ -161,6 +162,7 @@ def test_success_path(self):
161162
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
162163
with warnings.catch_warnings():
163164
warnings.simplefilter('ignore', DeprecationWarning)
165+
warnings.simplefilter('ignore', ImportWarning)
164166
self.assertEqual((name, path),
165167
self.init.find_loader(name, path))
166168

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Calls to find_module() by the import system now raise ImportWarning.

0 commit comments

Comments
 (0)