From 6c1177b068a9b5374e550747f17f41abb7911ff9 Mon Sep 17 00:00:00 2001 From: Arwed Starke <70707249+arwedus@users.noreply.github.com> Date: Thu, 4 Apr 2024 12:58:25 +0200 Subject: [PATCH] feat: Add warning for missing import Closes #419 --- autoapi/documenters.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/autoapi/documenters.py b/autoapi/documenters.py index 44c06876..cb41ff03 100644 --- a/autoapi/documenters.py +++ b/autoapi/documenters.py @@ -1,5 +1,7 @@ import re +import sphinx.util.logging + from sphinx.ext import autodoc from ._objects import ( @@ -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 @@ -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: @@ -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):