in python/scripts/generate_type_stubs_and_docs.py [0:0]
def patch_sphinx_to_read_pyi():
"""
Sphinx doesn't know how to read .pyi files, but we use .pyi files as our
"source of truth" for the public API that we expose to IDEs and our documentation.
This patch tells Sphinx how to read .pyi files, using them to replace their .py
counterparts.
"""
old_import_module = sphinx.ext.autodoc.importer.import_module
def patch_import_module(modname: str, *args, **kwargs) -> typing.Any:
if modname in sys.modules:
return sys.modules[modname]
try:
return import_stub(".", modname)
except ImportError:
return old_import_module(modname, *args, **kwargs)
except Exception as e:
print(f"Failed to import stub module: {e}")
traceback.print_exc()
raise
sphinx.ext.autodoc.importer.import_module = patch_import_module