Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNT] address deprecation of load_module #190

Merged
merged 7 commits into from Oct 4, 2023
Merged

Conversation

fkiraly
Copy link
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

skbase/lookup/_lookup.py Fixed Show fixed Hide fixed
skbase/lookup/_lookup.py Fixed Show fixed Hide fixed
@fkiraly
Copy link
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
Contributor

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

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.

@fkiraly fkiraly merged commit b8b483e into main Oct 4, 2023
18 checks passed
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.

None yet

3 participants