Support new ast features and node types introduced in Python 3.8 (#2859)

Had to adjust the return type of ast.parse() from Module to AST, which
is more truthful anyways.
This commit is contained in:
Guido van Rossum
2019-03-12 08:34:56 -07:00
committed by Sebastian Rittau
parent 5918098576
commit 6b6d8c82ac
2 changed files with 39 additions and 5 deletions

View File

@@ -10,16 +10,32 @@ class AST:
_attributes: ClassVar[typing.Tuple[str, ...]]
_fields: ClassVar[typing.Tuple[str, ...]]
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
# TODO: Not all nodes have all of the following attributes
lineno: int
col_offset: int
if sys.version_info >= (3, 8):
end_lineno: Optional[int]
end_col_offset: Optional[int]
type_comment: Optional[str]
class mod(AST):
...
if sys.version_info >= (3, 8):
class type_ignore(AST): ...
class TypeIgnore(type_ignore): ...
class FunctionType(mod):
argtypes: typing.List[expr]
returns: expr
class Module(mod):
body = ... # type: typing.List[stmt]
if sys.version_info >= (3, 7):
docstring: Optional[str]
if sys.version_info >= (3, 8):
type_ignores: typing.List[TypeIgnore]
class Interactive(mod):
body = ... # type: typing.List[stmt]
@@ -232,10 +248,10 @@ class Call(expr):
args = ... # type: typing.List[expr]
keywords = ... # type: typing.List[keyword]
class Num(expr):
n = ... # type: float
class Num(expr): # Deprecated in 3.8; use Constant
n = ... # type: complex
class Str(expr):
class Str(expr): # Deprecated in 3.8; use Constant
s = ... # type: str
if sys.version_info >= (3, 6):
@@ -247,12 +263,24 @@ if sys.version_info >= (3, 6):
class JoinedStr(expr):
values = ... # type: typing.List[expr]
class Bytes(expr):
class Bytes(expr): # Deprecated in 3.8; use Constant
s = ... # type: bytes
class NameConstant(expr):
value = ... # type: Any
if sys.version_info >= (3, 8):
class Constant(expr):
value: Any # None, str, bytes, bool, int, float, complex, Ellipsis
kind: Optional[str]
# Aliases for value, for backwards compatibility
s: Any
n: complex
class NamedExpr(expr):
target: expr
value: expr
class Ellipsis(expr): ...
class Attribute(expr):

View File

@@ -1,5 +1,6 @@
# Python 3.5 ast
import sys
# Rename typing to _typing, as not to conflict with typing imported
# from _ast below when loaded in an unorthodox way by the Dropbox
# internal Bazel integration.
@@ -17,7 +18,12 @@ class NodeTransformer(NodeVisitor):
_T = TypeVar('_T', bound=AST)
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> Module: ...
if sys.version_info >= (3, 8):
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...,
type_comments: bool = ..., feature_version: int = ...) -> AST: ...
else:
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...
def copy_location(new_node: _T, old_node: AST) -> _T: ...
def dump(node: AST, annotate_fields: bool = ..., include_attributes: bool = ...) -> str: ...
def fix_missing_locations(node: _T) -> _T: ...