Skip to content

Commit b277abc

Browse files
committed
Use ast.parse from the standard library
1 parent eb6e137 commit b277abc

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

sphinx/domains/python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from sphinx.domains import Domain, Index, IndexEntry, ObjType
2222
from sphinx.environment import BuildEnvironment
2323
from sphinx.locale import _, __
24-
from sphinx.pycode.ast import parse as ast_parse
2524
from sphinx.roles import XRefRole
2625
from sphinx.util import logging
2726
from sphinx.util.docfields import Field, GroupedField, TypedField
@@ -206,7 +205,7 @@ def unparse(node: ast.AST) -> List[Node]:
206205
raise SyntaxError # unsupported syntax
207206

208207
try:
209-
tree = ast_parse(annotation)
208+
tree = ast.parse(annotation, type_comments=True)
210209
result: List[Node] = []
211210
for node in unparse(tree):
212211
if isinstance(node, nodes.literal):

sphinx/ext/autodoc/preserve_defaults.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import sphinx
1212
from sphinx.application import Sphinx
1313
from sphinx.locale import __
14-
from sphinx.pycode.ast import parse as ast_parse
1514
from sphinx.pycode.ast import unparse as ast_unparse
1615
from sphinx.util import logging
1716

@@ -36,10 +35,10 @@ def get_function_def(obj: Any) -> Optional[ast.FunctionDef]:
3635
if source.startswith((' ', r'\t')):
3736
# subject is placed inside class or block. To read its docstring,
3837
# this adds if-block before the declaration.
39-
module = ast_parse('if True:\n' + source)
38+
module = ast.parse('if True:\n' + source)
4039
return module.body[0].body[0] # type: ignore
4140
else:
42-
module = ast_parse(source)
41+
module = ast.parse(source)
4342
return module.body[0] # type: ignore
4443
except (OSError, TypeError): # failed to load source code
4544
return None

sphinx/ext/autodoc/type_comment.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sphinx
88
from sphinx.application import Sphinx
99
from sphinx.locale import __
10-
from sphinx.pycode.ast import parse as ast_parse
1110
from sphinx.pycode.ast import unparse as ast_unparse
1211
from sphinx.util import inspect, logging
1312

@@ -86,14 +85,14 @@ def get_type_comment(obj: Any, bound_method: bool = False) -> Signature:
8685
if source.startswith((' ', r'\t')):
8786
# subject is placed inside class or block. To read its docstring,
8887
# this adds if-block before the declaration.
89-
module = ast_parse('if True:\n' + source)
88+
module = ast.parse('if True:\n' + source, type_comments=True)
9089
subject = cast(ast.FunctionDef, module.body[0].body[0]) # type: ignore
9190
else:
92-
module = ast_parse(source)
93-
subject = cast(ast.FunctionDef, module.body[0]) # type: ignore
91+
module = ast.parse(source, type_comments=True)
92+
subject = cast(ast.FunctionDef, module.body[0])
9493

9594
if getattr(subject, "type_comment", None):
96-
function = ast_parse(subject.type_comment, mode='func_type')
95+
function = ast.parse(subject.type_comment, mode='func_type', type_comments=True)
9796
return signature_from_ast(subject, bound_method, function) # type: ignore
9897
else:
9998
return None

sphinx/pycode/ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""Helpers for AST (Abstract Syntax Tree)."""
22

33
import ast
4+
import warnings
45
from typing import Dict, List, Optional, Type, overload
56

7+
from sphinx.deprecation import RemovedInSphinx70Warning
8+
69
OPERATORS: Dict[Type[ast.AST], str] = {
710
ast.Add: "+",
811
ast.And: "and",
@@ -28,6 +31,10 @@
2831

2932
def parse(code: str, mode: str = 'exec') -> "ast.AST":
3033
"""Parse the *code* using the built-in ast module."""
34+
warnings.warn(
35+
"'sphinx.pycode.ast.parse' is deprecated, use 'ast.parse' instead.",
36+
RemovedInSphinx70Warning, stacklevel=2
37+
)
3138
try:
3239
return ast.parse(code, mode=mode, type_comments=True)
3340
except SyntaxError:

sphinx/pycode/parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from tokenize import COMMENT, NL
1212
from typing import Any, Dict, List, Optional, Tuple
1313

14-
from sphinx.pycode.ast import parse as ast_parse
1514
from sphinx.pycode.ast import unparse as ast_unparse
1615

1716
comment_re = re.compile('^\\s*#: ?(.*)\r?\n?$')
@@ -552,7 +551,7 @@ def parse(self) -> None:
552551

553552
def parse_comments(self) -> None:
554553
"""Parse the code and pick up comments."""
555-
tree = ast_parse(self.code)
554+
tree = ast.parse(self.code, type_comments=True)
556555
picker = VariableCommentPicker(self.code.splitlines(True), self.encoding)
557556
picker.visit(tree)
558557
self.annotations = picker.annotations

sphinx/util/inspect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ def isproperty(obj: Any) -> bool:
333333

334334
def isgenericalias(obj: Any) -> bool:
335335
"""Check if the object is GenericAlias."""
336-
if (hasattr(typing, '_GenericAlias') and # only for py37+
337-
isinstance(obj, typing._GenericAlias)): # type: ignore
336+
if isinstance(obj, typing._GenericAlias): # type: ignore
338337
return True
339338
elif (hasattr(types, 'GenericAlias') and # only for py39+
340339
isinstance(obj, types.GenericAlias)): # type: ignore

0 commit comments

Comments
 (0)