Warn and discourage lazy.load of subpackages #57
Merged
+47
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
importlib.find_spec
tool automatically (and eagerly) imports packages when subpackages are asked to be found. The olderfind_loader
did not do that, but it is deprecated and will be removed in python v3.12. While it might be possible to make theload
feature work using manually buildModuleSpec
s, the payoff is unlikely to be worth the convoluted errors obtained when something goes awry. It would/will be much more effective for people to encourage the package toattach
the subpackages so they can be lazily loaded.That said, packages can still be lazily
load
ed. And even subpackages can be lazilyload
ed though this causes the package to be eagerly imported. So there might be some use for thisload
function. This potential benefit might be enough reason to keep the function in this library. But I can also see that removing the function altogether would be reasonable too.With that in mind, this PR changes the function to raise a RuntimeWarning when the input is a subpackage (has "." in the name of the package). It also changes the doc_strings to discourage the use of
lazy.load(sub.package)
. And it adds a test to check for the warning, and to verify that the return value ofload
with subpackage input continues to provide an eagerly imported package with a lazily loaded subpackage.I think this is better than leaving
load
unchanged -- some of the docs even suggested using it for subpackages.But, is this better than removing
load
?