From 0f7031bcd783a585a06b69e2bcacebb4a1876956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A9nainn=20Woodsend?= Date: Tue, 3 Jan 2023 01:36:59 +0000 Subject: [PATCH] Remove usages of legacy importlib API. The Python >= 3.10 compatible usages of importlib added in 277ff54b71e31531923a51459b328391d76bb5b3 can be safely applied to any version of Python >= 3.5 which covers the full range that Hydra supports. Getting rid of the old decrecated API, asides from cleaning up, will keep Hydra compatible with PyInstaller if PyInstaller doesn't get cold feet and revert its dropping of support for the legacy API (https://github.com/pyinstaller/pyinstaller/pull/7344). --- hydra/core/plugins.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/hydra/core/plugins.py b/hydra/core/plugins.py index e63a35eaeb..652a9a511a 100644 --- a/hydra/core/plugins.py +++ b/hydra/core/plugins.py @@ -182,20 +182,16 @@ def _scan_all_plugins( import_time = timer() with warnings.catch_warnings(record=True) as recorded_warnings: - if sys.version_info < (3, 10): - m = importer.find_module(modname) # type: ignore - assert m is not None - loaded_mod = m.load_module(modname) + spec = importer.find_spec(modname, None) + assert spec is not None + if modname in sys.modules: + loaded_mod = sys.modules[modname] else: - spec = importer.find_spec(modname) - assert spec is not None - if modname in sys.modules: - loaded_mod = sys.modules[modname] - else: - loaded_mod = importlib.util.module_from_spec(spec) - if loaded_mod is not None: - spec.loader.exec_module(loaded_mod) - sys.modules[modname] = loaded_mod + loaded_mod = importlib.util.module_from_spec(spec) + if loaded_mod is not None: + sys.modules[modname] = loaded_mod + assert spec.loader + spec.loader.exec_module(loaded_mod) import_time = timer() - import_time if len(recorded_warnings) > 0: