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

Lazy-load dateutil submodules? #771

Closed
pganssle opened this issue Jun 18, 2018 · 4 comments
Closed

Lazy-load dateutil submodules? #771

pganssle opened this issue Jun 18, 2018 · 4 comments

Comments

@pganssle
Copy link
Member

It is a fairly common problem (see, e.g. issue #770) that people assume that import dateutil will automatically import all of dateutil's sub-modules. Currently we don't do this because we want to be as lazy as possible about imports.

We'll need to profile it, but I think there is a "best of both worlds" option here, where in dateutil.__init__ we define lazy-loaded modules, so that the first time you invoke, e.g. dateutil.tz dateutil.tz will be imported in the background, and subsequent queries come from a cache.

For Python 3.7+, I think the best / fastest way to do this is to use module-level __getattr__ and __dir__ implementations, but for pre-Python 3.7, we may need to do something fancy and module sys.modules or something to implement the equivalent to a module-level __getattr__.

@kirit93
Copy link
Contributor

kirit93 commented Jun 26, 2018

@pganssle have you considered using lazy_import for this?
I added

import lazy_import
tz  = lazy_import.lazy_module("dateutil.tz")

to __init__.py and I was able to do this

>>> import dateutil
>>> tz = dateutil.tz
>>> tz.gettz()
tzfile('/etc/localtime')
>>> 

lazy_import is compatible with Python ≥ 2.7 or ≥ 3.4.

@pganssle
Copy link
Member Author

I'm not willing to add any new dependencies. It shouldn't be too difficult to implement, though.

@pganssle
Copy link
Member Author

Looks like lazy_import is GPL, so it's also an incompatible license. Our implementation should be a "clean room" implementation not based on theirs.

@pganssle pganssle modified the milestones: 2.8.0, Feature release Oct 25, 2018
pganssle referenced this issue in HypothesisWorks/hypothesis Jun 6, 2019
Co-authored-by: Paul Ganssle <paul@ganssle.io>
pganssle pushed a commit to orsonadams/dateutil that referenced this issue May 24, 2021
This uses PEP 562 to implement lazy loading of submodules in dateutil
(dateutilGH-771).
pganssle pushed a commit to orsonadams/dateutil that referenced this issue May 24, 2021
This uses PEP 562 to implement lazy loading of submodules in dateutil
(dateutilGH-771).
mariocj89 pushed a commit to orsonadams/dateutil that referenced this issue Jul 16, 2021
This uses PEP 562 to implement lazy loading of submodules in dateutil
(dateutilGH-771).
@mariocj89
Copy link
Member

Done via #1007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants