Skip to content

Warn and discourage lazy.load of subpackages #57

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

Merged
merged 4 commits into from
Jun 28, 2023

Conversation

dschult
Copy link
Member

@dschult dschult commented Jun 20, 2023

The importlib.find_spec tool automatically (and eagerly) imports packages when subpackages are asked to be found. The older find_loader did not do that, but it is deprecated and will be removed in python v3.12. While it might be possible to make the load feature work using manually build ModuleSpecs, 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 to attach the subpackages so they can be lazily loaded.

That said, packages can still be lazily loaded. And even subpackages can be lazily loaded though this causes the package to be eagerly imported. So there might be some use for this load 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 of load 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?

@codecov-commenter
Copy link

codecov-commenter commented Jun 20, 2023

Codecov Report

Merging #57 (42ac558) into main (a6188ac) will increase coverage by 3.42%.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main      #57      +/-   ##
==========================================
+ Coverage   90.53%   93.95%   +3.42%     
==========================================
  Files           4        4              
  Lines         169      182      +13     
==========================================
+ Hits          153      171      +18     
+ Misses         16       11       -5     
Impacted Files Coverage Δ
lazy_loader/__init__.py 91.86% <100.00%> (+6.49%) ⬆️
lazy_loader/tests/test_lazy_loader.py 96.73% <100.00%> (+0.35%) ⬆️

@jarrodmillman jarrodmillman added this to the 0.3 milestone Jun 20, 2023
@jarrodmillman jarrodmillman requested a review from stefanv June 21, 2023 06:34
@jarrodmillman
Copy link
Member

LGTM. We can always deprecate (and later remove) load in a subsequent PR if we want.

@stefanv
Copy link
Member

stefanv commented Jun 27, 2023

Thanks you @dschult. It's also worth referencing #55.

@jarrodmillman jarrodmillman merged commit 874085d into scientific-python:main Jun 28, 2023
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.

4 participants