Skip to content

Commit 6c1177b

Browse files
committed
feat: Add warning for missing import
Closes readthedocs#419
1 parent 3b037c7 commit 6c1177b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

autoapi/documenters.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import re
22

3+
import sphinx.util.logging
4+
35
from sphinx.ext import autodoc
46

57
from ._objects import (
@@ -13,6 +15,9 @@
1315
)
1416

1517

18+
LOGGER = sphinx.util.logging.getLogger(__name__)
19+
20+
1621
class AutoapiDocumenter(autodoc.Documenter):
1722
def get_attr(self, obj, name, *defargs):
1823
attrgetters = self.env.app.registry.autodoc_attrgettrs
@@ -33,11 +38,18 @@ def get_attr(self, obj, name, *defargs):
3338

3439
raise AttributeError(name)
3540

36-
def import_object(self):
41+
def import_object(self) -> bool:
42+
"""Imports and sets the object to be documented.
43+
44+
The object is searched in the autoapi_objects dict based on the fullname attribute of the documenter.
45+
46+
Returns:
47+
bool: True if the object was successfully imported and set, False otherwise.
48+
"""
3749
max_splits = self.fullname.count(".")
50+
objects = self.env.autoapi_objects
3851
for num_splits in range(max_splits, -1, -1):
3952
path_stack = list(reversed(self.fullname.rsplit(".", num_splits)))
40-
objects = self.env.autoapi_objects
4153
parent = None
4254
current = objects.get(path_stack.pop())
4355
while current and path_stack:
@@ -50,6 +62,9 @@ def import_object(self):
5062
self._method_parent = parent
5163
return True
5264

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

5570
def get_real_modname(self):

0 commit comments

Comments
 (0)