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

Package sh breaks --import-mode=importlib #7689

Closed
sbacchio opened this issue Aug 26, 2020 · 2 comments
Closed

Package sh breaks --import-mode=importlib #7689

sbacchio opened this issue Aug 26, 2020 · 2 comments
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed type: question general question, might be closed after 2 weeks of inactivity

Comments

@sbacchio
Copy link

Hi, I have track down an issue involving the package sh and the option --import-mode=importlib.
When package sh is loaded, an importer is added to sys.meta_path:

>>> import sys
>>> sys.meta_path
[<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
>>> import sh
>>> sys.meta_path
[<sh.ModuleImporterFromVariables object at 0x7f567a9e4a10>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
>>> 

This breaks the importing system of pytest at this line, since the new importer does not have the method find_spec.
I get, indeed, the following error:

../../../anaconda3/lib/python3.7/site-packages/_pytest/pathlib.py:483: in import_path
    spec = meta_importer.find_spec(module_name, [str(path.parent)])
E   AttributeError: 'ModuleImporterFromVariables' object has no attribute 'find_spec'

Should this issue be resolved from pytest side, or from sh side?

@Zac-HD Zac-HD added topic: collection related to the collection phase type: bug problem that needs to be addressed type: question general question, might be closed after 2 weeks of inactivity labels Aug 27, 2020
@sbacchio
Copy link
Author

The necessary changes in sh (amoffat/sh#536) have been made and this issue should be resolved with the next release of the module.

I understand that find_module (e.g. this was used in sh) has been deprecated in favor of find_spec, but maybe pytest should support both in order to avoid similar issues?

I could open a PR with the following changes:

-             spec = meta_importer.find_spec(module_name, [str(path.parent)])
+             try:
+                 spec = meta_importer.find_spec(module_name, [str(path.parent)])
+             except AttributeError:
+                 found = meta_importer.find_module(module_name, [str(path.parent)])
+                 spec = ModuleSpec(module_name, found) if found is not None else None

with from importlib.machinery import ModuleSpec in the imports

@nicoddemus
Copy link
Member

Thanks @sbacchio for the report!

I understand that find_module (e.g. this was used in sh) has been deprecated in favor of find_spec, but maybe pytest should support both in order to avoid similar issues?

While the proposed suggestion is simple, it incurs some maintenance overhead because in the future we will need to also remove this code (with breaking release possibly), and in general fixing this "upstream" in the affected packages is more beneficial overall.

For this reason I would rather wait and see if this affects more modules: so far it doesn't seem to be the case, don't know if because people don't use --importmode=importlib much or there's no problems.

Again thanks for the report and patching up sh. 👍

Closing this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants