mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Use `ast.parse
` from the standard library
This commit is contained in:
parent
eb6e137097
commit
b277abcb49
@ -21,7 +21,6 @@ from sphinx.directives import ObjectDescription
|
||||
from sphinx.domains import Domain, Index, IndexEntry, ObjType
|
||||
from sphinx.environment import BuildEnvironment
|
||||
from sphinx.locale import _, __
|
||||
from sphinx.pycode.ast import parse as ast_parse
|
||||
from sphinx.roles import XRefRole
|
||||
from sphinx.util import logging
|
||||
from sphinx.util.docfields import Field, GroupedField, TypedField
|
||||
@ -206,7 +205,7 @@ def _parse_annotation(annotation: str, env: BuildEnvironment) -> List[Node]:
|
||||
raise SyntaxError # unsupported syntax
|
||||
|
||||
try:
|
||||
tree = ast_parse(annotation)
|
||||
tree = ast.parse(annotation, type_comments=True)
|
||||
result: List[Node] = []
|
||||
for node in unparse(tree):
|
||||
if isinstance(node, nodes.literal):
|
||||
|
@ -11,7 +11,6 @@ from typing import Any, Dict, List, Optional
|
||||
import sphinx
|
||||
from sphinx.application import Sphinx
|
||||
from sphinx.locale import __
|
||||
from sphinx.pycode.ast import parse as ast_parse
|
||||
from sphinx.pycode.ast import unparse as ast_unparse
|
||||
from sphinx.util import logging
|
||||
|
||||
@ -36,10 +35,10 @@ def get_function_def(obj: Any) -> Optional[ast.FunctionDef]:
|
||||
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)
|
||||
module = ast.parse('if True:\n' + source)
|
||||
return module.body[0].body[0] # type: ignore
|
||||
else:
|
||||
module = ast_parse(source)
|
||||
module = ast.parse(source)
|
||||
return module.body[0] # type: ignore
|
||||
except (OSError, TypeError): # failed to load source code
|
||||
return None
|
||||
|
@ -7,7 +7,6 @@ from typing import Any, Dict, List, cast
|
||||
import sphinx
|
||||
from sphinx.application import Sphinx
|
||||
from sphinx.locale import __
|
||||
from sphinx.pycode.ast import parse as ast_parse
|
||||
from sphinx.pycode.ast import unparse as ast_unparse
|
||||
from sphinx.util import inspect, logging
|
||||
|
||||
@ -86,14 +85,14 @@ def get_type_comment(obj: Any, bound_method: bool = False) -> Signature:
|
||||
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)
|
||||
module = ast.parse('if True:\n' + source, type_comments=True)
|
||||
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
|
||||
module = ast.parse(source, type_comments=True)
|
||||
subject = cast(ast.FunctionDef, module.body[0])
|
||||
|
||||
if getattr(subject, "type_comment", None):
|
||||
function = ast_parse(subject.type_comment, mode='func_type')
|
||||
function = ast.parse(subject.type_comment, mode='func_type', type_comments=True)
|
||||
return signature_from_ast(subject, bound_method, function) # type: ignore
|
||||
else:
|
||||
return None
|
||||
|
@ -1,8 +1,11 @@
|
||||
"""Helpers for AST (Abstract Syntax Tree)."""
|
||||
|
||||
import ast
|
||||
import warnings
|
||||
from typing import Dict, List, Optional, Type, overload
|
||||
|
||||
from sphinx.deprecation import RemovedInSphinx70Warning
|
||||
|
||||
OPERATORS: Dict[Type[ast.AST], str] = {
|
||||
ast.Add: "+",
|
||||
ast.And: "and",
|
||||
@ -28,6 +31,10 @@ OPERATORS: Dict[Type[ast.AST], str] = {
|
||||
|
||||
def parse(code: str, mode: str = 'exec') -> "ast.AST":
|
||||
"""Parse the *code* using the built-in ast module."""
|
||||
warnings.warn(
|
||||
"'sphinx.pycode.ast.parse' is deprecated, use 'ast.parse' instead.",
|
||||
RemovedInSphinx70Warning, stacklevel=2
|
||||
)
|
||||
try:
|
||||
return ast.parse(code, mode=mode, type_comments=True)
|
||||
except SyntaxError:
|
||||
|
@ -11,7 +11,6 @@ from token import DEDENT, INDENT, NAME, NEWLINE, NUMBER, OP, STRING
|
||||
from tokenize import COMMENT, NL
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from sphinx.pycode.ast import parse as ast_parse
|
||||
from sphinx.pycode.ast import unparse as ast_unparse
|
||||
|
||||
comment_re = re.compile('^\\s*#: ?(.*)\r?\n?$')
|
||||
@ -552,7 +551,7 @@ class Parser:
|
||||
|
||||
def parse_comments(self) -> None:
|
||||
"""Parse the code and pick up comments."""
|
||||
tree = ast_parse(self.code)
|
||||
tree = ast.parse(self.code, type_comments=True)
|
||||
picker = VariableCommentPicker(self.code.splitlines(True), self.encoding)
|
||||
picker.visit(tree)
|
||||
self.annotations = picker.annotations
|
||||
|
@ -333,8 +333,7 @@ def isproperty(obj: Any) -> bool:
|
||||
|
||||
def isgenericalias(obj: Any) -> bool:
|
||||
"""Check if the object is GenericAlias."""
|
||||
if (hasattr(typing, '_GenericAlias') and # only for py37+
|
||||
isinstance(obj, typing._GenericAlias)): # type: ignore
|
||||
if isinstance(obj, typing._GenericAlias): # type: ignore
|
||||
return True
|
||||
elif (hasattr(types, 'GenericAlias') and # only for py39+
|
||||
isinstance(obj, types.GenericAlias)): # type: ignore
|
||||
|
Loading…
Reference in New Issue
Block a user