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

importing twice triggers load #56

Open
dvincentwest opened this issue Jun 25, 2024 · 3 comments
Open

importing twice triggers load #56

dvincentwest opened this issue Jun 25, 2024 · 3 comments
Labels

Comments

@dvincentwest
Copy link

Problem

When importing a module setup with apipkg, successive imports, even if in different files, trigger a full load. I thought it would only load when actually used. I have some anecdotal observation that the behavior is intermittent, but when I put together this test code, it reproduces every time

How to Reproduce

system info:

MacOS 14.5
Python 3.12.4

Consider the following filetree

mypkg/
  - module.py
  - lazy.py

with contents:

# lazy.py
import apipkg
apipkg.initpkg(__name__,{
        "module": "mypkg.module",
    })
# module.py
print("module loaded")

then in a terminal:

> python
>>> import mypkg.lazy.module
>>> import mypkg.lazy.module
module loaded
@RonnyPfannschmidt
Copy link
Member

i believe this relates to import looking up the attributes as requested, based on the structure of the import request i'd consider it the bug that it was not loaded the first time

the request in import was for mypkg.lazy.module - at that point it needs to resolve

i suspect theres a shortcut in python, but the actual bug is not loading it the first time

never ever request by name something thats nested in a lazy module if you dont want it included

@dvincentwest
Copy link
Author

I see. Perhaps I misunderstood the operation then. This means that only the top-level lazy module specification is meant to lie dormant (i.e. lazy) and any access below the top level is meant to trigger a full load

@RonnyPfannschmidt
Copy link
Member

Accesses do not trigger full load, but import has to validate the existence of the full imported name

So lazy would only ensure the existence of the lazy module

However lazy.foo needs to raise a import error if foo doesn't exist

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

No branches or pull requests

2 participants