-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 import #63073
Lazy import #63073
Conversation
ec4ba9e
to
8d7b246
Compare
(last push was a no-change rebase onto current master) |
@whytewolf said in #62932 (comment):
I'll start a SEP for this. However, I want to note that the above characterization of this PR is not accurate. The new import salt.utils.lazy
_baz = salt.utils.lazy.lazy_import("foo.bar.baz") behaves the same as: import foo.bar.baz as _baz except:
The new
|
The `salt.loader` module is intentially imported late in `salt.config.call_id_function()` to avoid a circular import error. However, `salt.loader` is also used in `salt.config.mminion_config()`, so import it there as well. This avoids an error if `mminion_config()` is called before `call_id_function()`, or if the import statement in `call_id_function()` is removed in the future.
The delayed `import` statements match `import` statements at the top of the file so they are effectively no-ops.
For rationale, see the documentation for the new function. Future commits will use this to replace existing delayed `import` statements, and to defer some more module loads to avoid circular imports.
This makes it possible to import these affected modules from core code without introducing a circular import.
Closing in favor of the more limited PR #63335. |
What does this PR do?
lazy_import
function that acts like Python'simport
statement but defers module load until a module attribute is read or deleted. This function is unrelated to and independent of (not a wrapper around) thesalt.loader.*
stuff.import
statements (import
statements inside a function that are evaluated when the function is called) to lazy imports.The new
lazy_import
function is a copy of https://docs.python.org/3/library/importlib.html#implementing-lazy-imports except:parent.child
, thechild
attribute of theparent
module is set to theparent.child
module object (like theimport
statement does).What issues does this PR fix or reference?
See #62932 (comment) for context.
Behavior Changes
No behavior changes are expected. The new
salt.utils.lazy.lazy_import
function is meant to be a drop-in replacement of the Pythonimport
statement. In other words, this:behaves the same as:
except:
foo/__init__.py
,foo/bar/__init__.py
, andfoo/bar/baz.py
—and the execution of their contents—does not happen until an attribute of_baz
is read or deleted (assuming the modules are not already loaded).Merge requirements satisfied?
Commits signed with GPG?
No