-
Notifications
You must be signed in to change notification settings - Fork 100
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
Replace use of deprecated package imp with current importlib #189
Replace use of deprecated package imp with current importlib #189
Conversation
Possible fix for issue #187 Warning: I am poking things with a stick without any kind of deep understanding of the imp and importlib packages. I have tested this on an Ubuntu 20.04 system with python 3.8.10 installed, and several PTF tests I tried worked fine and no longer give the deprecation warnings. |
I have looked at the test failure results, but it was not clear to me why the failure occurred. |
ptf
Outdated
@@ -544,7 +544,7 @@ def load_test_modules(config): | |||
if modname in sys.modules: | |||
mod = sys.modules[modname] | |||
else: | |||
mod = imp.load_module(modname, *imp.find_module(modname, [root])) | |||
mod = importlib.import_module(modname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last time I messed around with importlib it was a little bit more complicated than that. This is missing the root folder as path.
I remember you had to use find_spec and load_module instead.
def import_module(root_path, module_name):
""" Try to import a module and class directly instead of the typical
Python method. Allows for dynamic imports. """
finder = importlib.machinery.PathFinder()
# Unfortunately this does not support Posix paths and silently fails. So we have to use str().
# This is a standard lib function...
module_specs = finder.find_spec(str(module_name), [str(root_path)])
return module_specs.loader.load_module()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fruffy is right here
The test failure means that no test specs can be collected, and this is definitely connected to this change.
You should be able to run the test scripts locally to reproduce.
Additionally this change means that Python 3.1 is required, so this line needs to be changed accordingly:
Line 22 in 9c1787d
python_requires = >=2.7, >=3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have fixed this. Still poking things with a stick in terms of my knowledge level :-)
ptf
Outdated
# Unfortunately this does not support Posix paths and silently | ||
# fails. So we have to use str(). | ||
# This is a standard lib function... | ||
module_specs = finder.find_spec(str(module_name), [str(root_path)]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit confusing to me: aren't module_name
and root_path
already strings? looks like they should be based on the call sites below.
when I ran some experiments, it seemed that os.path.splitext
for example was returning strings, not path objects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed the comments and calls to str() here after confirming that the way this def import_module was being called, its parameters were always type str anyway.
I suspect those comments were copied and pasted from some other place where Fabian was using it, where that was not the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it makes sense, but the function was initially written to be more general. find_spec
does not seem to work if root_path
is a Path
object. The path has to be explicitly converted. Explicit type checking might be a better approach here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the limited use here (2 callers in the same file) and given that we are aligned with find_spec
as far as parameter types are concerned, I think the current solution is good enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
However if I understand the docs correctly, this requires Python 3.4 or above: https://docs.python.org/3/library/importlib.html#importlib.machinery.PathFinder.find_spec
This is perfectly fine, but we need to update the following accordingly:
Line 22 in c458186
python_requires = >=3 |
Added Python >= 3.4 requirement in setup.cfg with commit 7 |
No description provided.