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

👌 IMPROVE: Replace deprecated imp with importlib #4848

Merged
merged 7 commits into from
May 6, 2021
14 changes: 9 additions & 5 deletions aiida/restapi/translator/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
###########################################################################
"""Translator for node"""
import pkgutil
import imp
import importlib
import sys
import inspect
import os

Expand Down Expand Up @@ -346,12 +347,15 @@ def _get_subclasses(self, parent=None, parent_class=None, recursive=True):
full_path_base = os.path.join(package_path, name)

if is_pkg:
app_module = imp.load_package(full_path_base, full_path_base)
spec = importlib.util.spec_from_file_location(name, full_path_base)
app_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(app_module)
else:
full_path = f'{full_path_base}.py'
# I could use load_module but it takes lots of arguments,
# then I use load_source
app_module = imp.load_source(f'rst{name}', full_path)
spec = importlib.util.spec_from_file_location(f'rst{name}', full_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Suggested change
spec = importlib.util.spec_from_file_location(f'rst{name}', full_path)
spec = importlib.util.spec_from_file_location(f'{name}', full_path)

app_module = importlib.util.module_from_spec(spec)
sys.modules[f'rst{name}'] = app_module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here (or perhaps I don't understand what this does).

is this some weird autocomplete of "firstname" in the editor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and I have changed them accordingly

spec.loader.exec_module(app_module)

# Go through the content of the module
if not is_pkg:
Expand Down