-
-
Notifications
You must be signed in to change notification settings - Fork 318
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
Refactor lazy imports #976
Comments
We have adopted this lazy loading approach in MNE-Python and I have to say I'm not a fan. It feels extremely hacky and basically abuses
I'd also say this is not quite correct. A few people wrote a proposal, and a handful of packages have adopted it. That's about it. There are very good reasons why the PEP for lazy loading was rejected not so long ago. |
Oh very interesting, thank you! |
Don't get me wrong, I'm not telling you what and what not to do :) I just wanted to share my personal feelings and experiences here. |
Upstream at CPython, there have been recent PRs to defer some imports in stdlib modules to improve import time: python/cpython#109653 If you see some other candidates, feel free to comment on the issue. No guarantees though, some maintainers are cautious about these kind of changes. |
I was unaware of such an effort, that's awesome! That will help CLIs everywhere in perpetuity. edit: also apps running in function-based models like AWS Lambda |
Forgive me if this is duplicative information, but python importlib has lazy loading utilities available: https://docs.python.org/3/library/importlib.html#importlib.util.LazyLoader |
Yes but you have to use that programmatically rather than importing as usual or something that resembles imports. |
Apologies for the issue resurrection, but I was curious what other options have been considered beyond SPEC 1 and the basic importlib
Disclaimer: The last one is mine. Also, you can avoid using any of the linked code by (re)implementing something like it yourself if you want; I'm more so using it as examples of a few different possible solution mechanisms to learn whether any fit the needs of hatch, and if none do, learn what hatch does need that existing lazy import implementations don't fulfill. |
It's unlikely I would do something that would modify the interpreter generally because we support plugins and don't want to impact them at all. |
Modifying the interpreter would be going farther than even I imagined, haha. Makes total sense. My previous concrete suggestions, at least, all have context-manager-level or specific-package-level scoping for affecting the behavior of import statements or have configuration options for tuning what they affect, so they'd be much more conservative. Anyway, just wanted to float some ideas. If they're too invasive for this use case, so be it. |
I appreciate the effort and could be wrong, I just took a cursory glance because I don't have much time right now. I'll circle back to this eventually, thanks! |
No worries. Creating better forms of lazy importing has been a personal passion as of late, so I figured I'd give my two cents in hopes of helping, that's all. |
Currently, to keep the CLI responsive we import most things where they are used rather than at the top of modules. The scientific Python community has coalesced on a design and a reference implementation that we should adopt:
This will be used ubiquitously throughout not just Hatch but also Hatchling.
Special care must be taken when adding dependencies to Hatchling and I think this is fine for the following reasons:
pluggy
soon so it will just be a tradeThe text was updated successfully, but these errors were encountered: