-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
…types it makes more sense to supply it.
I tried taking this one step further to also recognize that Also, I think that the linter that enforces that parameter defaults in stub files must be |
|
Per recent typing-sig thread, sounds like it's time for that to change :) (FWIW it's already clear that it doesn't cause trouble for pytype, because otherwise typeshed pytype tests would not pass). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just one optional suggestion.
@@ -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: ... |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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).
With
mode="exec"
(which is the default mode),ast.parse
always returns anast.Module
. It's irritating to always have tocast
this.Verified this change with a small file:
This raises an error (expected Module, got AST) on typeshed master, passes with this PR.
Also added explicit
mode="exec"
, same results.With
mode="eval"
, error on both master and this PR.