Skip to content

Commit 6b6d8c8

Browse files
gvanrossumsrittau
authored andcommitted
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.
1 parent 5918098 commit 6b6d8c8

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

stdlib/3/_ast.pyi

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,32 @@ class AST:
1010
_attributes: ClassVar[typing.Tuple[str, ...]]
1111
_fields: ClassVar[typing.Tuple[str, ...]]
1212
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
13+
# TODO: Not all nodes have all of the following attributes
1314
lineno: int
1415
col_offset: int
16+
if sys.version_info >= (3, 8):
17+
end_lineno: Optional[int]
18+
end_col_offset: Optional[int]
19+
type_comment: Optional[str]
1520

1621
class mod(AST):
1722
...
1823

24+
if sys.version_info >= (3, 8):
25+
class type_ignore(AST): ...
26+
27+
class TypeIgnore(type_ignore): ...
28+
29+
class FunctionType(mod):
30+
argtypes: typing.List[expr]
31+
returns: expr
32+
1933
class Module(mod):
2034
body = ... # type: typing.List[stmt]
2135
if sys.version_info >= (3, 7):
2236
docstring: Optional[str]
37+
if sys.version_info >= (3, 8):
38+
type_ignores: typing.List[TypeIgnore]
2339

2440
class Interactive(mod):
2541
body = ... # type: typing.List[stmt]
@@ -232,10 +248,10 @@ class Call(expr):
232248
args = ... # type: typing.List[expr]
233249
keywords = ... # type: typing.List[keyword]
234250

235-
class Num(expr):
236-
n = ... # type: float
251+
class Num(expr): # Deprecated in 3.8; use Constant
252+
n = ... # type: complex
237253

238-
class Str(expr):
254+
class Str(expr): # Deprecated in 3.8; use Constant
239255
s = ... # type: str
240256

241257
if sys.version_info >= (3, 6):
@@ -247,12 +263,24 @@ if sys.version_info >= (3, 6):
247263
class JoinedStr(expr):
248264
values = ... # type: typing.List[expr]
249265

250-
class Bytes(expr):
266+
class Bytes(expr): # Deprecated in 3.8; use Constant
251267
s = ... # type: bytes
252268

253269
class NameConstant(expr):
254270
value = ... # type: Any
255271

272+
if sys.version_info >= (3, 8):
273+
class Constant(expr):
274+
value: Any # None, str, bytes, bool, int, float, complex, Ellipsis
275+
kind: Optional[str]
276+
# Aliases for value, for backwards compatibility
277+
s: Any
278+
n: complex
279+
280+
class NamedExpr(expr):
281+
target: expr
282+
value: expr
283+
256284
class Ellipsis(expr): ...
257285

258286
class Attribute(expr):

stdlib/3/ast.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Python 3.5 ast
22

3+
import sys
34
# Rename typing to _typing, as not to conflict with typing imported
45
# from _ast below when loaded in an unorthodox way by the Dropbox
56
# internal Bazel integration.
@@ -17,7 +18,12 @@ class NodeTransformer(NodeVisitor):
1718

1819
_T = TypeVar('_T', bound=AST)
1920

20-
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> Module: ...
21+
if sys.version_info >= (3, 8):
22+
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...,
23+
type_comments: bool = ..., feature_version: int = ...) -> AST: ...
24+
else:
25+
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...
26+
2127
def copy_location(new_node: _T, old_node: AST) -> _T: ...
2228
def dump(node: AST, annotate_fields: bool = ..., include_attributes: bool = ...) -> str: ...
2329
def fix_missing_locations(node: _T) -> _T: ...

0 commit comments

Comments
 (0)