Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ modules, and one that knows how to import modules from an :term:`import path`
import machinery will try it only if the finder does not implement
``find_spec()``.

.. versionchanged:: 3.10
Use of :meth:`~importlib.abc.MetaPathFinder.find_module` by the import system
now raises :exc:`ImportWarning`.


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

.. versionchanged:: 3.10
Use of ``load_module()`` will raise :exc:`ImportWarning`.

Submodules
----------

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

.. versionchanged:: 3.10
Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` by the import
system will raise :exc:`ImportWarning`.


Replacing the standard import system
====================================
Expand Down
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,15 @@ Deprecated
:meth:`~importlib.abc.Loader.exec_module` is preferred.
(Contributed by Brett Cannon in :issue:`26131`.)

* The use of :meth:`importlib.abc.MetaPathFinder.find_module` and
:meth:`importlib.abc.PathEntryFinder.find_module` by the import system now
trigger an :exc:`ImportWarning` as
:meth:`importlib.abc.MetaPathFinder.find_spec` and
:meth:`importlib.abc.PathEntryFinder.find_spec`
are preferred, respectively. You can use
:func:`importlib.util.spec_from_loader` to help in porting.
(Contributed by Brett Cannon in :issue:`42134`.)

* The import system now uses the ``__spec__`` attribute on modules before
falling back on :meth:`~importlib.abc.Loader.module_repr` for a module's
``__repr__()`` method. Removal of the use of ``module_repr()`` is scheduled
Expand Down
5 changes: 3 additions & 2 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,9 @@ def _resolve_name(name, package, level):


def _find_spec_legacy(finder, name, path):
# This would be a good place for a DeprecationWarning if
# we ended up going that route.
msg = (f"{_object_name(finder)}.find_spec() not found; "
"falling back to find_module()")
_warnings.warn(msg, ImportWarning)
loader = finder.find_module(name, path)
if loader is None:
return None
Expand Down
3 changes: 3 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,9 @@ def _legacy_get_spec(cls, fullname, finder):
if hasattr(finder, 'find_loader'):
loader, portions = finder.find_loader(fullname)
else:
msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
"falling back to find_module()")
_warnings.warn(msg, ImportWarning)
loader = finder.find_module(fullname)
portions = []
if loader is not None:
Expand Down
16 changes: 12 additions & 4 deletions Lib/test/test_importlib/import_/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,16 @@ def find_module(self, fullname):
failing_finder.to_return = None
path = 'testing path'
with util.import_state(path_importer_cache={path: failing_finder}):
self.assertIsNone(
with warnings.catch_warnings():
warnings.simplefilter("ignore", ImportWarning)
self.assertIsNone(
self.machinery.PathFinder.find_spec('whatever', [path]))
success_finder = TestFinder()
success_finder.to_return = __loader__
with util.import_state(path_importer_cache={path: success_finder}):
spec = self.machinery.PathFinder.find_spec('whatever', [path])
with warnings.catch_warnings():
warnings.simplefilter("ignore", ImportWarning)
spec = self.machinery.PathFinder.find_spec('whatever', [path])
self.assertEqual(spec.loader, __loader__)

def test_finder_with_find_loader(self):
Expand Down Expand Up @@ -248,7 +252,9 @@ def find_module(fullname):

with util.import_state(path=[Finder.path_location]+sys.path[:],
path_hooks=[Finder]):
self.machinery.PathFinder.find_spec('importlib')
with warnings.catch_warnings():
warnings.simplefilter("ignore", ImportWarning)
self.machinery.PathFinder.find_spec('importlib')

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

with util.import_state(path=[Finder.path_location]+sys.path[:],
path_hooks=[Finder]):
self.machinery.PathFinder.find_module('importlib')
with warnings.catch_warnings():
warnings.simplefilter("ignore", ImportWarning)
self.machinery.PathFinder.find_module('importlib')


(Frozen_PEFTests,
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_importlib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_success(self):
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
warnings.simplefilter('ignore', ImportWarning)
self.assertEqual((name, None), self.init.find_loader(name))

def test_success_path(self):
Expand All @@ -161,6 +162,7 @@ def test_success_path(self):
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
warnings.simplefilter('ignore', ImportWarning)
self.assertEqual((name, path),
self.init.find_loader(name, path))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calls to find_module() by the import system now raise ImportWarning.
Loading