@@ -263,6 +263,9 @@ def collect(self, identifier: str, options: PythonOptions) -> CollectorItem:
263
263
_logger .debug (f"{ len (unresolved )} aliases were still unresolved after { iterations } iterations" )
264
264
_logger .debug (f"Unresolved aliases: { ', ' .join (sorted (unresolved ))} " )
265
265
266
+ if identifier not in self ._modules_collection :
267
+ self .try_to_import (identifier , loader )
268
+
266
269
try :
267
270
doc_object = self ._modules_collection [identifier ]
268
271
except KeyError as error :
@@ -278,6 +281,47 @@ def collect(self, identifier: str, options: PythonOptions) -> CollectorItem:
278
281
279
282
return doc_object
280
283
284
+
285
+ def try_to_import (self , identifier : str , loader ):
286
+ import importlib .util
287
+ from pathlib import Path
288
+
289
+
290
+ parts = ()
291
+ doc_object = None
292
+ for part in identifier .split ("." ):
293
+ parts = parts + (part ,)
294
+ sub_identifier = "." .join (parts )
295
+ spec = importlib .util .find_spec ("." .join (parts ))
296
+ if spec is None :
297
+ if doc_object is not None and part in doc_object .members :
298
+ # If we already have a doc_object, we can just use it
299
+ doc_object = doc_object .get_member (part )
300
+ continue
301
+ raise CollectionError (f"Module { identifier } could not be found" )
302
+ origin = spec .origin
303
+ if origin is None :
304
+ # Pick the first loc that is accessible from here (ie is a children of cwd)
305
+ origin = next (
306
+ (
307
+ loc
308
+ for loc in spec .submodule_search_locations
309
+ if loc .startswith (os .getcwd ())
310
+ ),
311
+ None ,
312
+ )
313
+
314
+ doc_object = loader ._load_module (
315
+ part ,
316
+ Path (origin ),
317
+ parent = doc_object ,
318
+ )
319
+ try :
320
+ self ._modules_collection .set_member (sub_identifier , doc_object )
321
+ except :
322
+ pass
323
+ return doc_object
324
+
281
325
def render (self , data : CollectorItem , options : PythonOptions ) -> str :
282
326
"""Render the collected data.
283
327
0 commit comments