From de9cf4d6417c792b3bd91eae3530a6fda4c4d82f Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Sat, 15 Mar 2025 22:19:55 -0400 Subject: [PATCH 1/2] Add `suppress_warning` parameter to the `load` function --- lazy_loader/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lazy_loader/__init__.py b/lazy_loader/__init__.py index 9774159..a23a66b 100644 --- a/lazy_loader/__init__.py +++ b/lazy_loader/__init__.py @@ -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:: @@ -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 From 359774f460396c48763f06c64615d3fdbda2f458 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Sat, 15 Mar 2025 22:31:27 -0400 Subject: [PATCH 2/2] Update __init__.py --- lazy_loader/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lazy_loader/__init__.py b/lazy_loader/__init__.py index a23a66b..f356d21 100644 --- a/lazy_loader/__init__.py +++ b/lazy_loader/__init__.py @@ -193,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)