Skip to content

Commit fcb97fe

Browse files
carljmsrittau
authored andcommitted
Overload ast.parse to recognize that mode=exec means Module return. (#3039)
1 parent 5447ff6 commit fcb97fe

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

stdlib/3/ast.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import sys
55
# from _ast below when loaded in an unorthodox way by the Dropbox
66
# internal Bazel integration.
77
import typing as _typing
8-
from typing import Any, Iterator, Optional, Union, TypeVar
8+
from typing import overload, Any, Iterator, Optional, Union, TypeVar
9+
from typing_extensions import Literal
910

1011
# The same unorthodox Bazel integration causes issues with sys, which
1112
# is imported in both modules. unfortunately we can't just rename sys,
@@ -23,9 +24,18 @@ class NodeTransformer(NodeVisitor):
2324
_T = TypeVar('_T', bound=AST)
2425

2526
if sys.version_info >= (3, 8):
27+
@overload
28+
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...,
29+
type_comments: bool = ..., feature_version: int = ...) -> Module: ...
30+
31+
@overload
2632
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...,
2733
type_comments: bool = ..., feature_version: int = ...) -> AST: ...
2834
else:
35+
@overload
36+
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...) -> Module: ...
37+
38+
@overload
2939
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...
3040

3141
def copy_location(new_node: _T, old_node: AST) -> _T: ...

0 commit comments

Comments
 (0)