Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for reading .pyi files. #4824

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions sphinx/ext/autodoc/importer.py
Expand Up @@ -156,6 +156,17 @@ def import_object(modname, objpath, objtype='', attrgetter=safe_getattr, warning

try:
module = import_module(modname, warningiserror=warningiserror)
import os
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding new code to here is a bit ugly. How about moving this to helper function?

if hasattr(module, '__file__') and os.path.isfile('{}i'.format(module.__file__)):
try:
from importlib._bootstrap import spec_from_loader
from importlib._bootstrap_external import SourceFileLoader
from importlib._bootstrap import module_from_spec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these functions are provided by importlib.util package. I feel importlib._bootstrap is private and non-public interface. So this should be refactored later.
https://docs.python.org/3.6/library/importlib.html#importlib.util.spec_from_loader

spec = spec_from_loader(modname, SourceFileLoader(modname, '{}i'.format(module.__file__)))
module = module_from_spec(spec)
spec.loader.exec_module(module)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this overrides whole of modules by .pyi's. Is this right?
But .pyi file is not compatible with original .py file. Because they don't have same docstrings, imports and so on. I think this breaks documentation...

except BaseException as e:
raise e
logger.debug('[autodoc] => %r', module)
obj = module
parent = None
Expand Down