From 559e840053e0878a78fef34d27c5c195ac0e2ea6 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 21 Sep 2023 10:56:58 +0100 Subject: [PATCH] common: Replace deprecated importlib APIs (1/2) Resolve the following deprecation warning: DeprecationWarning: find_module() is deprecated and slated for removal in Python 3.12; use find_spec() instead Signed-off-by: Stephen Finucane Change-Id: I88005c694b94d249bce53f46df67b51c7e01adae --- heat/common/plugin_loader.py | 6 +++--- heat/tests/test_plugin_loader.py | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/heat/common/plugin_loader.py b/heat/common/plugin_loader.py index 59f75df153..90233742f3 100644 --- a/heat/common/plugin_loader.py +++ b/heat/common/plugin_loader.py @@ -66,11 +66,11 @@ def _import_module(importer, module_name, package): if module_name in sys.modules: return sys.modules[module_name] - loader = importer.find_module(module_name) - if loader is None: + module_spec = importer.find_spec(module_name) + if module_spec is None: return None - module = loader.load_module(module_name) + module = module_spec.loader.load_module(module_name) # Make this accessible through the parent package for static imports local_name = module_name.partition(package.__name__ + '.')[2] diff --git a/heat/tests/test_plugin_loader.py b/heat/tests/test_plugin_loader.py index 681e7b2e93..9a6be30e5e 100644 --- a/heat/tests/test_plugin_loader.py +++ b/heat/tests/test_plugin_loader.py @@ -12,7 +12,7 @@ # under the License. -import pkgutil +import importlib.machinery import sys from unittest import mock @@ -53,14 +53,14 @@ class PluginLoaderTest(common.HeatTestCase): def test_import_module_existing(self): import heat.engine.service existing = heat.engine.service - importer = pkgutil.ImpImporter(heat.engine.__path__[0]) + importer = importlib.machinery.FileFinder(heat.engine.__path__[0]) loaded = plugin_loader._import_module(importer, 'heat.engine.service', heat.engine) self.assertIs(existing, loaded) def test_import_module_garbage(self): - importer = pkgutil.ImpImporter(heat.engine.__path__[0]) + importer = importlib.machinery.FileFinder(heat.engine.__path__[0]) self.assertIsNone(plugin_loader._import_module(importer, 'wibble', heat.engine)) @@ -68,7 +68,7 @@ class PluginLoaderTest(common.HeatTestCase): @mock.patch.object(plugin_loader, "_import_module", mock.MagicMock()) @mock.patch('pkgutil.walk_packages') def test_load_modules_skip_test(self, mp): - importer = pkgutil.ImpImporter(heat.engine.__path__[0]) + importer = importlib.machinery.FileFinder(heat.engine.__path__[0]) mp.return_value = ((importer, "hola.foo", None), (importer, "hola.tests.test_foo", None)) @@ -79,7 +79,7 @@ class PluginLoaderTest(common.HeatTestCase): @mock.patch.object(plugin_loader, "_import_module", mock.MagicMock()) @mock.patch('pkgutil.walk_packages') def test_load_modules_skip_setup(self, mp): - importer = pkgutil.ImpImporter(heat.engine.__path__[0]) + importer = importlib.machinery.FileFinder(heat.engine.__path__[0]) mp.return_value = ((importer, "hola.foo", None), (importer, "hola.setup", None))