Skip to content

Commit

Permalink
[MNT] address deprecation of load_module (#190)
Browse files Browse the repository at this point in the history
`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`, wntrblm/nox#498
  • Loading branch information
fkiraly committed Oct 4, 2023
1 parent 95ed9bc commit b8b483e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion skbase/lookup/_lookup.py
Expand Up @@ -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
Expand Down

0 comments on commit b8b483e

Please sign in to comment.