Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6984 from tk0miya/2755_type_comment_support
Close #2755: autodoc: Support type_comment style annotation
- Loading branch information
Showing
8 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
sphinx.ext.autodoc.type_comment | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Update annotations info of living objects using type_comments. | ||
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS. | ||
:license: BSD, see LICENSE for details. | ||
""" | ||
|
||
import ast | ||
from inspect import getsource | ||
from typing import Any, Dict | ||
from typing import cast | ||
|
||
import sphinx | ||
from sphinx.application import Sphinx | ||
from sphinx.pycode.ast import parse as ast_parse | ||
from sphinx.pycode.ast import unparse as ast_unparse | ||
from sphinx.util import inspect | ||
from sphinx.util import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_type_comment(obj: Any) -> ast.FunctionDef: | ||
"""Get type_comment'ed FunctionDef object from living object. | ||
This tries to parse original code for living object and returns | ||
AST node for given *obj*. It requires py38+ or typed_ast module. | ||
""" | ||
try: | ||
source = getsource(obj) | ||
if source.startswith((' ', r'\t')): | ||
# subject is placed inside class or block. To read its docstring, | ||
# this adds if-block before the declaration. | ||
module = ast_parse('if True:\n' + source) | ||
subject = cast(ast.FunctionDef, module.body[0].body[0]) # type: ignore | ||
else: | ||
module = ast_parse(source) | ||
subject = cast(ast.FunctionDef, module.body[0]) # type: ignore | ||
|
||
if getattr(subject, "type_comment", None): | ||
return ast_parse(subject.type_comment, mode='func_type') # type: ignore | ||
else: | ||
return None | ||
except (OSError, TypeError): # failed to load source code | ||
return None | ||
except SyntaxError: # failed to parse type_comments | ||
return None | ||
|
||
|
||
def update_annotations_using_type_comments(app: Sphinx, obj: Any, bound_method: bool) -> None: | ||
"""Update annotations info of *obj* using type_comments.""" | ||
try: | ||
function = get_type_comment(obj) | ||
if function and hasattr(function, 'argtypes'): | ||
if function.argtypes != [ast.Ellipsis]: # type: ignore | ||
sig = inspect.signature(obj, bound_method) | ||
for i, param in enumerate(sig.parameters.values()): | ||
if param.name not in obj.__annotations__: | ||
annotation = ast_unparse(function.argtypes[i]) # type: ignore | ||
obj.__annotations__[param.name] = annotation | ||
|
||
if 'return' not in obj.__annotations__: | ||
obj.__annotations__['return'] = ast_unparse(function.returns) # type: ignore | ||
except NotImplementedError as exc: # failed to ast.unparse() | ||
logger.warning("Failed to parse type_comment for %r: %s", obj, exc) | ||
|
||
|
||
def setup(app: Sphinx) -> Dict[str, Any]: | ||
app.connect('autodoc-before-process-signature', update_annotations_using_type_comments) | ||
|
||
return {'version': sphinx.__display_version__, 'parallel_read_safe': True} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
sphinx.pycode.ast | ||
~~~~~~~~~~~~~~~~~ | ||
Helpers for AST (Abstract Syntax Tree). | ||
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS. | ||
:license: BSD, see LICENSE for details. | ||
""" | ||
|
||
import sys | ||
|
||
if sys.version_info > (3, 8): | ||
import ast | ||
else: | ||
try: | ||
# use typed_ast module if installed | ||
from typed_ast import ast3 as ast | ||
except ImportError: | ||
import ast # type: ignore | ||
|
||
|
||
def parse(code: str, mode: str = 'exec') -> "ast.AST": | ||
"""Parse the *code* using built-in ast or typed_ast. | ||
This enables "type_comments" feature if possible. | ||
""" | ||
try: | ||
# type_comments parameter is available on py38+ | ||
return ast.parse(code, mode=mode, type_comments=True) # type: ignore | ||
except TypeError: | ||
# fallback to ast module. | ||
# typed_ast is used to parse type_comments if installed. | ||
return ast.parse(code, mode=mode) | ||
|
||
|
||
def unparse(node: ast.AST) -> str: | ||
"""Unparse an AST to string.""" | ||
if node is None: | ||
return None | ||
elif isinstance(node, ast.Attribute): | ||
return "%s.%s" % (unparse(node.value), node.attr) | ||
elif isinstance(node, ast.Bytes): | ||
return repr(node.s) | ||
elif isinstance(node, ast.Call): | ||
args = ([unparse(e) for e in node.args] + | ||
["%s=%s" % (k.arg, unparse(k.value)) for k in node.keywords]) | ||
return "%s(%s)" % (unparse(node.func), ", ".join(args)) | ||
elif isinstance(node, ast.Dict): | ||
keys = (unparse(k) for k in node.keys) | ||
values = (unparse(v) for v in node.values) | ||
items = (k + ": " + v for k, v in zip(keys, values)) | ||
return "{" + ", ".join(items) + "}" | ||
elif isinstance(node, ast.Ellipsis): | ||
return "..." | ||
elif isinstance(node, ast.Index): | ||
return unparse(node.value) | ||
elif isinstance(node, ast.Lambda): | ||
return "<function <lambda>>" # TODO | ||
elif isinstance(node, ast.List): | ||
return "[" + ", ".join(unparse(e) for e in node.elts) + "]" | ||
elif isinstance(node, ast.Name): | ||
return node.id | ||
elif isinstance(node, ast.NameConstant): | ||
return repr(node.value) | ||
elif isinstance(node, ast.Num): | ||
return repr(node.n) | ||
elif isinstance(node, ast.Set): | ||
return "{" + ", ".join(unparse(e) for e in node.elts) + "}" | ||
elif isinstance(node, ast.Str): | ||
return repr(node.s) | ||
elif isinstance(node, ast.Subscript): | ||
return "%s[%s]" % (unparse(node.value), unparse(node.slice)) | ||
elif isinstance(node, ast.Tuple): | ||
return ", ".join(unparse(e) for e in node.elts) | ||
elif sys.version_info > (3, 6) and isinstance(node, ast.Constant): | ||
# this branch should be placed at last | ||
return repr(node.value) | ||
else: | ||
raise NotImplementedError('Unable to parse %s object' % type(node).__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
test_pycode_ast | ||
~~~~~~~~~~~~~~~ | ||
Test pycode.ast | ||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS. | ||
:license: BSD, see LICENSE for details. | ||
""" | ||
|
||
import pytest | ||
|
||
from sphinx.pycode import ast | ||
|
||
|
||
@pytest.mark.parametrize('source,expected', [ | ||
("os.path", "os.path"), # Attribute | ||
("b'bytes'", "b'bytes'"), # Bytes | ||
("object()", "object()"), # Call | ||
("1234", "1234"), # Constant | ||
("{'key1': 'value1', 'key2': 'value2'}", | ||
"{'key1': 'value1', 'key2': 'value2'}"), # Dict | ||
("...", "..."), # Ellipsis | ||
("Tuple[int, int]", "Tuple[int, int]"), # Index, Subscript | ||
("lambda x, y: x + y", | ||
"<function <lambda>>"), # Lambda | ||
("[1, 2, 3]", "[1, 2, 3]"), # List | ||
("sys", "sys"), # Name, NameConstant | ||
("1234", "1234"), # Num | ||
("{1, 2, 3}", "{1, 2, 3}"), # Set | ||
("'str'", "'str'"), # Str | ||
("(1, 2, 3)", "1, 2, 3"), # Tuple | ||
]) | ||
def test_unparse(source, expected): | ||
module = ast.parse(source) | ||
assert ast.unparse(module.body[0].value) == expected | ||
|
||
|
||
def test_unparse_None(): | ||
assert ast.unparse(None) is None |