Skip to content

Commit

Permalink
feat: Add warning for missing import
Browse files Browse the repository at this point in the history
Closes #419
  • Loading branch information
arwedus committed Apr 4, 2024
1 parent 3b037c7 commit 6c1177b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions autoapi/documenters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

import sphinx.util.logging

from sphinx.ext import autodoc

from ._objects import (
Expand All @@ -13,6 +15,9 @@
)


LOGGER = sphinx.util.logging.getLogger(__name__)


class AutoapiDocumenter(autodoc.Documenter):
def get_attr(self, obj, name, *defargs):
attrgetters = self.env.app.registry.autodoc_attrgettrs
Expand All @@ -33,11 +38,18 @@ def get_attr(self, obj, name, *defargs):

raise AttributeError(name)

def import_object(self):
def import_object(self) -> bool:
"""Imports and sets the object to be documented.
The object is searched in the autoapi_objects dict based on the fullname attribute of the documenter.
Returns:
bool: True if the object was successfully imported and set, False otherwise.
"""
max_splits = self.fullname.count(".")
objects = self.env.autoapi_objects
for num_splits in range(max_splits, -1, -1):
path_stack = list(reversed(self.fullname.rsplit(".", num_splits)))
objects = self.env.autoapi_objects
parent = None
current = objects.get(path_stack.pop())
while current and path_stack:
Expand All @@ -50,6 +62,9 @@ def import_object(self):
self._method_parent = parent
return True

# If we get here, the object was not found. Emit a warning as autodoc does.
LOGGER.warning("Failed to import %s '%s' [autoapi.import]", self.directivetype, self.fullname, type="autoapi", subtype="import")
self.env.note_reread()
return False

def get_real_modname(self):
Expand Down

0 comments on commit 6c1177b

Please sign in to comment.