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

Provide importlib.util.lazy_import helper function #76373

Closed
ncoghlan opened this issue Dec 1, 2017 · 6 comments
Closed

Provide importlib.util.lazy_import helper function #76373

ncoghlan opened this issue Dec 1, 2017 · 6 comments
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Dec 1, 2017

BPO 32192
Nosy @warsaw, @brettcannon, @ncoghlan, @ericsnowcurrently, @nanjekyejoannah, @iritkatriel
PRs
  • bpo-32192: A basic lazy importer example #21330
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-10-05.18:34:50.201>
    created_at = <Date 2017-12-01.08:50:41.820>
    labels = ['3.8', 'type-feature', '3.7', 'docs']
    title = 'Provide importlib.util.lazy_import helper function'
    updated_at = <Date 2020-10-05.18:34:50.200>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2020-10-05.18:34:50.200>
    actor = 'brett.cannon'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2020-10-05.18:34:50.201>
    closer = 'brett.cannon'
    components = ['Documentation']
    creation = <Date 2017-12-01.08:50:41.820>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32192
    keywords = ['patch']
    message_count = 6.0
    messages = ['307370', '307396', '307481', '373620', '377893', '378073']
    nosy_count = 7.0
    nosy_names = ['barry', 'brett.cannon', 'ncoghlan', 'docs@python', 'eric.snow', 'nanjekyejoannah', 'iritkatriel']
    pr_nums = ['21330']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32192'
    versions = ['Python 3.7', 'Python 3.8']

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Dec 1, 2017

    While importlib provides all the *pieces* to implement lazy imports, we don't actually provide a clear way of chaining them together into a lazy import operation. Without any error checking, that looks like:

        import sys
        import importlib.util
        def lazy_import(name):
            spec = importlib.util.find_spec(name)
            loader = importlib.util.LazyLoader(spec.loader)
            spec.loader = loader
            module = importlib.util.module_from_spec(spec)
            sys.modules[name] = module
            loader.exec_module(module)
            return module
        >>> lazy_typing = lazy_import("typing")
        >>> lazy_typing.TYPE_CHECKING
        False

    I'm thinking it may make sense to just provide a robust implementation of that, and accept that it may lead to some bug reports that are closed with "You need to fix the module you're loading to be compatible with lazy imports"

    @ncoghlan ncoghlan added 3.7 (EOL) end of life 3.8 (EOL) end of life type-feature A feature request or enhancement labels Dec 1, 2017
    @brettcannon
    Copy link
    Member

    Already planning this as a PyPI package (at least to start). See https://notebooks.azure.com/Brett/libraries/di2Btqj7zSI/html/Lazy%20importing.ipynb for the design.

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Dec 3, 2017

    Maintaining the actual implementation as a third party module seems like a good idea to me, so I'm marking this as a documentation issue instead.

    The idea would be to add this as an example of a very basic lazy importer under https://docs.python.org/3/library/importlib.html#examples

    @ncoghlan ncoghlan added the docs Documentation in the Doc dir label Dec 3, 2017
    @nanjekyejoannah
    Copy link
    Contributor

    New changeset 8dd32fe by Joannah Nanjekye in branch 'master':
    bpo-32192: A basic lazy importer example (GH-21330)
    8dd32fe

    @iritkatriel
    Copy link
    Member

    This seems complete. Can is be closed?

    @brettcannon
    Copy link
    Member

    Yep, I think the example is enough to close this. Thanks!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants