Initial setup of lazy_import functions.#4909
Conversation
Still needs: - way to handle pytest.importorskip - Loader class instead of monkey patch on SourceFileLoader - Way to identify a lazily-loaded-not-yet-used module
|
I believe the test failures here are due to not replacing I've settled on mimicking the They should be fairly easy to read now. I'll start workign on figuring out how much of the code has to be changed to enable |
stefanv
left a comment
There was a problem hiding this comment.
Nice work, Dan! This looks great already. I just had a few questions (mainly for my own enlightenment).
networkx/tests/test_lazy_imports.py
Outdated
| pass | ||
|
|
||
|
|
||
| def test_lazy_package_import(): |
There was a problem hiding this comment.
This is nice; would be good to get something like that over to the skimage PR too!
There was a problem hiding this comment.
This is nice; would be good to get something like that over to the skimage PR too!
I'm going to try to get something like these tests of attach over in the skimage PR as suggested.
Should I also update the lazy.load function there to allow "delayed failed modules"? It currently raises a ModuleNotFoundError immediately upon lazy.load when a library is not found. That works well for many cases, but not if you want to import optional libraries. Thoughts @stefanv ?
There was a problem hiding this comment.
I think that would be a good feature to have, thanks Dan!
|
Some of these ideas might be helpful in the PR over at: scikit-image/scikit-image#5101 |
|
Looks like the differences between the NX lazy-load and scikit lazy-load are:
What are the next steps?
Note: number 1) is done to Finally, shall I make these "next steps" in this PR? I'm thinking maybe making another PR that relies on this one. It will touch a lot of code to change the imports. |
stefanv
left a comment
There was a problem hiding this comment.
This looks good to me.
As @dschult mentioned, the biggest change from the skimage approach here is that import exceptions are delayed. This means that a user may be able to import a subpackage successfully, but that code will break once it is used. In practice (a) it will allow more code to execute successfully [if your code doesn't use a specific import, it won't fail] (b) raise errors at a different time to what users are currently accustomed to (on function execution rather than module import). I think (b) is not an issue for two reasons: (1) these imports used to reside inside functions before load anyway and (2) the difference will only really be noticeable in interactive work, where raising the exception later makes more sense.
OK, I may have convinced myself to port that change over to skimage as well ;)
|
Thanks @Stefan, But I guess they are currently being prompted to move the import inside their functions. So that's likely to be about the same maintenance burden. The CI will check it either way too. |
True :) I commented on the other PR that slightly nicer syntax may be something like |
|
I was using the function name "load" because of the version in skimage. But maybe that's not the best name... Why is the term "load" used? That seems to normally refer to bringing data from a file into a variable. The standard term in Python seems to be "import". Is there a reason to avoid the term "import" here, e.g. |
|
We went through various discussions on naming and that is what we settled on, but I don't think there's any particularly good reason for it other to distinguish it from a traditional import. |
* Initial setup of lazy_import functions. Still needs: - way to handle pytest.importorskip --> soln is not to load delayed modules into sys.modules - Loader class instead of monkey patch on SourceFileLoader - Way to identify a lazily-loaded-not-yet-used module --> now create an instance of the Delayed Module class. * fix importorskip with new module class * Remove lazy_importorskip. Don't add Delayed reporting module to sys.modules * make tests work for pypy * refactor to include changes from skimage.lazy * fix test handling of types.ModuleType * Change name from nx.lazy_imports.load to nx.lazy_import * fix tests to use new name. keep `attach` name as is.
* Initial setup of lazy_import functions. Still needs: - way to handle pytest.importorskip --> soln is not to load delayed modules into sys.modules - Loader class instead of monkey patch on SourceFileLoader - Way to identify a lazily-loaded-not-yet-used module --> now create an instance of the Delayed Module class. * fix importorskip with new module class * Remove lazy_importorskip. Don't add Delayed reporting module to sys.modules * make tests work for pypy * refactor to include changes from skimage.lazy * fix test handling of types.ModuleType * Change name from nx.lazy_imports.load to nx.lazy_import * fix tests to use new name. keep `attach` name as is.
Lazy load of potentially uninstalled libraries.
Also, a function to set up the internal sub-packages in a lazy-load fashion.
This first version will fail tests in environments without numpy because pytest.importorskip is not lazy yet.
Still needs: