1515import warnings
1616
1717__version__ = "0.5rc0.dev0"
18- __all__ = ["attach" , "load " , "attach_stub " ]
18+ __all__ = ["attach" , "attach_stub " , "load " ]
1919
2020
2121threadlock = threading .Lock ()
@@ -90,13 +90,13 @@ def __getattr__(name):
9090 raise AttributeError (f"No { package_name } attribute { name } " )
9191
9292 def __dir__ ():
93- return __all__
93+ return __all__ . copy ()
9494
9595 if os .environ .get ("EAGER_IMPORT" , "" ):
9696 for attr in set (attr_to_modules .keys ()) | submodules :
9797 __getattr__ (attr )
9898
99- return __getattr__ , __dir__ , list ( __all__ )
99+ return __getattr__ , __dir__ , __all__ . copy ( )
100100
101101
102102class DelayedImportErrorModule (types .ModuleType ):
@@ -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 )
@@ -222,21 +226,19 @@ def myfunc():
222226 raise ModuleNotFoundError (not_found_message )
223227 import inspect
224228
225- try :
226- parent = inspect .stack ()[1 ]
227- frame_data = {
228- "filename" : parent .filename ,
229- "lineno" : parent .lineno ,
230- "function" : parent .function ,
231- "code_context" : parent .code_context ,
232- }
233- return DelayedImportErrorModule (
234- frame_data ,
235- "DelayedImportErrorModule" ,
236- message = not_found_message ,
237- )
238- finally :
239- del parent
229+ parent = inspect .stack ()[1 ]
230+ frame_data = {
231+ "filename" : parent .filename ,
232+ "lineno" : parent .lineno ,
233+ "function" : parent .function ,
234+ "code_context" : parent .code_context ,
235+ }
236+ del parent
237+ return DelayedImportErrorModule (
238+ frame_data ,
239+ "DelayedImportErrorModule" ,
240+ message = not_found_message ,
241+ )
240242
241243 if spec is not None :
242244 module = importlib .util .module_from_spec (spec )
0 commit comments