Skip to content

Add suppress_warning parameter to the load function #146

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

Merged
merged 2 commits into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

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