[Import-SIG] Proposal for a lazy-loading finder

Brett Cannon brett at python.org
Mon Jul 10 14:48:22 EDT 2017


On Sun, 9 Jul 2017 at 06:59 Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 9 July 2017 at 20:03, Antoine Pitrou <solipsis at pitrou.net> wrote:
> > I would suggest allow people to fully customize the blacklist /
> > whitelist logic using a callable (because looking up by fullname is a
> > bit inflexible).
>
> Oh, I like that - and then we'd have "ensure_lazy" and "ensure_eager"
> as callback factories that accepted a predefined list of names.
>

If we provide an optional callable argument then I would drop the
whitelist/ensure_lazy option. It's easier to explain and the common case
will be blacklisting a module for lazy loading if you're implicitly
flipping it on. For the whitelist case we can add
importlib.util.lazy_import() and people can just be explicit (if this is
important enough to even care about).

And I purposefully didn't do a prefix match for ensure_eager as it's only
meant for specific modules that have some try/except block which fails in
the face of lazy loading. And since that should be a per-module thing
instead of a per-package thing I don't want it over-extending. Plus
providing a callback solution lets people engineer their own prefix
matching solution if that's what they need.


>
> I think this is going to be one of the key changes in going from
> "utility a project implements for itself if it needs it" to "general
> purpose language level facility" - I could easily see the simple
> predefined list approach working for a *particular* project, but I
> think once we open this capability up to all Python users (rather than
> only those that are comfortable with customising the import system at
> runtime) it will prove to be inadequate.
>

Probably, which is partially why I have put off proposing the idea of
importlib.util.activate_lazy_loading() for so long (and yes, Antoine, that
function and LazyLoadingFinder is all that's needed :) . But I met multiple
people at PyCon US this year who thanked me for the lazy loader which
suggests the note warning people about not using the lazy loader is being
ignore, so I figured it was finally time to help make sure people at least
do it correctly.

-Brett


>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Import-SIG mailing list
> Import-SIG at python.org
> https://mail.python.org/mailman/listinfo/import-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/import-sig/attachments/20170710/6d47efa5/attachment.html>


More information about the Import-SIG mailing list