diff options
| -rw-r--r-- | sphinx/domains/python.py | 3 | ||||
| -rw-r--r-- | sphinx/ext/autodoc/preserve_defaults.py | 5 | ||||
| -rw-r--r-- | sphinx/ext/autodoc/type_comment.py | 9 | ||||
| -rw-r--r-- | sphinx/pycode/ast.py | 7 | ||||
| -rw-r--r-- | sphinx/pycode/parser.py | 3 | ||||
| -rw-r--r-- | sphinx/util/inspect.py | 3 |
6 files changed, 16 insertions, 14 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 72a15433d..8d1ae56f3 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -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): diff --git a/sphinx/ext/autodoc/preserve_defaults.py b/sphinx/ext/autodoc/preserve_defaults.py index 2cd58ade5..0b6d183a4 100644 --- a/sphinx/ext/autodoc/preserve_defaults.py +++ b/sphinx/ext/autodoc/preserve_defaults.py @@ -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 diff --git a/sphinx/ext/autodoc/type_comment.py b/sphinx/ext/autodoc/type_comment.py index b500a08c1..65697ea93 100644 --- a/sphinx/ext/autodoc/type_comment.py +++ b/sphinx/ext/autodoc/type_comment.py @@ -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 diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index e61b01d18..4997ab09e 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -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: diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index e537a7726..a51892e5e 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -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 diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index 403630b93..aba7c6615 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -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 |
