Skip to content

Commit 2dc3d9d

Browse files
authored
feat: add fallback to support namespace packages
1 parent bac553d commit 2dc3d9d

File tree

1 file changed

+44
-0
lines changed
  • src/mkdocstrings_handlers/python/_internal

1 file changed

+44
-0
lines changed

src/mkdocstrings_handlers/python/_internal/handler.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def collect(self, identifier: str, options: PythonOptions) -> CollectorItem:
263263
_logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")
264264
_logger.debug(f"Unresolved aliases: {', '.join(sorted(unresolved))}")
265265

266+
if identifier not in self._modules_collection:
267+
self.try_to_import(identifier, loader)
268+
266269
try:
267270
doc_object = self._modules_collection[identifier]
268271
except KeyError as error:
@@ -278,6 +281,47 @@ def collect(self, identifier: str, options: PythonOptions) -> CollectorItem:
278281

279282
return doc_object
280283

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+
281325
def render(self, data: CollectorItem, options: PythonOptions) -> str:
282326
"""Render the collected data.
283327

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy