Is your feature request related to a problem? Please describe.
Yes. Following the release of Sphinx 9.1.0, autodoc attempts to use neighboring .pyi type stubs via _StubFileLoader instead of importing native binary modules (like Cython extensions). As discussed in #13415, this breaks docstring extraction for compiled cdef class objects, triggering silent AttributeError warnings during safe_getattr lookups.
While PR #13446 successfully implemented the SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBS environment variable to bypass this issue, the variable is completely undocumented. Upgrading users face completely blank or broken API reference pages with no guidance in the documentation on how to restore native runtime execution.
Describe the solution you'd like
Please explicitly document the SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBS environment variable within the Autodoc configuration or invocation guides so compiled extension authors know how to safely opt out.
Describe alternatives you've considered
We attempted various conf.py monkeypatches and to override safe_getattr or type-checking routines.
Finally through overriding Documenter.get_attr and inspecting a module's __spec__ and __file__ attributes was it apparent that the modules were being loaded by sphinx.ext.autodoc._dynamic._importer._StubFileLoader, leading to a trail of breadcrumbs which finally lead to the discovery of:
|
def _import_module(modname: str, try_reload: bool = False) -> Any: |
|
if modname in sys.modules: |
|
return sys.modules[modname] |
|
|
|
skip_pyi = bool(os.getenv('SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBS', '')) |
|
original_module_names = frozenset(sys.modules) |
|
try: |
|
spec = find_spec(modname) |
|
if spec is None: |
|
msg = f'No module named {modname!r}' |
|
raise ModuleNotFoundError(msg, name=modname) # NoQA: TRY301 |
|
spec, pyi_path = _find_type_stub_spec(spec, modname) |
|
if skip_pyi or pyi_path is None: |
|
module = importlib.import_module(modname) |
The environment variable added in #13446 is the correct and intended fix; it simply needs to be discoverable in the official documentation.
Additional context
The real-world impact of this missing documentation is captured across consecutive Read the Docs builds in this PR: cyndilib/cyndilib#114
- First Build (Broken): Upgrading to
Sphinx >= 9.1 completely wiped out the API reference pages due to silent stub resolution failures.
- Second Build (Fixed): Manually passing
SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBS=1 instantly restored full binary docstring parsing.
Is your feature request related to a problem? Please describe.
Yes. Following the release of Sphinx 9.1.0,
autodocattempts to use neighboring.pyitype stubs via_StubFileLoaderinstead of importing native binary modules (like Cython extensions). As discussed in #13415, this breaks docstring extraction for compiledcdef classobjects, triggering silentAttributeErrorwarnings duringsafe_getattrlookups.While PR #13446 successfully implemented the
SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBSenvironment variable to bypass this issue, the variable is completely undocumented. Upgrading users face completely blank or broken API reference pages with no guidance in the documentation on how to restore native runtime execution.Describe the solution you'd like
Please explicitly document the
SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBSenvironment variable within the Autodoc configuration or invocation guides so compiled extension authors know how to safely opt out.Describe alternatives you've considered
We attempted various
conf.pymonkeypatches and to overridesafe_getattror type-checking routines.Finally through overriding
Documenter.get_attrand inspecting a module's__spec__and__file__attributes was it apparent that the modules were being loaded bysphinx.ext.autodoc._dynamic._importer._StubFileLoader, leading to a trail of breadcrumbs which finally lead to the discovery of:sphinx/sphinx/ext/autodoc/_dynamic/_importer.py
Lines 208 to 221 in c1b618c
The environment variable added in #13446 is the correct and intended fix; it simply needs to be discoverable in the official documentation.
Additional context
The real-world impact of this missing documentation is captured across consecutive Read the Docs builds in this PR: cyndilib/cyndilib#114
Sphinx >= 9.1completely wiped out the API reference pages due to silent stub resolution failures.SPHINX_AUTODOC_IGNORE_NATIVE_MODULE_TYPE_STUBS=1instantly restored full binary docstring parsing.