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

Replace use of deprecated package imp with current importlib #189

Merged
merged 7 commits into from
Jun 19, 2023

Conversation

jafingerhut
Copy link
Collaborator

No description provided.

@jafingerhut
Copy link
Collaborator Author

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.

@jafingerhut
Copy link
Collaborator Author

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)
Copy link
Contributor

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

Copy link
Member

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:

ptf/setup.cfg

Line 22 in 9c1787d

python_requires = >=2.7, >=3
(note that I don't have an issue with this version requirement).

Copy link
Collaborator Author

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
Comment on lines 111 to 114
# 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)])
Copy link
Member

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

Copy link
Collaborator Author

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.

Copy link
Contributor

@fruffy fruffy Jun 17, 2023

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.

Copy link
Member

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

Copy link
Member

@antoninbas antoninbas left a 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:

ptf/setup.cfg

Line 22 in c458186

python_requires = >=3

@jafingerhut
Copy link
Collaborator Author

Added Python >= 3.4 requirement in setup.cfg with commit 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants