1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2024-10-05 18:31:12 +01:00

utils/misc: Replace deprecated __import__ by importlib

Use importlib.import_module instead of __import__ as per Python doc
recommendation.

This will also fix the case where the class is in a
package's submodule (since __import__ returns the top-level package),
e.g. "foo.bar.Class".
This commit is contained in:
Douglas Raillard 2023-10-05 12:18:43 +01:00 committed by Marc Bonnici
parent 77ebefba08
commit 28b78a93f1

View File

@ -236,7 +236,12 @@ def load_class(classpath):
"""Loads the specified Python class. ``classpath`` must be a fully-qualified """Loads the specified Python class. ``classpath`` must be a fully-qualified
class name (i.e. namspaced under module/package).""" class name (i.e. namspaced under module/package)."""
modname, clsname = classpath.rsplit('.', 1) modname, clsname = classpath.rsplit('.', 1)
return getattr(__import__(modname), clsname) mod = importlib.import_module(modname)
cls = getattr(mod, clsname)
if isinstance(cls, type):
return cls
else:
raise ValueError(f'The classpath "{classpath}" does not point at a class: {cls}')
def get_pager(): def get_pager():