From 03bbac08d7857b8893d9aa454d857e409bf36db6 Mon Sep 17 00:00:00 2001 From: rashansmith <smith.rashan@gmail.com> Date: Sun, 14 Jul 2024 12:58:55 +0200 Subject: [PATCH 1/6] Add deprecation warnings --- Lib/importlib/_abc.py | 5 +++++ Lib/importlib/_bootstrap_external.py | 11 +++++++++++ Lib/importlib/abc.py | 13 +++++++++++++ Lib/importlib/machinery.py | 12 +++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index 693b466112638f..7ad7e7af3071fd 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 bf14d57b2503ea..7022776a1038a9 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1037,6 +1037,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): @@ -1272,6 +1276,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..02dd56c5bd4d36 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 in ('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..29804ce466a4be 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -1,5 +1,5 @@ """The machinery of importlib: finders, loaders, hooks, etc.""" - +import warnings from ._bootstrap import ModuleSpec from ._bootstrap import BuiltinImporter from ._bootstrap import FrozenImporter @@ -27,3 +27,13 @@ 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' ): + warnings.warn(f"The '{name}' module suffix is deprecated.", + DeprecationWarning, stacklevel=2) + if name == 'DEBUG_BYTECODE_SUFFIXES': + return 1 + else: + return 2 From 820ac2162a88dd50bcb3ec945f01e5f3595866f9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 14 Jul 2024 12:46:03 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-07-14-12-46-02.gh-issue-121754.2Tt2fE.rst 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 From d6c0645cbcf23e07393eeb274d7253c7d344933b Mon Sep 17 00:00:00 2001 From: Rashan <smith.rashan@gmail.com> Date: Sun, 14 Jul 2024 14:46:43 +0200 Subject: [PATCH 3/6] Update Lib/importlib/_abc.py Co-authored-by: Tomas R <tomas.roun8@gmail.com> --- Lib/importlib/_abc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index 7ad7e7af3071fd..58d1787bef8e72 100644 --- a/Lib/importlib/_abc.py +++ b/Lib/importlib/_abc.py @@ -9,7 +9,7 @@ class Loader(metaclass=abc.ABCMeta): """Abstract base class for import loaders.""" def __init__(self): warnings.warn(f"Loader is deprecated.", - DeprecationWarning, stacklevel=2) + DeprecationWarning, stacklevel=2) super().__init__() def create_module(self, spec): From cf8264da298d584bd44ae9dbfaa35549aa22d299 Mon Sep 17 00:00:00 2001 From: Rashan <smith.rashan@gmail.com> Date: Sun, 14 Jul 2024 15:02:43 +0200 Subject: [PATCH 4/6] Apply suggestions from code review Thanks for the review Co-authored-by: Tomas R <tomas.roun8@gmail.com> --- Lib/importlib/abc.py | 8 ++++---- Lib/importlib/machinery.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py index 02dd56c5bd4d36..5d8abb4b8006be 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -24,7 +24,7 @@ def __getattr__(name): - if name in ('ResourceLoader' ): + if name == 'ResourceLoader': warnings.warn(f"The '{name}' attribute is deprecated.", DeprecationWarning, stacklevel=2) @@ -76,7 +76,7 @@ class ResourceLoader(Loader): """ warnings.warn(f"The Resource Loader class is deprecated.", - DeprecationWarning, stacklevel=2) + DeprecationWarning, stacklevel=2) @abc.abstractmethod def get_data(self, path): @@ -94,7 +94,7 @@ class InspectLoader(Loader): """ warnings.warn(f"The InspectLoader class is deprecated.", - DeprecationWarning, stacklevel=2) + DeprecationWarning, stacklevel=2) def is_package(self, fullname): """Optional method which when implemented should return whether the @@ -210,7 +210,7 @@ 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) + 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 29804ce466a4be..b4ad76261f2417 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -30,7 +30,7 @@ def all_suffixes(): def __getattr__(name): - if name in ('DEBUG_BYTECODE_SUFFIXES','OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder' ): + if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder'): warnings.warn(f"The '{name}' module suffix is deprecated.", DeprecationWarning, stacklevel=2) if name == 'DEBUG_BYTECODE_SUFFIXES': From cfbe0795add0061c862afd316a9670ab3291d810 Mon Sep 17 00:00:00 2001 From: rashansmith <smith.rashan@gmail.com> Date: Sun, 14 Jul 2024 15:06:54 +0200 Subject: [PATCH 5/6] Update machinery deprecation warning --- Lib/importlib/machinery.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py index b4ad76261f2417..d7f9a784628712 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -1,4 +1,5 @@ """The machinery of importlib: finders, loaders, hooks, etc.""" + import warnings from ._bootstrap import ModuleSpec from ._bootstrap import BuiltinImporter @@ -31,9 +32,11 @@ def all_suffixes(): def __getattr__(name): if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder'): - warnings.warn(f"The '{name}' module suffix is deprecated.", + if name in ('DEBUG_BYTECODE_SUFFIXES','OPTIMIZED_BYTECODE_SUFFIXES'): + warnings.warn(f"The '{name}' module is deprecated.", DeprecationWarning, stacklevel=2) - if name == 'DEBUG_BYTECODE_SUFFIXES': - return 1 + return name else: - return 2 + warnings.warn(f"The '{name}' class is deprecated.", + DeprecationWarning, stacklevel=2) + return name From 62944585f997122bde4434ef9acf2f8cfbbc968f Mon Sep 17 00:00:00 2001 From: Rashan <smith.rashan@gmail.com> Date: Mon, 15 Jul 2024 09:54:15 +0200 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Charlie Zhao <zhaoyu_hit@qq.com> --- Lib/importlib/machinery.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py index d7f9a784628712..b5acad54d98699 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -32,11 +32,11 @@ def all_suffixes(): def __getattr__(name): if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES', 'WindowsRegistryFinder'): - if name in ('DEBUG_BYTECODE_SUFFIXES','OPTIMIZED_BYTECODE_SUFFIXES'): + if name in ('DEBUG_BYTECODE_SUFFIXES', 'OPTIMIZED_BYTECODE_SUFFIXES'): warnings.warn(f"The '{name}' module is deprecated.", - DeprecationWarning, stacklevel=2) + DeprecationWarning, stacklevel=2) return name else: warnings.warn(f"The '{name}' class is deprecated.", - DeprecationWarning, stacklevel=2) + DeprecationWarning, stacklevel=2) return name