Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lazy_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __getattr__(self, x):
)


def load(fullname, *, require=None, error_on_import=False):
def load(fullname, *, require=None, error_on_import=False, suppress_warning=False):
"""Return a lazily imported proxy for a module.

We often see the following pattern::
Expand Down Expand Up @@ -174,6 +174,10 @@ def myfunc():
Whether to postpone raising import errors until the module is accessed.
If set to `True`, import errors are raised as soon as `load` is called.

suppress_warning : bool
Whether to prevent emitting a warning when loading subpackages.
If set to `True`, no warning will occur.

Returns
-------
pm : importlib.util._LazyModule
Expand All @@ -189,10 +193,10 @@ def myfunc():
if have_module and require is None:
return module

if "." in fullname:
if not suppress_warning and "." in fullname:
msg = (
"subpackages can technically be lazily loaded, but it causes the "
"package to be eagerly loaded even if it is already lazily loaded."
"package to be eagerly loaded even if it is already lazily loaded. "
"So, you probably shouldn't use subpackages with this lazy feature."
)
warnings.warn(msg, RuntimeWarning)
Expand Down
Loading