Skip to content

Commit

Permalink
Improve some deprecations in the importlib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Carreau committed Feb 15, 2017
1 parent f15fa87 commit 51e8898
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Lib/importlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def find_loader(name, path=None):
This function is deprecated in favor of importlib.util.find_spec().
"""
warnings.warn('Use importlib.util.find_spec() instead.',
warnings.warn('Deprecated since Python 3.4. '
'Use importlib.util.find_spec() instead.',
DeprecationWarning, stacklevel=2)
try:
loader = sys.modules[name].__loader__
Expand Down
25 changes: 19 additions & 6 deletions Lib/importlib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
except ImportError as exc:
_frozen_importlib_external = _bootstrap_external
import abc
import warnings


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

@abc.abstractmethod
Expand All @@ -57,11 +60,16 @@ def find_module(self, fullname, path):
If no module is found, return None. The fullname is a str and
the path is a list of strings or None.
This method is deprecated in favor of finder.find_spec(). If find_spec()
exists then backwards-compatible functionality is provided for this
method.
This method is deprecated since Python 3.4 in favor of
finder.find_spec(). If find_spec() exists then backwards-compatible
functionality is provided for this method.
"""
warnings.warn("MetaPathFinder.find_module() is deprecated since Python "
"3.4 in favor of MetaPathFinder.find_spec()"
"(available since 3.4)",
DeprecationWarning,
stacklevel=2)
if not hasattr(self, 'find_spec'):
return None
found = self.find_spec(fullname, path)
Expand Down Expand Up @@ -94,10 +102,15 @@ def find_loader(self, fullname):
The portion will be discarded if another path entry finder
locates the module as a normal module or package.
This method is deprecated in favor of finder.find_spec(). If find_spec()
is provided than backwards-compatible functionality is provided.
This method is deprecated since Python 3.4 in favor of
finder.find_spec(). If find_spec() is provided than backwards-compatible
functionality is provided.
"""
warnings.warn("PathEntryFinder.find_loader() is deprecated since Python "
"3.4 in favor of PathEntryFinder.find_spec() "
"(available since 3.4)",
DeprecationWarning,
stacklevel=2)
if not hasattr(self, 'find_spec'):
return None, []
found = self.find_spec(fullname)
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_importlib/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def test_invalidate_caches(self):
# Calling the method is a no-op.
self.ins.invalidate_caches()

def test_find_module_warns(self):
with self.assertWarns(DeprecationWarning):
self.ins.find_module('something', None)

(Frozen_MPFDefaultTests,
Source_MPFDefaultTests
Expand All @@ -189,6 +192,9 @@ def test_invalidate_caches(self):
# Should be a no-op.
self.ins.invalidate_caches()

def test_find_loader_warns(self):
with self.assertWarns(DeprecationWarning):
self.ins.find_loader('something')

(Frozen_PEFDefaultTests,
Source_PEFDefaultTests
Expand Down

0 comments on commit 51e8898

Please sign in to comment.