Skip to content

Commit 51e8898

Browse files
committed
Improve some deprecations in the importlib
Add the python version since the functionality is deprecated, and raise a couple of deprecation warnings in a few places. Theses functions are marked as deprecated in the documentation, but especially in existing codebase, programmers tends to not re-check whether functions are deprecated. So trigger the warning when possible. It's also more probable that a developer will drop deprecated functionality if we immediately give them information about replacement API, and not have them to go find it in the documentation. Include deprecation information in DocString as well as many tools pull documentation from there and not from docs.python.org. Add test making sure `find_loader()` and `find_module()` Both emit a deprecation warning.
1 parent f15fa87 commit 51e8898

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

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

0 commit comments

Comments
 (0)