Skip to content

Commit 1d4601c

Browse files
Carreaubrettcannon
authored andcommitted
bpo-29576: add explicit deprecation for importlib.abc.find_loader() and find_module() (GH-32)
1 parent 72dccde commit 1d4601c

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

Doc/whatsnew/3.7.rst

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ Deprecated
140140
``0x03050400`` and ``0x03060000`` (not including) or ``0x03060100`` or
141141
higher. (Contributed by Serhiy Storchaka in :issue:`27867`.)
142142

143+
- Methods
144+
:meth:`MetaPathFinder.find_module() <importlib.abc.MetaPathFinder.find_module>`
145+
(replaced by
146+
:meth:`MetaPathFinder.find_spec() <importlib.abc.MetaPathFinder.find_spec>`
147+
) and
148+
:meth:`PathEntryFinder.find_loader() <importlib.abc.PathEntryFinder.find_loader>`
149+
(replaced by
150+
:meth:`PathEntryFinder.find_spec() <importlib.abc.PathEntryFinder.find_spec>`)
151+
both deprecated in Python 3.4 now emit :exc:`DeprecationWarning`. (Contributed
152+
by Matthias Bussonnier in :issue:`29576`)
153+
143154

144155
Removed
145156
=======

Lib/importlib/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def find_loader(name, path=None):
7979
This function is deprecated in favor of importlib.util.find_spec().
8080
8181
"""
82-
warnings.warn('Use importlib.util.find_spec() instead.',
82+
warnings.warn('Deprecated since Python 3.4. '
83+
'Use importlib.util.find_spec() instead.',
8384
DeprecationWarning, stacklevel=2)
8485
try:
8586
loader = sys.modules[name].__loader__

Lib/importlib/abc.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
except ImportError as exc:
1414
_frozen_importlib_external = _bootstrap_external
1515
import abc
16+
import warnings
1617

1718

1819
def _register(abstract_cls, *classes):
@@ -34,6 +35,8 @@ class Finder(metaclass=abc.ABCMeta):
3435
reimplementations of the import system. Otherwise, finder
3536
implementations should derive from the more specific MetaPathFinder
3637
or PathEntryFinder ABCs.
38+
39+
Deprecated since Python 3.3
3740
"""
3841

3942
@abc.abstractmethod
@@ -57,11 +60,16 @@ def find_module(self, fullname, path):
5760
If no module is found, return None. The fullname is a str and
5861
the path is a list of strings or None.
5962
60-
This method is deprecated in favor of finder.find_spec(). If find_spec()
61-
exists then backwards-compatible functionality is provided for this
62-
method.
63+
This method is deprecated since Python 3.4 in favor of
64+
finder.find_spec(). If find_spec() exists then backwards-compatible
65+
functionality is provided for this method.
6366
6467
"""
68+
warnings.warn("MetaPathFinder.find_module() is deprecated since Python "
69+
"3.4 in favor of MetaPathFinder.find_spec()"
70+
"(available since 3.4)",
71+
DeprecationWarning,
72+
stacklevel=2)
6573
if not hasattr(self, 'find_spec'):
6674
return None
6775
found = self.find_spec(fullname, path)
@@ -94,10 +102,15 @@ def find_loader(self, fullname):
94102
The portion will be discarded if another path entry finder
95103
locates the module as a normal module or package.
96104
97-
This method is deprecated in favor of finder.find_spec(). If find_spec()
98-
is provided than backwards-compatible functionality is provided.
99-
105+
This method is deprecated since Python 3.4 in favor of
106+
finder.find_spec(). If find_spec() is provided than backwards-compatible
107+
functionality is provided.
100108
"""
109+
warnings.warn("PathEntryFinder.find_loader() is deprecated since Python "
110+
"3.4 in favor of PathEntryFinder.find_spec() "
111+
"(available since 3.4)",
112+
DeprecationWarning,
113+
stacklevel=2)
101114
if not hasattr(self, 'find_spec'):
102115
return None, []
103116
found = self.find_spec(fullname)

Lib/test/test_importlib/test_abc.py

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def test_invalidate_caches(self):
163163
# Calling the method is a no-op.
164164
self.ins.invalidate_caches()
165165

166+
def test_find_module_warns(self):
167+
with self.assertWarns(DeprecationWarning):
168+
self.ins.find_module('something', None)
166169

167170
(Frozen_MPFDefaultTests,
168171
Source_MPFDefaultTests
@@ -189,6 +192,9 @@ def test_invalidate_caches(self):
189192
# Should be a no-op.
190193
self.ins.invalidate_caches()
191194

195+
def test_find_loader_warns(self):
196+
with self.assertWarns(DeprecationWarning):
197+
self.ins.find_loader('something')
192198

193199
(Frozen_PEFDefaultTests,
194200
Source_PEFDefaultTests

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ Extension Modules
229229
Library
230230
-------
231231

232+
- bpo-29576: Improve some deprecations in importlib. Some deprecated methods
233+
now emit DeprecationWarnings and have better descriptive messages.
234+
232235
- bpo-29534: Fixed different behaviour of Decimal.from_float()
233236
for _decimal and _pydecimal. Thanks Andrew Nester.
234237

0 commit comments

Comments
 (0)