diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index 693b466112638f..58d1787bef8e72 100644 --- a/Lib/importlib/_abc.py +++ b/Lib/importlib/_abc.py @@ -1,11 +1,16 @@ """Subset of importlib.abc used to reduce importlib.util imports.""" from . import _bootstrap import abc +import warnings class Loader(metaclass=abc.ABCMeta): """Abstract base class for import loaders.""" + def __init__(self): + warnings.warn(f"Loader is deprecated.", + DeprecationWarning, stacklevel=2) + super().__init__() def create_module(self, spec): """Return a module to initialize and into which to load. diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 1b76328429f63a..34e13ae980daa4 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -765,6 +765,10 @@ def path_mtime(self, path): Raises OSError when the path cannot be handled. """ + _warnings.warn( + f"SourcelessFileLoader is deprecated.", + DeprecationWarning, + stacklevel=2) raise OSError def path_stats(self, path): @@ -1000,6 +1004,13 @@ def set_data(self, path, data, *, _mode=0o666): class SourcelessFileLoader(FileLoader, _LoaderBasics): """Loader which handles sourceless file imports.""" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + _warnings.warn( + f"SourcelessFileLoader is deprecated.", + DeprecationWarning, + stacklevel=2 + ) def get_code(self, fullname): path = self.get_filename(fullname) diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py index eea6b38af6fa13..5d8abb4b8006be 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -13,6 +13,7 @@ _frozen_importlib_external = _bootstrap_external from ._abc import Loader import abc +import warnings __all__ = [ @@ -22,6 +23,11 @@ ] +def __getattr__(name): + if name == 'ResourceLoader': + warnings.warn(f"The '{name}' attribute is deprecated.", + DeprecationWarning, stacklevel=2) + def _register(abstract_cls, *classes): for cls in classes: abstract_cls.register(cls) @@ -69,6 +75,8 @@ class ResourceLoader(Loader): This ABC represents one of the optional protocols specified by PEP 302. """ + warnings.warn(f"The Resource Loader class is deprecated.", + DeprecationWarning, stacklevel=2) @abc.abstractmethod def get_data(self, path): @@ -85,6 +93,8 @@ class InspectLoader(Loader): This ABC represents one of the optional protocols specified by PEP 302. """ + warnings.warn(f"The InspectLoader class is deprecated.", + DeprecationWarning, stacklevel=2) def is_package(self, fullname): """Optional method which when implemented should return whether the @@ -198,6 +208,9 @@ class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLo """ def path_mtime(self, path): + """Deprecated""" + warnings.warn(f"The path_mtime function is deprecated.", + DeprecationWarning, stacklevel=2) """Return the (int) modification time for the path (str).""" if self.path_stats.__func__ is SourceLoader.path_stats: raise OSError diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py index 6e294d59bfdcb9..b5acad54d98699 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -1,5 +1,6 @@ """The machinery of importlib: finders, loaders, hooks, etc.""" +import warnings from ._bootstrap import ModuleSpec from ._bootstrap import BuiltinImporter from ._bootstrap import FrozenImporter @@ -27,3 +28,15 @@ def all_suffixes(): 'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder', 'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader', 'WindowsRegistryFinder', 'all_suffixes'] + + +def __getattr__(name): + if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder'): + if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES'): + warnings.warn(f"The '{name}' module is deprecated.", + DeprecationWarning, stacklevel=2) + return name + else: + warnings.warn(f"The '{name}' class is deprecated.", + DeprecationWarning, stacklevel=2) + return name diff --git a/Misc/NEWS.d/next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst b/Misc/NEWS.d/next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst new file mode 100644 index 00000000000000..8faed4cddfb61a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst @@ -0,0 +1 @@ +This PR adds some deprecation warnings to importlib