From b8b483e9140a479bf9518ad6d36a3856b0ce51c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Wed, 4 Oct 2023 23:43:59 +0200 Subject: [PATCH] [MNT] address deprecation of `load_module` (#190) `load_module` is deprecated and will be replaced by `exec_module` in python 3.12, this PR addresses that. The fix is highly nontrivial - the crucial line was `imported_mod = module.load_module()` for a `module: SourceFileLoader`, which had to be replaced by the equivalent four (!) lines ```python spec = importlib.util.spec_from_file_location(module.name, module.path) imported_mod = importlib.util.module_from_spec(spec) # these two are also crucial due to the side effects loader = spec.loader loader.exec_module(imported_mod) ``` The deprecation message raised by `load_module` is highly unhelpful, and I had to reverse engineer this from a very helpful PR in `nox`, https://github.com/wntrblm/nox/pull/498 --- skbase/lookup/_lookup.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/skbase/lookup/_lookup.py b/skbase/lookup/_lookup.py index fc4163ed..b45b7c5c 100644 --- a/skbase/lookup/_lookup.py +++ b/skbase/lookup/_lookup.py @@ -297,7 +297,11 @@ def _import_module( if isinstance(module, str): imported_mod = importlib.import_module(module) elif isinstance(module, importlib.machinery.SourceFileLoader): - imported_mod = module.load_module() + spec = importlib.util.spec_from_file_location(module.name, module.path) + imported_mod = importlib.util.module_from_spec(spec) + + loader = spec.loader + loader.exec_module(imported_mod) exc = None except Exception as e: # we store the exception so we can restore the stdout fisrt