Why?
Should lazy import replace majority of import statements?
Or is the aim to provide the tool for few slow-to-load libraries?
If the latter, then this is not an issue at all.
If the former, then this implementation is hardly suitable for it - if used extensively, it is a ticking time bomb:
lazy from thing import thing
lazy from os.path import splitext, aabspath, future_error_here, and_here, and_here_as_well
# so far so good...
stem, _ = splitext('file.py')
# still good...
# Made use of one of the objects from `from` import, but it didn't expose any errors for its neighbours.
Without static type checking this is unsustainable.
And static type checking is optional (e.g. stdlib doesn’t use it).
Yes, this analogy is undeniably convenient.
But in contrast, I think, the consistency of behaviour of:
try:
lazy import non_existent_package
except ModuleNotFoundError:
...
is much more important place to put emphasis on.
Yes, it can. To be more precise, from what I have observed - 100 - 400 µs.
And this is quite fast.
Import load times of slow-to-load libraries, where load times become a bottleneck are in much much higher order. E.g.:
full_stdlib - 170 ms
numpy - 100 ms
scipy - 14 ms
scipy-stats - 500 ms
pandas - 200 ms
There might be opportunities to optimize - but I wouldn’t expect anything major for low-mid effort attempt. (There might be surprises on the high effort end, but wouldn’t count on it either.)
From my experience, big portion of slow-to-load packages are slow to load because of low quality code, bloats and unnecessary dependencies.
Of course there is a portion of high quality stuff that is slow to load, but in relation to fast-to-load stuff the ratio is small.
If there is a place to apply Python’s Zen, it is this:
Explicit is better than implicit.
...
Simple is better than complex.
...
Errors should never pass silently.
Unless explicitly silenced.
Some decisions are being made based on only one of these, where the validity is arguable at best.
While the way I see it, this proposal violates the above to very high degree in their truest sense.
If community can not live without from module import attribute, then so be it, but I can not tress the price being paid for this single feature enough. (my github crashed on PR’s diff…).
Otherwise, most/all of other benefits of this (such as filtering, dedicated syntax, …), can just as well be applied to much more explicit approach of eagerly finding spec, that has many other non-trivial benefits in several dimensions.