Skip to content

Overload ast.parse to recognize that mode=exec means Module return. #3039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion stdlib/3/ast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import sys
# from _ast below when loaded in an unorthodox way by the Dropbox
# internal Bazel integration.
import typing as _typing
from typing import Any, Iterator, Optional, Union, TypeVar
from typing import overload, Any, Iterator, Optional, Union, TypeVar
from typing_extensions import Literal

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

if sys.version_info >= (3, 8):
@overload
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...,
type_comments: bool = ..., feature_version: int = ...) -> Module: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: You could also add the following overload, since exec is the default mode:

    @overload
    def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., *,
              type_comments: bool = ..., feature_version: int = ...) -> Module: ...

Similar for the Python 3.7 version.

(Also a style nit: Please don't add empty lines between overloads.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't get back to this sooner, thanks for the merge! Regarding this comment, I tried adding that overload and IIRC mypy complained about overlapping overloads. It seems that implicitly due to ordering of overloads, mypy already treats this as the "default" one. (I think the clearer way to express this to typecheckers would be to actually include the default value of the optional argument in the stub).


@overload
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...,
type_comments: bool = ..., feature_version: int = ...) -> AST: ...
else:
@overload
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...) -> Module: ...

@overload
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...

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