|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import sys |
3 | 4 | from typing import TYPE_CHECKING, NewType, TypeVar |
4 | 5 |
|
5 | 6 | from docutils.statemachine import StringList |
|
27 | 28 |
|
28 | 29 | if TYPE_CHECKING: |
29 | 30 | from collections.abc import Callable, Iterator, Sequence |
30 | | - from types import ModuleType |
31 | 31 | from typing import Any, ClassVar, Final, Literal, NoReturn |
32 | 32 |
|
33 | 33 | from sphinx.config import Config |
@@ -97,7 +97,6 @@ def __init__( |
97 | 97 | self.get_attr = directive.get_attr |
98 | 98 | self.name = name |
99 | 99 | self.indent: Final = indent |
100 | | - self.module: ModuleType | None = None |
101 | 100 | # the parent/owner of the object to document |
102 | 101 | self.parent: Any = None |
103 | 102 | # the module analyzer to get at attribute docs, or None |
@@ -144,10 +143,9 @@ def _load_object_by_name(self) -> Literal[True] | None: |
144 | 143 | ) |
145 | 144 | if ret is None: |
146 | 145 | return None |
147 | | - props, module, parent = ret |
| 146 | + props, parent = ret |
148 | 147 |
|
149 | 148 | self.props = props |
150 | | - self.module = module |
151 | 149 | self.parent = parent |
152 | 150 | self._load_object_has_been_called = True |
153 | 151 | return True |
@@ -407,9 +405,19 @@ def _generate( |
407 | 405 | logger.debug('[autodoc] module analyzer failed: %s', exc) |
408 | 406 | # no source file -- e.g. for builtin and C modules |
409 | 407 | self.analyzer = None |
410 | | - # at least add the module.__file__ as a dependency |
411 | | - if module___file__ := getattr(self.module, '__file__', ''): |
412 | | - self.directive.record_dependencies.add(module___file__) |
| 408 | + # at least add the module source file as a dependency |
| 409 | + if self.props.module_name: |
| 410 | + try: |
| 411 | + module_spec = sys.modules[self.props.module_name].__spec__ |
| 412 | + except (AttributeError, KeyError): |
| 413 | + pass |
| 414 | + else: |
| 415 | + if ( |
| 416 | + module_spec is not None |
| 417 | + and module_spec.has_location |
| 418 | + and module_spec.origin |
| 419 | + ): |
| 420 | + self.directive.record_dependencies.add(module_spec.origin) |
413 | 421 | else: |
414 | 422 | self.directive.record_dependencies.add(self.analyzer.srcname) |
415 | 423 |
|
|
0 commit comments