Skip to content

Commit 3269978

Browse files
warsawarhadthedevAA-Turner
authored
gh-98040: Remove find_loader, find_module and other deprecated APIs (#98059)
* Remove deprecated classes from pkgutil * Remove some other PEP 302 obsolescence * Use find_spec instead of load_module * Remove more tests of PEP 302 obsolete APIs * Remove another bunch of tests using obsolete load_modules() * Remove deleted names from __all__ * Remove obsolete footnote * imp is removed * Remove `imp` from generated stdlib names * What's new and blurb * Update zipimport documentation for the removed methods * Fix some Windows tests * Remove any test (or part of a test) that references `find_module()`. * Use assertIsNone() / assertIsNotNone() consistently. * Update Doc/reference/import.rst * We don't need pkgutil._get_spec() any more either * test.test_importlib.fixtures.NullFinder * ...BadLoaderFinder.find_module * ...test_api.InvalidatingNullFinder.find_module * ...test.test_zipimport test of z.find_module * Suppress cross-references to find_loader and find_module * Suppress cross-references to Finder * Suppress cross-references to pkgutil.ImpImporter and pkgutil.ImpLoader --------- Co-authored-by: Oleg Iarygin <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent bcea36f commit 3269978

39 files changed

+137
-1047
lines changed

Doc/library/importlib.rst

Lines changed: 2 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,6 @@ Functions
127127
.. versionchanged:: 3.3
128128
Parent packages are automatically imported.
129129

130-
.. function:: find_loader(name, path=None)
131-
132-
Find the loader for a module, optionally within the specified *path*. If the
133-
module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is
134-
returned (unless the loader would be ``None`` or is not set, in which case
135-
:exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path`
136-
is done. ``None`` is returned if no loader is found.
137-
138-
A dotted name does not have its parents implicitly imported as that requires
139-
loading them and that may not be desired. To properly import a submodule you
140-
will need to import all parent packages of the submodule and use the correct
141-
argument to *path*.
142-
143-
.. versionadded:: 3.3
144-
145-
.. versionchanged:: 3.4
146-
If ``__loader__`` is not set, raise :exc:`ValueError`, just like when the
147-
attribute is set to ``None``.
148-
149-
.. deprecated:: 3.4
150-
Use :func:`importlib.util.find_spec` instead.
151-
152130
.. function:: invalidate_caches()
153131

154132
Invalidate the internal caches of finders stored at
@@ -247,7 +225,6 @@ are also provided to help in implementing the core ABCs.
247225
ABC hierarchy::
248226

249227
object
250-
+-- Finder (deprecated)
251228
+-- MetaPathFinder
252229
+-- PathEntryFinder
253230
+-- Loader
@@ -258,36 +235,14 @@ ABC hierarchy::
258235
+-- SourceLoader
259236

260237

261-
.. class:: Finder
262-
263-
An abstract base class representing a :term:`finder`.
264-
265-
.. deprecated:: 3.3
266-
Use :class:`MetaPathFinder` or :class:`PathEntryFinder` instead.
267-
268-
.. abstractmethod:: find_module(fullname, path=None)
269-
270-
An abstract method for finding a :term:`loader` for the specified
271-
module. Originally specified in :pep:`302`, this method was meant
272-
for use in :data:`sys.meta_path` and in the path-based import subsystem.
273-
274-
.. versionchanged:: 3.4
275-
Returns ``None`` when called instead of raising
276-
:exc:`NotImplementedError`.
277-
278-
.. deprecated:: 3.10
279-
Implement :meth:`MetaPathFinder.find_spec` or
280-
:meth:`PathEntryFinder.find_spec` instead.
281-
282-
283238
.. class:: MetaPathFinder
284239

285240
An abstract base class representing a :term:`meta path finder`.
286241

287242
.. versionadded:: 3.3
288243

289244
.. versionchanged:: 3.10
290-
No longer a subclass of :class:`Finder`.
245+
No longer a subclass of :class:`!Finder`.
291246

292247
.. method:: find_spec(fullname, path, target=None)
293248

@@ -303,25 +258,6 @@ ABC hierarchy::
303258

304259
.. versionadded:: 3.4
305260

306-
.. method:: find_module(fullname, path)
307-
308-
A legacy method for finding a :term:`loader` for the specified
309-
module. If this is a top-level import, *path* will be ``None``.
310-
Otherwise, this is a search for a subpackage or module and *path*
311-
will be the value of :attr:`__path__` from the parent
312-
package. If a loader cannot be found, ``None`` is returned.
313-
314-
If :meth:`find_spec` is defined, backwards-compatible functionality is
315-
provided.
316-
317-
.. versionchanged:: 3.4
318-
Returns ``None`` when called instead of raising
319-
:exc:`NotImplementedError`. Can use :meth:`find_spec` to provide
320-
functionality.
321-
322-
.. deprecated:: 3.4
323-
Use :meth:`find_spec` instead.
324-
325261
.. method:: invalidate_caches()
326262

327263
An optional method which, when called, should invalidate any internal
@@ -342,7 +278,7 @@ ABC hierarchy::
342278
.. versionadded:: 3.3
343279

344280
.. versionchanged:: 3.10
345-
No longer a subclass of :class:`Finder`.
281+
No longer a subclass of :class:`!Finder`.
346282

347283
.. method:: find_spec(fullname, target=None)
348284

@@ -356,36 +292,6 @@ ABC hierarchy::
356292

357293
.. versionadded:: 3.4
358294

359-
.. method:: find_loader(fullname)
360-
361-
A legacy method for finding a :term:`loader` for the specified
362-
module. Returns a 2-tuple of ``(loader, portion)`` where ``portion``
363-
is a sequence of file system locations contributing to part of a namespace
364-
package. The loader may be ``None`` while specifying ``portion`` to
365-
signify the contribution of the file system locations to a namespace
366-
package. An empty list can be used for ``portion`` to signify the loader
367-
is not part of a namespace package. If ``loader`` is ``None`` and
368-
``portion`` is the empty list then no loader or location for a namespace
369-
package were found (i.e. failure to find anything for the module).
370-
371-
If :meth:`find_spec` is defined then backwards-compatible functionality is
372-
provided.
373-
374-
.. versionchanged:: 3.4
375-
Returns ``(None, [])`` instead of raising :exc:`NotImplementedError`.
376-
Uses :meth:`find_spec` when available to provide functionality.
377-
378-
.. deprecated:: 3.4
379-
Use :meth:`find_spec` instead.
380-
381-
.. method:: find_module(fullname)
382-
383-
A concrete implementation of :meth:`Finder.find_module` which is
384-
equivalent to ``self.find_loader(fullname)[0]``.
385-
386-
.. deprecated:: 3.4
387-
Use :meth:`find_spec` instead.
388-
389295
.. method:: invalidate_caches()
390296

391297
An optional method which, when called, should invalidate any internal
@@ -881,13 +787,6 @@ find and load modules.
881787
is no longer valid then ``None`` is returned but no value is cached
882788
in :data:`sys.path_importer_cache`.
883789

884-
.. classmethod:: find_module(fullname, path=None)
885-
886-
A legacy wrapper around :meth:`find_spec`.
887-
888-
.. deprecated:: 3.4
889-
Use :meth:`find_spec` instead.
890-
891790
.. classmethod:: invalidate_caches()
892791

893792
Calls :meth:`importlib.abc.PathEntryFinder.invalidate_caches` on all
@@ -938,13 +837,6 @@ find and load modules.
938837

939838
.. versionadded:: 3.4
940839

941-
.. method:: find_loader(fullname)
942-
943-
Attempt to find the loader to handle *fullname* within :attr:`path`.
944-
945-
.. deprecated:: 3.10
946-
Use :meth:`find_spec` instead.
947-
948840
.. method:: invalidate_caches()
949841

950842
Clear out the internal cache.

Doc/library/pkgutil.rst

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,6 @@ support.
4848
this function to raise an exception (in line with :func:`os.path.isdir`
4949
behavior).
5050

51-
52-
.. class:: ImpImporter(dirname=None)
53-
54-
:pep:`302` Finder that wraps Python's "classic" import algorithm.
55-
56-
If *dirname* is a string, a :pep:`302` finder is created that searches that
57-
directory. If *dirname* is ``None``, a :pep:`302` finder is created that
58-
searches the current :data:`sys.path`, plus any modules that are frozen or
59-
built-in.
60-
61-
Note that :class:`ImpImporter` does not currently support being used by
62-
placement on :data:`sys.meta_path`.
63-
64-
.. deprecated:: 3.3
65-
This emulation is no longer needed, as the standard import mechanism
66-
is now fully :pep:`302` compliant and available in :mod:`importlib`.
67-
68-
69-
.. class:: ImpLoader(fullname, file, filename, etc)
70-
71-
:term:`Loader <loader>` that wraps Python's "classic" import algorithm.
72-
73-
.. deprecated:: 3.3
74-
This emulation is no longer needed, as the standard import mechanism
75-
is now fully :pep:`302` compliant and available in :mod:`importlib`.
76-
77-
7851
.. function:: find_loader(fullname)
7952

8053
Retrieve a module :term:`loader` for the given *fullname*.

Doc/library/sys.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ always available.
11771177

11781178
:term:`Module specs <module spec>` were introduced in Python 3.4, by
11791179
:pep:`451`. Earlier versions of Python looked for a method called
1180-
:meth:`~importlib.abc.MetaPathFinder.find_module`.
1180+
:meth:`!find_module`.
11811181
This is still called as a fallback if a :data:`meta_path` entry doesn't
11821182
have a :meth:`~importlib.abc.MetaPathFinder.find_spec` method.
11831183

Doc/library/zipimport.rst

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ zipimporter Objects
7474
:exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
7575
archive.
7676

77+
.. versionchanged:: 3.12
78+
79+
Methods ``find_loader()`` and ``find_module()``, deprecated in 3.10 are
80+
now removed. Use :meth:`find_spec` instead.
81+
7782
.. method:: create_module(spec)
7883

7984
Implementation of :meth:`importlib.abc.Loader.create_module` that returns
@@ -89,28 +94,6 @@ zipimporter Objects
8994
.. versionadded:: 3.10
9095

9196

92-
.. method:: find_loader(fullname, path=None)
93-
94-
An implementation of :meth:`importlib.abc.PathEntryFinder.find_loader`.
95-
96-
.. deprecated:: 3.10
97-
98-
Use :meth:`find_spec` instead.
99-
100-
101-
.. method:: find_module(fullname, path=None)
102-
103-
Search for a module specified by *fullname*. *fullname* must be the fully
104-
qualified (dotted) module name. It returns the zipimporter instance itself
105-
if the module was found, or :const:`None` if it wasn't. The optional
106-
*path* argument is ignored---it's there for compatibility with the
107-
importer protocol.
108-
109-
.. deprecated:: 3.10
110-
111-
Use :meth:`find_spec` instead.
112-
113-
11497
.. method:: find_spec(fullname, target=None)
11598

11699
An implementation of :meth:`importlib.abc.PathEntryFinder.find_spec`.

Doc/reference/import.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,18 @@ modules, and one that knows how to import modules from an :term:`import path`
324324

325325
.. versionchanged:: 3.4
326326
The :meth:`~importlib.abc.MetaPathFinder.find_spec` method of meta path
327-
finders replaced :meth:`~importlib.abc.MetaPathFinder.find_module`, which
327+
finders replaced :meth:`!find_module`, which
328328
is now deprecated. While it will continue to work without change, the
329329
import machinery will try it only if the finder does not implement
330330
``find_spec()``.
331331

332332
.. versionchanged:: 3.10
333-
Use of :meth:`~importlib.abc.MetaPathFinder.find_module` by the import system
333+
Use of :meth:`!find_module` by the import system
334334
now raises :exc:`ImportWarning`.
335335

336+
.. versionchanged:: 3.12
337+
``find_module()`` has been removed. Use :meth:`find_spec` instead.
338+
336339

337340
Loading
338341
=======
@@ -837,7 +840,7 @@ stores finder objects rather than being limited to :term:`importer` objects).
837840
In this way, the expensive search for a particular :term:`path entry`
838841
location's :term:`path entry finder` need only be done once. User code is
839842
free to remove cache entries from :data:`sys.path_importer_cache` forcing
840-
the path based finder to perform the path entry search again [#fnpic]_.
843+
the path based finder to perform the path entry search again.
841844

842845
If the path entry is not present in the cache, the path based finder iterates
843846
over every callable in :data:`sys.path_hooks`. Each of the :term:`path entry
@@ -887,21 +890,21 @@ module. ``find_spec()`` returns a fully populated spec for the module.
887890
This spec will always have "loader" set (with one exception).
888891

889892
To indicate to the import machinery that the spec represents a namespace
890-
:term:`portion`, the path entry finder sets "submodule_search_locations" to
893+
:term:`portion`, the path entry finder sets ``submodule_search_locations`` to
891894
a list containing the portion.
892895

893896
.. versionchanged:: 3.4
894897
:meth:`~importlib.abc.PathEntryFinder.find_spec` replaced
895-
:meth:`~importlib.abc.PathEntryFinder.find_loader` and
896-
:meth:`~importlib.abc.PathEntryFinder.find_module`, both of which
898+
:meth:`!find_loader` and
899+
:meth:`!find_module`, both of which
897900
are now deprecated, but will be used if ``find_spec()`` is not defined.
898901

899902
Older path entry finders may implement one of these two deprecated methods
900903
instead of ``find_spec()``. The methods are still respected for the
901904
sake of backward compatibility. However, if ``find_spec()`` is
902905
implemented on the path entry finder, the legacy methods are ignored.
903906

904-
:meth:`~importlib.abc.PathEntryFinder.find_loader` takes one argument, the
907+
:meth:`!find_loader` takes one argument, the
905908
fully qualified name of the module being imported. ``find_loader()``
906909
returns a 2-tuple where the first item is the loader and the second item
907910
is a namespace :term:`portion`.
@@ -920,10 +923,13 @@ a list containing the portion.
920923
``find_loader()`` in preference to ``find_module()``.
921924

922925
.. versionchanged:: 3.10
923-
Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` and
924-
:meth:`~importlib.abc.PathEntryFinder.find_loader` by the import
926+
Calls to :meth:`!find_module` and
927+
:meth:`!find_loader` by the import
925928
system will raise :exc:`ImportWarning`.
926929

930+
.. versionchanged:: 3.12
931+
``find_module()`` and ``find_loader()`` have been removed.
932+
927933

928934
Replacing the standard import system
929935
====================================
@@ -1045,8 +1051,8 @@ The original specification for :data:`sys.meta_path` was :pep:`302`, with
10451051
subsequent extension in :pep:`420`.
10461052

10471053
:pep:`420` introduced :term:`namespace packages <namespace package>` for
1048-
Python 3.3. :pep:`420` also introduced the :meth:`find_loader` protocol as an
1049-
alternative to :meth:`find_module`.
1054+
Python 3.3. :pep:`420` also introduced the :meth:`!find_loader` protocol as an
1055+
alternative to :meth:`!find_module`.
10501056

10511057
:pep:`366` describes the addition of the ``__package__`` attribute for
10521058
explicit relative imports in main modules.
@@ -1073,9 +1079,3 @@ methods to finders and loaders.
10731079
module may replace itself in :data:`sys.modules`. This is
10741080
implementation-specific behavior that is not guaranteed to work in other
10751081
Python implementations.
1076-
1077-
.. [#fnpic] In legacy code, it is possible to find instances of
1078-
:class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
1079-
is recommended that code be changed to use ``None`` instead. See
1080-
:ref:`portingpythoncode` for more details. Note that the ``imp`` module
1081-
was removed in Python 3.12.

Doc/whatsnew/2.3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ module:
728728

729729
Importer objects must have a single method, ``find_module(fullname,
730730
path=None)``. *fullname* will be a module or package name, e.g. ``string`` or
731-
``distutils.core``. :meth:`find_module` must return a loader object that has a
731+
``distutils.core``. :meth:`!find_module` must return a loader object that has a
732732
single method, ``load_module(fullname)``, that creates and returns the
733733
corresponding module object.
734734

0 commit comments

Comments
 (0)