Skip to content

Commit 513f429

Browse files
authored
Merge pull request #146 from ofek/patch-1
Add `suppress_warning` parameter to the `load` function
2 parents 2233232 + 359774f commit 513f429

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lazy_loader/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __getattr__(self, x):
118118
)
119119

120120

121-
def load(fullname, *, require=None, error_on_import=False):
121+
def load(fullname, *, require=None, error_on_import=False, suppress_warning=False):
122122
"""Return a lazily imported proxy for a module.
123123
124124
We often see the following pattern::
@@ -174,6 +174,10 @@ def myfunc():
174174
Whether to postpone raising import errors until the module is accessed.
175175
If set to `True`, import errors are raised as soon as `load` is called.
176176
177+
suppress_warning : bool
178+
Whether to prevent emitting a warning when loading subpackages.
179+
If set to `True`, no warning will occur.
180+
177181
Returns
178182
-------
179183
pm : importlib.util._LazyModule
@@ -189,10 +193,10 @@ def myfunc():
189193
if have_module and require is None:
190194
return module
191195

192-
if "." in fullname:
196+
if not suppress_warning and "." in fullname:
193197
msg = (
194198
"subpackages can technically be lazily loaded, but it causes the "
195-
"package to be eagerly loaded even if it is already lazily loaded."
199+
"package to be eagerly loaded even if it is already lazily loaded. "
196200
"So, you probably shouldn't use subpackages with this lazy feature."
197201
)
198202
warnings.warn(msg, RuntimeWarning)

0 commit comments

Comments
 (0)