Skip to content

[MNT] address deprecation of load_module#190

Merged
fkiraly merged 7 commits into
mainfrom
deprec-load_module
Oct 4, 2023
Merged

[MNT] address deprecation of load_module#190
fkiraly merged 7 commits into
mainfrom
deprec-load_module

Conversation

@fkiraly
Copy link
Copy Markdown
Contributor

@fkiraly fkiraly commented May 19, 2023

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

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

Comment thread skbase/lookup/_lookup.py Fixed
Comment thread skbase/lookup/_lookup.py Fixed
@fkiraly
Copy link
Copy Markdown
Contributor Author

fkiraly commented Jun 22, 2023

FYI @yarnabrina - perhaps you see a quick fix to this? I'm not sure what to replace the deprecated logic by, is the problem.

@yarnabrina
Copy link
Copy Markdown
Member

Sorry, I have not used exec_module ever. But the documented code seems be in a different format (for example, with arguments).

https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly

If you can explain me what you are trying to do, for example why the if block is not enough and it has to go to eif block, I can try to think on this. Given this is a internal function, can you not control what is passed to this function?

@fkiraly
Copy link
Copy Markdown
Contributor Author

fkiraly commented Jun 27, 2023

Re the "what", the issue is simply that load_module is deprecated, and I am trying to preserve the logic of the affected code block by any means, without using load_module. There are no further constraints.

@fkiraly fkiraly changed the title [MNT] addresses deprecation of load_module [MNT] address deprecation of load_module Oct 3, 2023
@fkiraly
Copy link
Copy Markdown
Contributor Author

fkiraly commented Oct 4, 2023

for posterity, this was highly non-trivial.
I reverse-engineered the solution from wntrblm/nox#498.

Any line imported_mod = module.load_module() for a module: SourceFileLoader can equivalently be replaced by apparently equivalent the four lines

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 here.

@fkiraly fkiraly added the maintenance Continuous integration, unit testing & package distribution label Oct 4, 2023
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 4, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (38cef3d) 83.60% compared to head (a2aa811) 83.60%.
Report is 1 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #190   +/-   ##
=======================================
  Coverage   83.60%   83.60%           
=======================================
  Files          43       43           
  Lines        2843     2843           
=======================================
  Hits         2377     2377           
  Misses        466      466           
Files Coverage Δ
skbase/lookup/_lookup.py 93.86% <100.00%> (+0.07%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Continuous integration, unit testing & package distribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants