Skip to content
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

bpo-35766: Merge typed_ast back into CPython #11645

Merged
merged 49 commits into from
Jan 31, 2019

Conversation

gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 22, 2019

[UPDATE: I've rewritten this comment to match the latest state of the PR.]

See the bpo issue for motivation. This is the minimal code I hope to merge into CPython. The PR here is complete except for docs. To test this, use ast.parse(source, type_comments=True).

(Speaking of backwards compatibility, my bpo issue suggests that I'd also like some features supporting older grammar versions. The only thing that I would really appreciate is being able to parse code that uses async or await as a non-keyword. But I could structure this as a follow-up PR, much smaller in size.)

https://bugs.python.org/issue35766

@gvanrossum
Copy link
Member Author

I updated the PR to be merged with master now #11605 has been merged. Everything still works, and there are no more "ghost commits" from #11605.

@gvanrossum
Copy link
Member Author

gvanrossum commented Jan 22, 2019

I'll try to fix all failing tests before trying again. In my local branch I have test_ast and test_asdl_parser working; the rest is more mysterious (it seems bad indents are parsed differently somehow).

  • test_ast.py
  • test_asdl_parser.py
  • test_syntax.py
  • test_exceptions.py
  • test_parser.py

@gvanrossum
Copy link
Member Author

gvanrossum commented Jan 23, 2019

I may have to put this off for a while (certainly I won't get it into 3.8a1). The main problem at this point is the two failing tests, test_syntax and test_exceptions. Both are failing because the grammar changes have made it so that

if 1:
foo()

reports SyntaxError instead of IndentationError at the start of line 2. The reason for that is this definition in Grammar:

# the TYPE_COMMENT in suites is only parsed for funcdefs, but can't go elsewhere due to ambiguity
suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT

Previously this was

suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

and there is code in Python/pythonrun.c::err_input that depends on INDENT being the only possibility after the NEWLINE.

I haven't figured out how to fix this yet.

This fixes

if 1:
foo()

but does nothing about

def f():
foo()

The latter still reports 'SyntaxError: invalid syntax'.

So this is not a real solution, just a stop-gap measure until I find a better solution.
Also:
- remove redundant 'c' argument from new_type_comment()
- double-check that TYPE_COMMENT in a suite is followed by NL
@gvanrossum
Copy link
Member Author

Sorry for the flood of comments. I believe I've done or responded to every code review comment. Hopefully tests will pass now.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks for updates! This looks ready, I left a bunch of really minor comments.

while (*prefix && p < tok->cur) {
if (*prefix == ' ') {
while (*p == ' ' || *p == '\t')
p++;
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure what are the rules of single line bodies. I would prefer to have them always enclosed in curly braces {...}. It looks like you mix both styles. I will label them so that you can deiced what to do.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a few more braces. I don't know what PEP 7 says, but the code here seems to mostly use the convention that a single break or goto or return doesn't need to be surrounded by braces (except when part of an if-else if-etc. sequence where most other bodies braced). So I added braces to this specific case, but not around several of the others you flagged.

arr->size *= 2;
arr->items = realloc(arr->items, arr->size * sizeof(*arr->items));
if (!arr->items)
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

Single line body.

p += 6;
while (is_type_ignore && p < tok->cur) {
if (*p == '#')
break;
Copy link
Member

Choose a reason for hiding this comment

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

Single line body.

num = NCH(ch);
type_ignores = _Py_asdl_seq_new(num, arena);
if (!type_ignores)
goto out;
Copy link
Member

Choose a reason for hiding this comment

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

Single line body.

for (i = 0; i < num; i++) {
type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
if (!ti)
goto out;
Copy link
Member

Choose a reason for hiding this comment

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

Single line body.

num = 0;
for (i = 0; i < NCH(ch); i++) {
if (TYPE(CHILD(ch, i)) == test)
num++;
Copy link
Member

Choose a reason for hiding this comment

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

One more single line body. OK, there are probably too many of them.

Copy link
Member Author

Choose a reason for hiding this comment

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

This one I fixed.

Python/ast.c Outdated

if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
ast_error(c, CHILD(n, i),
"bare * has associated type comment");
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an inconsistent indent, should be two more spaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, and fixed indents for all other ast_error calls. (I wonder if it used to have a shorter name?)

Python/ast.c Outdated
@@ -3045,15 +3182,16 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
{
REQ(n, expr_stmt);
/* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
('=' (yield_expr|testlist_star_expr))* [TYPE_COMMENT])
Copy link
Member

Choose a reason for hiding this comment

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

This comment needs to be updated to match the grammar.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Python/ast.c Outdated

if (NCH(n) == 1) {
if (num == 1 || (num == 2 && TYPE(CHILD(n, 1)) == TYPE_COMMENT)) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems to me the (num == 2 && TYPE(CHILD(n, 1)) == TYPE_COMMENT) condition can never be true now (with the updated grammar).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@gvanrossum
Copy link
Member Author

@ambv Should I just merge this, or do you want to review it?

@ambv
Copy link
Contributor

ambv commented Jan 30, 2019

Unless you're in a hurry now, I'll review and stamp it tomorrow morning my time. Mostly to familiarize myself with the changes.

@gvanrossum
Copy link
Member Author

That's fine!

@gvanrossum
Copy link
Member Author

Unsure why the Appveyor build isn't running?

@zooba
Copy link
Member

zooba commented Jan 30, 2019

      Unsure why the Appveyor build isn't running?

It looks it decided to skip the build, maybe because the last commit is a merge?

image

Copy link
Contributor

@ambv ambv left a comment

Choose a reason for hiding this comment

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

Let's merge this. It's a bit of a bigger change than promised but such is life 😉

I left one comment, @gvanrossum, that you might want to respond to.

mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
{
mod_ty res;
PyObject *req_type[3];
char *req_name[] = {"Module", "Expression", "Interactive"};
char *req_name[] = {"Module", "Expression", "Interactive", "FunctionType"};
int isinstance;

req_type[0] = (PyObject*)Module_type;
req_type[1] = (PyObject*)Expression_type;
req_type[2] = (PyObject*)Interactive_type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we be also populating req_type[3] here to FunctionType_type? The assymetry looks brittle to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm... I misunderstood what this is for. Fortunately the code in builtin_compile_impl() prevents this to be reached with mode==3. I propose to just drop support for FunctionType here.

@ambv ambv merged commit dcfcd14 into python:master Jan 31, 2019
@pablogsal
Copy link
Member

@ambv @gvanrossum I have detected some refleaks in the buildbots after this PR and I started https://bugs.python.org/issue35881 to track this problem. I already started debugging, but I would like so input in case you can see something obvious right away.

@gvanrossum gvanrossum deleted the ast-type-comments branch February 1, 2019 23:29
tyomitch added a commit to tyomitch/cpython that referenced this pull request Feb 5, 2019
pythonGH-11645 added the fourth option for `mode` argument; updating the comment accordingly.
@tyomitch
Copy link
Contributor

tyomitch commented Feb 5, 2019

This PR added a fourth option for mode argument of builtin_compile_impl(), but didn't update its documentation; please see GH-11762

msullivan added a commit to python/typed_ast that referenced this pull request Feb 6, 2019
This is mostly cribbed from python/cpython#11645,
though it also adds a new error check to new_type_comment itself.
gvanrossum pushed a commit to python/typed_ast that referenced this pull request Feb 6, 2019
This is mostly cribbed from python/cpython#11645,
though it also adds a new error check to new_type_comment itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants